简体   繁体   中英

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

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)

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)

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')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM