简体   繁体   English

将熊猫对象转换为timedelta会导致NaT

[英]Converting pandas object to timedelta results in NaT

I have a DataFrame with three columns, one date and two times. 我有一个包含三列的DataFrame,一个日期和两次。 It's like this: 就像这样:

         date hour_in hour_out
0  01/06/2016        08:15      19:37   
1  02/06/2016        08:26      17:31   
2  03/06/2016        08:08      21:31

I'm trying to convert hour_in and hour_out to timedelta using this code (which is based on an answer on this question Dates from 1900-01-01 are added to my 'Time' after using df['Time'] = pd.to_datetime(phData['Time'], format='%H:%M:%S') ): 我正在尝试使用此代码将hour_inhour_out转换为timedelta(这是基于以下问题的答案: 使用df ['Time'] = pd.to_datetime之后,将1900-01-01的日期添加到我的“ Time”中(phData ['Time'],format ='%H:%M:%S') ):

df['hora_entrada'] = pd.to_timedelta(df['hora_entrada'], errors='coerce')
df['hora_saida']  = pd.to_timedelta(df['hora_saida'] , errors='coerce')

After the cast, my column is converted to the correct dtype timedelta64[ns] , but all the values are set to NaT . timedelta64[ns]转换后,我的列将转换为正确的timedelta64[ns] ,但所有值均设置为NaT My df.info() returns this: 我的df.info()返回以下内容:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 439 entries, 0 to 438
Data columns (total 4 columns):
data            439 non-null datetime64[ns]
hour_in         0 non-null timedelta64[ns]
hour_out        0 non-null timedelta64[ns]
dtypes: datetime64[ns](1), timedelta64[ns](2)

And the data output is like this: 数据输出是这样的:

          data hora_entrada hora_saida
0   2016-06-01          NaT        NaT
1   2016-06-02          NaT        NaT
2   2016-06-03          NaT        NaT

I've tried to convert the time columns to datetime and then to timedelta but I got strange results. 我试图将时间列转换为datetime时间,然后转换为时间timedelta但结果timedelta奇怪。 Here's an example: 这是一个例子:

          data          hora_entrada            hora_saida
0   2016-06-01 -25567 days +08:15:00 -25567 days +19:37:00
1   2016-06-02 -25567 days +08:26:00 -25567 days +17:31:00
2   2016-06-03 -25567 days +08:08:00 -25567 days +21:31:00

I think it's because when I convert it to datetime it's appended to the hour a date 1900-01-01 . 我认为这是因为当我将其转换为datetime时,会在datetime加上1900-01-01这个日期。

Consider the following approach: 考虑以下方法:

In [24]: pd.to_timedelta(df.hour_in + ':00', errors='coerce')
Out[24]:
0   08:15:00
1   08:26:00
2   08:08:00
Name: hour_in, dtype: timedelta64[ns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM