0

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

'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.
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Alejandro A
  • 1,150
  • 1
  • 9
  • 28
  • 2
    I've provided a solution here to tackle multiple datetime formats - https://stackoverflow.com/questions/61418389/pandas-format-datetime-with-many-different-date-types/61423853#61423853 Have a look.. – tidakdiinginkan Oct 09 '21 at 22:16
  • @tidakdiinginkan Thanks for your very elaborated answer, but I am looking in a way to do it with a one liner in pandas, I have been reading the docs like crazy I think the keyword "exact" will do – Alejandro A Oct 09 '21 at 22:45

1 Answers1

2

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).

FObersteiner
  • 22,500
  • 8
  • 42
  • 72