My dataframe contents are like:
df['Date']
2016/12/31 PM 04:50:00
2016/12/30 AM 02:20:00
2016/12/2 PM 01:51:00
2016/12/10 PM 03:33:00
How to get only date value? original dtype is object, I want 2016/12/31 PM 04:50:00 → 2016/12/31
Use strftime
Minimal example
>>> import pandas as pd
>>> rng = pd.date_range('1/1/2017', periods=72, freq='H')
>>> rng[0]
Timestamp('2017-01-01 00:00:00', offset='H')
>>> rng[0].strftime('%Y/%m/%d')
'2017/01/01
NB: Depending on the exact datatypes, you may need to explicitly cast.
>>> df = pd.DataFrame()
>>> df['rng'] = rng
>>> pd.DatetimeIndex(df['rng']).strftime('%Y/%m/%d')
array(['2017/01/01', '2017/01/01', '2017/01/01', '2017/01/01',
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.