从日期时间字段 python 中形成一列



这里是数据帧列及其数据类型

df['Hours'].head()
OutPut: 
0   00:00:00
1   00:00:00
2   11:38:00
3   08:40:00
Name: Hours, dtype: timedelta64[ns]

我想有条件地从中形成其他列,使其看起来像。

Hours        Test
00:00:00     N/A
00:00:00     N/A
11:38:00     02:38:00
08:40:00     Under Worked

哪里

if df['Hours'] == '00:00:00':
df[Test] = 'N/A'
elif (df['Hours'].dt.total_seconds()//3600) < 9:
df['Test'] = 'Under Worked' 
else:
df['Test'] = (df['Hours'].dt.total_seconds()//3600)-9

但它给了我错误

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我也尝试使用np.select

conditions = [
(str(df['Hours']) == '0 days 00:00:00'),
(df['Hours'].dt.total_seconds()//3600) < 9]
choices = ['NA', 'Worked Under']
df['Test'] = np.select(conditions, choices, default=(df['Hours'].dt.total_seconds()//3600)-9)

这是我得到的错误

ValueError: list of cases must be same length as list of conditions

如何解决?

使用:

df1['Hours'] = pd.to_timedelta(df1['Hours'])
conditions = [df1['Hours'] == pd.Timedelta(0), df1['Hours'] < pd.Timedelta(9, unit='H')]
choices = ['N/A', 'Under Worked']
s = df1['Hours'].sub(pd.Timedelta(9, unit='h')).astype(str).str[7:15]
df1['OT'] = np.select(conditions, choices, default=s)
print (df1)
Hours          Test            OT
0 00:00:00           N/A           N/A
1 00:00:00           N/A           N/A
2 11:38:00      02:38:00      02:38:00
3 08:40:00  Under Worked  Under Worked

最新更新