1

我有一个包含两列感兴趣的数据框,['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忽略我指定的单位?

4

1 回答 1

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
于 2018-07-14T21:12:50.003 回答