Python/Pandas将字符串转换为时间

kyxcudwk  于 2023-06-04  发布在  Python
关注(0)|答案(6)|浏览(393)

我在Python 2.7中有以下Pandas数据框。

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

dataframe看起来像这样:

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

此数据框来自 *.csv文件。我使用Pandas读取 *.csv文件作为Pandas数据框。当我使用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_Sailobject
有没有办法将此列转换为只有时间的日期时间格式?

附加信息:

要创建上面的dataframe和输出,这也可以工作:

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']
data = [
    [trial_num[0],sail_rem_time[0]],
    [trial_num[1],sail_rem_time[1]],[trial_num[2],sail_rem_time[2]],
    [trial_num[3],sail_rem_time[3]]
    ]
dfc = pd.DataFrame(data,columns=['Temp_Reading','Time_of_Sail'])
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
print dfc.dtypes
wfsdck30

wfsdck301#

这两行:

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
kadbb459

kadbb4592#

使用to_timedelta,我们可以将字符串转换为时间格式(timedelta64[ns]),方法是指定单位为秒、分钟等。

dfc['Time_of_Sail'] = pd.to_timedelta(dfc['Time_of_Sail'], unit='s')
new9mtju

new9mtju3#

这似乎起作用:
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'], format='%H:%M:%S' ).apply(pd.Timestamp)

nom7f22z

nom7f22z4#

如果有人在寻找一个更普遍的答案试试

dfc['Time_of_Sail']= pd.to_datetime(dfc['Time_of_Sail'])
fumotvh3

fumotvh35#

如果你只想做一个简单的转换,你可以做下面的:

import datetime as dt

dfc.Time_of_Sail = dfc.Time_of_Sail.astype(dt.datetime)

或者你可以像下面这样在你的time列中添加一个保持器字符串,然后使用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))
tez616oj

tez616oj6#

(使用Python 3.10.9/pandas 1.5.3测试)
您可以使用apply()函数和dt访问器将pd.to_datetime()datetime.time应用于 * 系列 *:dfc['Time_of_Sail'].apply(pd.to_datetime).dt.time

回顾一下

import pandas as pd

# Original DataFrame
>>> dfc = pd.DataFrame(
>>>     {
>>>         'Temp_Reading': [1, 2, 3, 4, 5],
>>>         'Time_of_Sail': ['11:33:11', '16:29:05', '09:37:56', '21:43:31', '17:42:06']
>>>     }
>>> )

# Convert to datetime.time object
>>> dfc['Time_of_Sail'] = dfc['Time_of_Sail'].apply(pd.to_datetime).dt.time
>>> dfc['Time_of_Sail']
    0    11:33:11
    1    16:29:05
    2    09:37:56
    3    21:43:31
    4    17:42:06
    Name: Time_of_Sail, dtype: object

数据类型

实际上,dfc.dtypes返回Time_of_Sail object,但仔细看,你会发现它们是datetime.time对象:

>>> from pprint import pprint
>>> pprint([i for i in dfc['Time_of_Sail']])
[datetime.time(11, 33, 11),
 datetime.time(16, 29, 5),
 datetime.time(9, 37, 56),
 datetime.time(21, 43, 31),
 datetime.time(17, 42, 6)]

相关问题