Reputation: 2299
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, so that I am left with:
A
0 2017-04-17
1 2017-04-18
2 2017-04-19
3 2017-04-20
4 2017-04-21
the dtype of column A is object. I have tried:
df['A'] = df['A']datetime.strftime('%Y-%m-%d')
with:
import datetime as datetime
I get:
AttributeError: 'Series' object has no attribute 'strftime'
Upvotes: 4
Views: 8867
Reputation: 862641
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'>
Upvotes: 5