luisfer
luisfer

Reputation: 2130

Pandas: How do I convert a custom date format to datetime (different locale)

I have a dataframe with a Date column in format "Jan 20, 2019" or "Feb 01, 2019". This is of course, english. But my locale is spanish (ES), I think it must be something with the locale, because if I do

pd.to_datetime('Feb 01, 2019', format='%b %m, %Y')

it works, I believe is because February (Feb) is the same as Febrero (Feb), but if I do

pd.to_datetime('Jan 01, 2019', format='%b %m, %Y')

I think that's because January (Jan) is different from Enero (Ene), so before my processing I did:

import locale
locale.setlocale(locale.LC_ALL, 'EN')

But it's not working either. I'm pretty sure my format string is okay, but I'm not completely sure. Should I have to "translate" components into something easier to understand like %m/%d/%Y?

Upvotes: 1

Views: 555

Answers (1)

ASGM
ASGM

Reputation: 11391

It's because your format string is wrong. You want %d (day as a decimal number), not %m (month as a decimal number):

pd.to_datetime('Jan 01, 2019', format='%b %d, %Y')

Upvotes: 1

Related Questions