Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel



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)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading