Converting dataframe column to datetime result to TypeError: Unrecognized value type: <class 'int'>

Viewed 38

I am trying to extract date and time from one column of data frame. The name of the data frame is df_BA_new and the corresponding column is 1. Below you can see the column 1

0         200604281300
1         200604281330
2         200604281335
3         200604281350
4         200604281355
    
392395    202209161320
392396    202209161325
392397    202209161330
392398    202209161335
392399    202209161340
Name: 1, Length: 392400, dtype: object

I try to convert data on this column to date time using the code shown below

df_BA_new[1] = pd.to_datetime(df_BA_new[1],format="%Y%m%d%H%M")

However, I take the following error

ValueError: unconverted data remains: 30

Thank you in advance

3 Answers

This example might help;

import pandas as pd
df = pd.DataFrame({'date': [200604281300, 200604281330, 200604281335]}) # example dataframe
df['date'] = pd.to_datetime(df['date'],format='%Y%m%d%H%M')
df

Output

0   2006-04-28 13:00:00
1   2006-04-28 13:30:00
2   2006-04-28 13:35:00
Name: date, dtype: datetime64[ns]

You almost there You need to convert type object to int64. It throwing error because you trying to give a object dtype as a input to pd. it must need int as input. For int too long we are uisng int64.

df_BA_new[1]=df_BA_new[1].astype(str).astype('int64')

df_BA_new[1] = pd.to_datetime(df_BA_new[1],format="%Y%m%d%H%M")

I don't know why, but adding errors='coerce' argument solved the problem. There was no missing data on the column that is why I did not understand what was the problem.

df_BA_new[1] =pd.to_datetime(df_BA_new[1],errors='coerce',format="%Y%m%d%H%M")

Related