user7188934
user7188934

Reputation: 1091

Python Pandas detects the wrong datetime format

After loading data from a csv file, I set the index to the "Date" column and then convert the index to datetime.

    df1=pd.read_csv('Data.csv')
    df1=df1.set_index('Date')
    df1.index=pd.to_datetime(df1.index)

However after conversion the datetime format shows it has been misinterpreted:

original date was e.g. 01-10-2014 00:00:00

but Pandas converts it to 2014-01-10 00:00:00

How can I get Pandas to respect or recognize the original date format?

Thank you

Upvotes: 5

Views: 12171

Answers (1)

EdChum
EdChum

Reputation: 394159

Your datestrings were being interpreted as month first, you need to specify the correct format:

df1.index=pd.to_datetime(df1.index, format='%d-%m-%Y %H:%M:%S')

so that it doesn't interpret the first part as the month

In [128]:
pd.to_datetime('01-10-2014 00:00:00', format='%d-%m-%Y %H:%M:%S')

Out[128]:
Timestamp('2014-10-01 00:00:00')

Upvotes: 9

Related Questions