熊猫to_timedelta忽略单位参数?

时间:2018-07-14 21:04:56

标签: python pandas datetime dataframe timedelta

我有一个包含两列感兴趣的['Response_hour','Incident_date']的数据框,如下所示:

Response_hour  Incident_date
08             2011-01-01  
07             2011-01-01
NaN            2011-01-02

我跑步时

df['temp'] = ddf['Incident_date'] + pd.to_timedelta(df.Response_hour, unit='h')
df['temp'][0]

我得到:

Timestamp('2011-01-01 00:00:00.000000008')

为什么to_timedelta忽略了我指定的单位?

1 个答案:

答案 0 :(得分:1)

尝试使用 int 转换。如果您有NaN值,则需要先将其替换为0。通过添加0,Indident_date保持不变。

# Use fillna() to replace the values by 0
df['Response_hour'] = df['Response_hour'].fillna(0)
# force type to int
df['Response_hour'] = df['Response_hour'].astype(int)
df['temp'] = df['Incident_date'] + pd.to_timedelta(df.Response_hour, unit='h') 

礼物:

  Incident_date  Response_hour                temp
0    2011-01-01              8 2011-01-01 08:00:00
1    2011-01-01              7 2011-01-01 07:00:00
2    2011-01-01              0 2011-01-01 00:00:00
3    2011-01-01              0 2011-01-01 00:00:00