Home > database >  Change DateTime Format - Python
Change DateTime Format - Python

Time:03-28

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!

CodePudding user response:

I guess you should try something like:

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