Convert float64 column to datetime pandas

21,054

Solution 1

You can use:

df['TradeDate'] = pd.to_datetime(df['TradeDate'], format='%Y%m%d.0')
print (df)
   TradeDate
0 2010-03-29
1 2010-03-28
2 2010-03-29

But if some bad values, add errors='coerce' for replace them to NaT

print (df)
    TradeDate
0  20100329.0
1  20100328.0
2  20100329.0
3  20153030.0
4         yyy

df['TradeDate'] = pd.to_datetime(df['TradeDate'], format='%Y%m%d.0', errors='coerce')
print (df)
   TradeDate
0 2010-03-29
1 2010-03-28
2 2010-03-29
3        NaT
4        NaT

Solution 2

You can use to_datetime with a custom format on a string representation of the values:

import pandas as pd
pd.to_datetime(pd.Series([20100329.0, 20100328.0, 20100329.0]).astype(str), format='%Y%m%d.0')
Share:
21,054
JohnAndrews
Author by

JohnAndrews

Updated on March 31, 2020

Comments

  • JohnAndrews
    JohnAndrews about 4 years

    I have the following pandas DataFrame column dfA['TradeDate']:

    0     20100329.0
    1     20100328.0
    2     20100329.0
    ...
    

    and I wish to transform it to a datetime.

    Based on another tread on SO, I convert it first to a string and then apply the strptime function.

    dfA['TradeDate'] = datetime.datetime.strptime( dfA['TradeDate'].astype('int').to_string() ,'%Y%m%d')
    

    However this returns the error that my format is incorrect (ValueError).

    An issue that I spotted is that the column is not properly to string, but to an object.

    When I try:

    dfA['TradeDate'] = datetime.datetime.strptime( dfA['TradeDate'].astype(int).astype(str),'%Y%m%d')
    

    It returns: must be a Str and not Series.