Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does pd.to_datetime fail to convert?

I have an object column with values which are dates. I manually placed 2016-08-31 instead of NaN after reading from csv.

            close_date
0  1948-06-01 00:00:00   
1  2016-08-31 00:00:00   
2  2016-08-31 00:00:00   
3  1947-07-01 00:00:00   
4  1967-05-31 00:00:00

Running df['close_date'] = pd.to_datetime(df['close_date']) results in

TypeError: invalid string coercion to datetime

Adding coerce=Trueargument results in:

TypeError: to_datetime() got an unexpected keyword argument 'coerce'

Furthermore, even though I call the column 'close_date', all the columns in the dataframe, some int64, float64, and datetime64[ns], change to dtype object.

What am I doing wrong?

like image 1000
HDunn Avatar asked Sep 01 '25 00:09

HDunn


1 Answers

You need errors='coerce' parameter what convert some not parseable values to NaT:

df['close_date'] = pd.to_datetime(df['close_date'], errors='coerce')
print (df)
  close_date
0 1948-06-01
1 2016-08-31
2 2016-08-31
3 1947-07-01
4 1967-05-31

print (df['close_date'].dtypes)
datetime64[ns]

But if there are some mixed values - numeric with datetimes convert to str first:

df['close_date'] = pd.to_datetime(df['close_date'].astype(str), errors='coerce')
like image 117
jezrael Avatar answered Sep 07 '25 11:09

jezrael