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

Change DateTime Format – Python

I have a dataframe with a Date as the Index. Initally my index is of the form %Y-%m-%d and the type is datetime64[ns]. I would like to change the format to %Y-%d-%m but continue to have an index of type datetime64[ns]. My problem is that when I change the format with

data.index = pd.to_datetime(data.index).strftime('%Y-%d-%m')
#or
data.index  = data.index.strftime('%Y-%d-%m')

it changes the type of the index to ‘object’ and I am not able to change it back.

To change it back I tried

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

data.index = pd.to_datetime(data.index)

but I get the error "month must be in 1..12: 2017-13-01".

Thanks a lot in advance!

>Solution :

I guess you should try something like:

data.index = pd.to_datetime(data.index, format='%Y-%d-%m')
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