Home > Software engineering >  Converting to datetime, assuming a part of the date can miss
Converting to datetime, assuming a part of the date can miss

Time:10-11

Let's say I have a simple two row df:

|     date_column      |
|----------------------|
|10/23/2001 12:34:44 AM|
|10/23/2001 12:33:44   |
|----------------------|

If I run the line:

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

I get the error of 'unconverted data remains', but I am perfectly okay with the '%p' missing, I just want to get the %m/%d/%Y.

Is there any way to achieve this? Can I tell pandas to parse only the present data in my "format" and ignore the rest, if it's missing?

UDPATE

looks like this keyword, according to the doc might do the trick:

exact: bool, True by default
Behaves as: - If True, require an exact format match. - If False, allow the format to match anywhere in the target string.

CodePudding user response:

you can simply let pandas to_datetime infer the format:

import pandas as pd

s = pd.to_datetime(["10/23/2001 12:34:44 AM", "10/23/2001 12:33:44"])

print(s)
# DatetimeIndex(['2001-10-23 00:34:44', '2001-10-23 12:33:44'], dtype='datetime64[ns]', freq=None)

Note that if AM/PM is not specified, 24h clock is assumed. Also, the month is assumed to come first (the day second).

  • Related