Julian
Julian

Reputation: 613

Python pandas to_datetime works inconsistently: Why are month and day mixed up?

With the python pandas package I run

pd.to_datetime("23.01.2019 06:50:59")

and get the expected result of

Timestamp('2019-01-23 06:50:59')

However, when running

pd.to_datetime("11.01.2019 18:34:39")

day and month are mixed up and I get

Timestamp('2019-11-01 18:34:39')

Expected was: Timestamp('2019-01-11 18:34:39')

Any ideas about why this happens and how to avoid it? Thanks!

Upvotes: 2

Views: 1163

Answers (1)

jezrael
jezrael

Reputation: 863801

Here is possible use parameter dayfirst=True:

print (pd.to_datetime("11.01.2019 18:34:39", dayfirst=True))
2019-01-11 18:34:39

Generaly solution is with specify format of datetimes:

print (pd.to_datetime("11.01.2019 18:34:39", format='%d.%m.%Y %H:%M:%S'))
2019-01-11 18:34:39

Why are month and day mixed up?

Because pandas try guess format and MMDDYYYY have more priority like DDMMYYY.

Upvotes: 5

Related Questions