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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37801321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:57:17  来源:igfitidea点击:

Python/Pandas convert string to time only

pythondatetimepandasdataframe

提问by edesz

I have the following Pandas dataframe in Python 2.7.

我在 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

The dataframe looks like this:

数据框如下所示:

  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

This dataframe comes from a *.csv file. I use Pandas to read in the *.csv file as a Pandas dataframe. When I use print dfc.dtypes, it shows me that the column Time_of_Sailhas a datatype object. I would like to convert this column to datetimedatatype BUT I only want the time part - I don't want the year, month, date.

此数据框来自 *.csv 文件。我使用 Pandas 将 *.csv 文件作为 Pandas 数据框读入。当我使用时print dfc.dtypes,它显示该列Time_of_Sail有一个数据类型object。我想将此列转换为datetime数据类型,但我只想要时间部分 - 我不想要年、月、日。

I can try this:

我可以试试这个:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]

but the problem is that the when I run print dfc.dtypesit still shows that the column Time_of_Sailis object.

但问题是当我运行print dfc.dtypes它时仍然显示该列Time_of_Sailobject.

Is there a way to convert this column into a datetime format that only has the time?

有没有办法将此列转换为只有时间的日期时间格式?

Additional Information:

附加信息:

To create the above dataframe and output, this also works:

要创建上述数据框和输出,这也适用:

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

回答by Merlin

These two lines:

这两行:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]

Can be written as:

可以写成:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'],format= '%H:%M:%S' ).dt.time

回答by Bhavani Prasad Basuthkar

Using to_timedelta,we can convert string to time format(timedelta64[ns]) by specifying units as second,min etc.,

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

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

回答by Moe Chughtai

If you just want a simple conversion you can do the below:

如果您只是想要一个简单的转换,您可以执行以下操作:

import datetime as dt

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

or you could add a holder string to your time column as below, and then convert afterwards using an apply function:

或者您可以将一个持有者字符串添加到您的时间列,如下所示,然后使用应用函数进行转换:

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))

回答by ferengi

This seems to work:

这似乎有效:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'], format='%H:%M:%S' ).apply(pd.Timestamp)

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'], format='%H:%M:%S' ).apply(pd.Timestamp)