python - 根据条件填充数据框时出错

标签 python pandas

我有以下函数来比较特定时间是否在两个值之间

def is_time_between(begin_time, end_time, check_time=None):
# If check time is not given, default to current UTC time
check_time = check_time or datetime.utcnow().time()
if begin_time < end_time:
    return check_time >= begin_time and check_time <= end_time
else: # crosses midnight
    return check_time >= begin_time or check_time <= end_time

该功能运行良好。我想使用以下函数来比较数据框和基于此条件填充其他列的时间值,如下所示

if is_time_between(time(5,0), time(12,59),df.time):
    df['day_interval'] = 1
elif is_time_between(time(13,0), time(17,59),df['time']):
    df['day_interval'] = 2
elif is_time_between(time(18,0), time(23,59),df['time']):
    df['day_interval'] = 3
else:
    df['day_interval']= 4

运行以下代码会引发以下错误

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

最佳答案

使用numpy.selectSeries.apply按列值返回掩码:

df = pd.DataFrame({'date':['2019-10-1 01:00:10',
                           '2019-10-2 14:00:10',
                           '2019-10-31 19:00:10',
                           '2019-10-31 06:00:10']})

df['time'] = pd.to_datetime(df['date']).dt.time
print(df)
                  date      time
0   2019-10-1 01:00:10  01:00:10
1   2019-10-2 14:00:10  14:00:10
2  2019-10-31 19:00:10  19:00:10
3  2019-10-31 06:00:10  06:00:10
<小时/>
m1 = df['time'].apply(lambda x: is_time_between(time(5,0), time(12,59), x))
m2 = df['time'].apply(lambda x: is_time_between(time(13,0), time(17,59), x))
m3 = df['time'].apply(lambda x: is_time_between(time(18,0), time(23,59), x))

df['day_interval'] = np.select([m1, m2, m3], [1,2,3], default=4)

另一个解决方案 cut并将时间转换为时间增量 to_timedelta :

bins = pd.to_timedelta(['00:00:00','05:00:00','13:00:00','18:00:00','23:59:59'])
df['day_interval1'] = pd.cut(pd.to_timedelta(df['time'].astype(str)), bins, labels=[4,1,2,3])

print (df)
                  date      time  day_interval day_interval1
0   2019-10-1 01:00:10  01:00:10             4             4
1   2019-10-2 14:00:10  14:00:10             2             2
2  2019-10-31 19:00:10  19:00:10             3             3
3  2019-10-31 06:00:10  06:00:10             1             1

关于python - 根据条件填充数据框时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413695/

相关文章:

python - 查询 Pandas 数据框

python - Django,获取 ChoiceField 表单的值

python - 是什么导致通过元类包装的方法在调用时忘记 self ?

python - 将 pandas.core.groupby.SeriesGroupBy 转换为 dataframe

Python:如何快速堆叠数据框中某一列的所有数组?

python - 将 DataFrame 拆分为来自多列的组字典

python - 如何使用Python从Json文件的嵌套列表中提取对象?

python - ImportError: libcblas.so.3: 无法打开共享对象文件: 没有那个文件或目录

python - 在〜Factory子类中调用factory_boy父类(super class)方法,不带 `self`

python - 从一列列表到多列