我是Python的初学者。
我的数据框df_all_data_0
包含列time_in
:
2018-01-13 13:17:29
2018-01-06 17:49:43
2018-01-18 09:44:37
2018-01-04 10:45:52
2018-01-11 12:58:31
我想分开日期(例如2018-01-13)和时间(13:17:29)。 我尝试了什么:
pd.to_datetime(df_all_data_0['time_in']).
date_str = df_all_data_0.strptime('%Y-%m-%d')
但我有错误:
AttributeError Traceback (most recent call last)
<ipython-input-14-d662eee68034> in <module>()
8
9
---> 10 date_str = df_all_data_0.strptime('%Y-%m-%d')
11 #print(type(date_str)) # <class 'str'>
12 #print(date_str) # 2017-10-24
/anaconda/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
2968 if name in self._info_axis:
2969 return self[name]
-> 2970 return object.__getattribute__(self, name)
2971
2972 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'strptime'
如何纠正?
答案 0 :(得分:1)
一种方法是使用pd.Series.dt.time
提取日期,时间设置为0,import pandas as pd
from io import StringIO
mystr = StringIO("""2018-01-13 13:17:29
2018-01-06 17:49:43
2018-01-18 09:44:37
2018-01-04 10:45:52
2018-01-11 12:58:31""")
df = pd.read_csv(mystr, sep='|', header=None, names=['DateTime'])
df['DateTime'] = pd.to_datetime(df['DateTime'])
df['Date'], df['Time'] = df['DateTime'].dt.normalize(), df['DateTime'].dt.time
print(df)
# DateTime Date Time
# 0 2018-01-13 13:17:29 2018-01-13 13:17:29
# 1 2018-01-06 17:49:43 2018-01-06 17:49:43
# 2 2018-01-18 09:44:37 2018-01-18 09:44:37
# 3 2018-01-04 10:45:52 2018-01-04 10:45:52
# 4 2018-01-11 12:58:31 2018-01-11 12:58:31
提取时间:
Time
请注意,dtype
列将包含print(df.dtypes)
# DateTime datetime64[ns]
# Date datetime64[ns]
# Time object
# dtype: object
对象:
{{1}}