5

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

0

1 Answer 1

9

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.