python - 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().
解决方案
我相信你需要做:
list_of_time = data_S['hour'].tolist()
当您像这样获取 pandas 列时,它不仅仅是一个简单的数字列表。 tolist
会这样做。
推荐阅读
- python - 如何在机器学习中使用 cross_val_score 进行预测
- vba - 1个记录集可以同时执行2个选择查询吗?
- macos - printf 在哪里定义?
- r - 将递归列表转换为 data.table R
- python - 给你一个由 n 个整数组成的数组。打印其数组值乘积的最后两位
- python - 在 Mac (Catalina) 上安装 PyGame 时出错
- sql - 在 SQL Server 中使用 CASE 语句的 MIN MAX
- json - 在 Mule 4 中合并 JSON 中的元素
- typo3 - 如何在 TSConfig addToList 中使用多行而不是线性列表?
- r - 我该如何解决“package 'ddply' is not available (for R version 3.6.2)”