0

I have a pandas column (Series) with the below rows:

DATETIMEOFCALL
--------------
13/12/2021 14:58
13/12/2021
13/12/2021 14:57:12

How could I convert them to pd.timestamp at once?

So far, I've done this, but it returns NaT for the second and third rows.

 df['DATETIMEOFCALL'] = pd.to_datetime(df['DATETIMEOFCALL'], format='%d/%m/%Y %H:%M', errors='coerce')

I would like to obtain a column with all the values in datetime format.

EDIT: Minimal Reproducible Example.

datetimes= {
    'DATETIMEOFCALL': [
        '12/10/2021 15:30',
        '10/12/2021', 
        '10/12/21', 
        '12/10/2021 14:59:59',
        '12/10/2021 14:59:69',
        None,
    ]
}
df = pd.DataFrame(datetimes)
banana_99
  • 591
  • 5
  • 15
  • Try remove `format='%d/%m/%Y %H:%M',` – jezrael Nov 25 '21 at 14:32
  • Does not work. See my edit. – banana_99 Nov 25 '21 at 14:40
  • you have a typo in your penultimate record as you have 69 seconds: `'12/10/2021 14:59:69'` --> `'12/10/2021 14:59:59'`. This will work as a one liner `df['DATETIMEOFCALL'] = df['DATETIMEOFCALL'].astype('datetime64[ns]')` https://stackoverflow.com/a/16853161/5125264 – Matt Nov 25 '21 at 14:50
  • Does this answer your question? [How do I convert strings in a Pandas data frame to a 'date' data type?](https://stackoverflow.com/questions/16852911/how-do-i-convert-strings-in-a-pandas-data-frame-to-a-date-data-type) – Matt Nov 25 '21 at 14:52
  • thanks for your answer. it's not a typo but an extreme case scenario, data input can be crazy. – banana_99 Nov 25 '21 at 14:52
  • 1
    looks like that is a separate issue to what you are asking here as certainly `14:59:69` correctly returns `NaT`. You will have to better define how to handle erroneous dates and datetimes before anyone can help you. What does a time of `14:59:69` even mean? – Matt Nov 25 '21 at 14:56

0 Answers0