Home > Enterprise >  Python Pandas change date column with format YYYYMMDD to MM-DD-YYYY
Python Pandas change date column with format YYYYMMDD to MM-DD-YYYY

Time:07-27

I have a dataframe with a column 'Valuation Date'. The data for this comes in the format YYYYMMDD. I need to change this to a normal date string (MM-DD-YYYY). This is what I have been using, but it isn't working.

It does not want to cycle through each value (only pulls the first value and uses it for each of the 84k rows. It is also iterating, which I want to avoid. Is there an easier way to do this?

There are 0 values in the data, keeping them 0 is necessary. All others need to be changed to the MM-DD-YYYY format.



for val in df['Valuation Date']:
    if val == 0:
        val == 0
    else:
        val = str(val)
        vy = val[:4]
        vm = val[4:6]
        vd = val[6:8]
        val = date(int(vy),int(vm),int(vd))   
        df['Valuation Date'] = val


Any help would be greatly appreciated!

End Solution provided by @thomask


def change_format(val):
    if val == 0:
        val == 0
    else:
        val = str(val)
        date = datetime.strptime(val,"%Y%M%d")
        new_date = date.strftime("%Y-%M-%d")
        return new_date

df['Valuation Date'] = df['Valuation Date'].apply(change_format)

CodePudding user response:

Use strptime and strftime of datetime.datetime methods:

from datetime import datetime

def change_format(val):
    val = str(val)
    date = datetime.strptime(val, "%Y%M%d")
    new_date = date.strftime("%Y-%M-%d")
    return new_date

Example :

change_format("20220622")
>>> "2022-06-22"

Then, you can apply this function to you dataframe series:

df['Valuation Date'].apply(change_format)

CodePudding user response:

You can convert the date in Pandas dataFrame using pd.to_datetime module. For your solution kindly refer the Code Snippet mentioned below: -

# Import all the Important Modules
import pandas as pd

# Data Regeneration 
df = pd.DataFrame({'Valuation Date': {0: '20160126', 1: '20160127'}})

# Date Conversion from 'YYYYMMDD' to 'MM-DD-YYYY'
df['Valuation Date'] = pd.to_datetime(df['Valuation Date']).dt.strftime('%m-%d-%Y')

# Print Result
print(df)
# Output of above Code Snippet
      Valuation Date
0     01-26-2016
1     01-27-2016

To know more about pd.to_datetime : - Click Here !!!

CodePudding user response:

it helps when you post the data to be able to reproduce. but, this seems to answer what you're looking for

df['Valuation Date'] = pd.to_datetime(df['Valuation Date']).dt.strftime('%m-%d-%Y')
  • Related