Skip to content Skip to sidebar Skip to footer

Errors In Converting Float64 Column To Datetime Pandas

I need to convert float64 type to datetime format. As an example 20181219.0 data, I want is as 2018-12-19 I have tried common code; df1['ACT_DATE1'] = pd.to_datetime(df1['ACT_DATE1

Solution 1:

First obviously problem here are some missing values, which are converted to NaT for missing datetimes.

Tested in pandas 0.25.0 and there is no problem with .0 values only necessary specify format of datetimes - %Y%m%d:

df1 = pd.DataFrame({'ACT_DATE1' : [20181219.0, np.nan]})

df1['ACT_DATE1'] = pd.to_datetime(df1['ACT_DATE1'], format='%Y%m%d') 
print (df1)
   ACT_DATE1
02018-12-191        NaT

If not working and is necessary remove missing values and converting to integer:

dates = df1['ACT_DATE1'].dropna().astype(int)
df1['ACT_DATE1'] = pd.to_datetime(dates, format='%Y%m%d') 

Post a Comment for "Errors In Converting Float64 Column To Datetime Pandas"