首页 > 解决方案 > python ValueError: Series 的真值不明确。试图检查日期时间

问题描述

我正在尝试将时间值与 DateTime 进行比较。

open_time = datetime.time(6,30,00)
close_time = datetime.time(13,00,00)
list_of_time = [data_S['hour']]

data_S 只是一个 Pandas DataFrame,我正在将小时列转换为列表。

打印的 list_of_time 如下所示:

[0       04:00:00
1       04:01:00
2       04:02:00
3       04:03:00
4       04:04:00
          ...
1515    16:55:00
1516    16:56:00
1517    16:57:00
1518    16:58:00
1519    16:59:00
Name: hour, Length: 1520, dtype: object]

我有这个 for 循环来检查列表中的时间是否小于 open_time

for i in list_of_time:
    if i < open_time:
       print (I)

运行文件时出现此错误,但我不明白错误告诉我什么。

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

标签: pythondataframetime

解决方案


我相信你需要做:

list_of_time = data_S['hour'].tolist()

当您像这样获取 pandas 列时,它不仅仅是一个简单的数字列表。 tolist会这样做。


推荐阅读