1

I'm using Pandas to read a .csv file that a 'Timestamp' date column in the format:

31/12/2016 00:00

I use the following line to convert it to a datetime64 dtype:

time = pd.to_datetime(df['Timestamp'])

The column has an entry corresponding to every 15mins for almost a year, and I've run into a problem when I want to plot more than 1 months worth.

Pandas seems to change the format from ISO to US upon reading (so YYYY:MM:DD to YYYY:DD:MM), so my plots have 30 day gaps whenever the datetime represents a new day. A plot of the first 5 days looks like:

Datetime_error

This is the raw data in the file either side of the jump:

01/01/2017 23:45
02/01/2017 00:00

If I print the values being plotted (after reading) around the 1st jump, I get:

2017-01-01 23:45:00
2017-02-01 00:00:00

So is there a way to get pandas to read the dates properly?

Thanks!

1 Answer 1

1

You can specify a format parameter in pd.to_datetime to tell pandas how to parse the date exactly, which I suppose is what you need:

time = pd.to_datetime(df['Timestamp'], format='%d/%m/%Y %H:%M')

pd.to_datetime('02/01/2017 00:00')
#Timestamp('2017-02-01 00:00:00')

pd.to_datetime('02/01/2017 00:00', format='%d/%m/%Y %H:%M')
#Timestamp('2017-01-02 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.