Maths student G
Maths student G

Reputation: 15

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

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!

Upvotes: 0

Views: 36

Answers (1)

TheFaultInOurStars
TheFaultInOurStars

Reputation: 3608

I guess you should try something like:

data.index = pd.to_datetime(data.index, format='%Y-%d-%m')

Upvotes: 1

Related Questions