Home > Blockchain >  How to read german date into pandas datetime?
How to read german date into pandas datetime?

Time:05-17

I have an excel file with a column date like these:

28.02.2022 00:00:00

What I want is to get it to datetime format without the hour.

I use this and this gives me an error "time data does not match data":

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

I can't find my error since the format seems to be right. I really appreciate every help.:)

CodePudding user response:

I suspect your format= should be

'%d.%m.%Y %H:%M:%S'

and not

'%d.%m.%Y %H:%M:%Y'

(which has a duplicate %Y).

>>> pd.to_datetime('28.02.2022 00:00:00', format='%d.%m.%Y %H:%M:%S')
Timestamp('2022-02-28 00:00:00')

Whether or not you care about the time part in the data is another thing.

CodePudding user response:

You can try:

  pd.to_datetime(df['date']).date()

Something like:

  str(pd.to_datetime('28.02.2022 00:00:00').date())

would return:

  '2022-02-28'
  • Related