user11607046
user11607046

Reputation: 197

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'], format='%Y/%m/%d')

But, what I get is 1970-01-01 00:00:00.020181219

Upvotes: 2

Views: 619

Answers (1)

jezrael
jezrael

Reputation: 863501

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
0 2018-12-19
1        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') 

Upvotes: 1

Related Questions