【问题标题】:python ValueError: The truth value of a Series is ambiguous. trying to check datetimespython ValueError: Series 的真值不明确。试图检查日期时间
【发布时间】:2021-03-18 05:08:14
【问题描述】:

我正在尝试将时间值与 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().

【问题讨论】:

  • 您在哪里看到这个错误?哪一行代码?

标签: python dataframe time


【解决方案1】:

我相信你需要这样做:

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

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

【讨论】:

  • 我认为这是正确的,因为值错误是说他们试图从可能存在多个实例的模棱两可的情况中获得 True 或 False。
猜你喜欢
  • 2017-03-27
  • 2019-12-19
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 2023-01-08
  • 2020-03-05
  • 2020-09-28
相关资源
最近更新 更多