python - Pandas 和日期时间类型错误 : cannot compare a TimedeltaIndex with type float

标签 python pandas datetime series

我有一个 Pandas DataFrame 系列时差,看起来像::

 print(delta_t)

 1       0 days 00:00:59
 3       0 days 00:04:22
 6       0 days 00:00:56
 8       0 days 00:01:21
 19      0 days 00:01:09
 22      0 days 00:00:36
 ...

(完整的 DataFrame 有一堆我丢弃的 NaN)。

我想知道哪些 delta_t 小于 1 天 1 小时 1 分钟,
所以我试过:
delta_t_lt1day = delta_t[np.where(delta_t < 30.)]

但后来得到了一个:
TypeError: cannot compare a TimedeltaIndex with type float

小 helper ?!?!

最佳答案

假设您的系列在 timedelta格式,可以跳过 np.where ,并使用类似这样的东西进行索引,您可以使用适当的单位将实际值与其他时间增量进行比较:

delta_t_lt1day = delta_t[delta_t < pd.Timedelta(1,'D')]

delta_t_lt1hour = delta_t[delta_t < pd.Timedelta(1,'h')]

delta_t_lt1minute = delta_t[delta_t < pd.Timedelta(1,'m')]

您将获得以下系列:
>>> delta_t_lt1day
0
1    00:00:59
3    00:04:22
6    00:00:56
8    00:01:21
19   00:01:09
22   00:00:36
Name: 1, dtype: timedelta64[ns]
>>> delta_t_lt1hour
0
1    00:00:59
3    00:04:22
6    00:00:56
8    00:01:21
19   00:01:09
22   00:00:36
Name: 1, dtype: timedelta64[ns]
>>> delta_t_lt1minute
0
1    00:00:59
6    00:00:56
22   00:00:36
Name: 1, dtype: timedelta64[ns]

关于python - Pandas 和日期时间类型错误 : cannot compare a TimedeltaIndex with type float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52939673/

相关文章:

python - 我需要一些帮助来查找列表中的列表中的内容(除其他外)

python - 根据一列中的值替换整个数据框中的空值

python - 如何将时间对象转换为总分钟数python

python - Pandas:查找最后五列满足条件的行

java - 日期验证,包括相应月份的天数

c - 如何在 C 语言中获取当前语言环境的格式化日期和时间?

python - 阅读Python串行

python - 如何在 Python 3 中测试所有可迭代对象是否为真

python - 如何使用 python-pandas 同时分解两个数据框?

python - Pandas 分组