我在Python 2.7中有以下Pandas数据帧。
GroupSections
数据框如下所示:
import pandas as pd
trial_num = [1,2,3,4,5]
sail_rem_time = ['11:33:11','16:29:05','09:37:56','21:43:31','17:42:06']
dfc = pd.DataFrame(zip(*[trial_num,sail_rem_time]),columns=['Temp_Reading','Time_of_Sail'])
print dfc
此数据框来自* .csv文件。我使用Pandas将* .csv文件作为Pandas数据帧读入。当我使用 Temp_Reading Time_of_Sail
1 11:33:11
2 16:29:05
3 09:37:56
4 21:43:31
5 17:42:06
时,它会向我显示列print dfc.dtypes
的数据类型为Time_of_Sail
。我想将此列转换为object
数据类型但我只想要时间部分 - 我不想要年,月,日。
我可以试试这个:
datetime
但问题是,当我运行dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]
时,它仍会显示列print dfc.dtypes
为Time_of_Sail
。
有没有办法将此列转换为只有时间的日期时间格式?
其他信息:
要创建上述数据框和输出,这也有效:
object
答案 0 :(得分:20)
这两行:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]
可以写成:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'],format= '%H:%M:%S' ).dt.time
答案 1 :(得分:4)
使用to_timedelta,我们可以通过将单位指定为second,min等来将字符串转换为时间格式(timedelta64 [ns]),
dfc['Time_of_Sail'] = pd.to_timedelta(dfc['Time_of_Sail'], unit='s')
答案 2 :(得分:2)
如果您只想进行简单转换,可以执行以下操作:
import datetime as dt
dfc.Time_of_Sail = dfc.Time_of_Sail.astype(dt.datetime)
或者你可以在你的时间栏中添加一个持有人字符串,如下所示,然后使用apply函数进行转换:
dfc.Time_of_Sail = dfc.Time_of_Sail.apply(lambda x: '2016-01-01 ' + str(x))
dfc.Time_of_Sail = pd.to_datetime(dfc.Time_of_Sail).apply(lambda x: dt.datetime.time(x))
答案 3 :(得分:0)
这似乎可行:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'], format='%H:%M:%S' ).apply(pd.Timestamp)