Skip to content Skip to sidebar Skip to footer

Pandas Convert Column To Datetime

I have this df: A 0 2017-04-17 00:00:00 1 2017-04-18 00:00:00 2 2017-04-19 00:00:00 3 2017-04-20 00:00:00 4 2017-04-21 00:00:00 I am trying to get rid of the H, M, S

Solution 1:

I think you need dt.strftime - output are strings:

#if necessary#df['A'] = pd.to_datetime(df['A'])print (type(df.loc[0, 'A']))
<class 'pandas.tslib.Timestamp'>

df['A'] = df['A'].dt.strftime('%Y-%m-%d')
print (df)
            A
0  2017-04-17
1  2017-04-18
2  2017-04-19
3  2017-04-20
4  2017-04-21

print (type(df.loc[0, 'A']))
<class 'str'>

and for dates use date:

df['A'] = df['A'].dt.date
print (df)

            A
0  2017-04-17
1  2017-04-18
2  2017-04-19
3  2017-04-20
4  2017-04-21

print (type(df.loc[0, 'A']))
<class 'datetime.date'>

Post a Comment for "Pandas Convert Column To Datetime"