Georg Heiler
Georg Heiler

Reputation: 17676

pandas handle column with different date time formats gracefully

I have a column with a birthdate. Some are N.A, some 01.01.2016 but some contain 01.01.2016 01:01:01 Filtering the N.A. values works fine. But handling the different date formats seems clumsy. Is it possible to have pandas handle these gracefully and e.g. for a birthdate only interpret the date and not fail?

Upvotes: 1

Views: 1368

Answers (1)

Asish M.
Asish M.

Reputation: 2647

pd.to_datetime() will handle multiple formats

>>> ser = pd.Series(['NaT', '01.01.2016', '01.01.2016 01:01:01'])
>>> pd.to_datetime(ser)
0                   NaT
1   2016-01-01 00:00:00
2   2016-01-01 01:01:01
dtype: datetime64[ns]

Upvotes: 4

Related Questions