用时间差范围进行序列筛选

2024-03-28 10:43:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个数据集,它们都是timedelta64:

data1
1   17:49:43
2   09:44:37
3   10:45:52
4   12:58:31
5   16:09:18
6   14:42:53
7   16:19:24
8   15:13:03
9   13:56:40

data2
1   17:50:17
2   09:47:09
3   10:46:10
4   13:02:13
5   16:09:16
6   14:46:14
7   16:20:16
8   15:14:15
9   14:04:14

data3
1   00:00:34
2   00:02:32
3   00:00:18
4   00:03:42
5   23:59:58
6   00:03:21
7   00:00:52
8   00:01:12
9   00:07:34

我申请了:

df_temp = data3['14:00:00' < data1 < '14:50:00'].mean()

然后,我得到一个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-234-1eb15c81506e> in <module>()
----> 1 df_temp = data3['14:00:00' < data1 < '14:50:00'].mean()

/anaconda/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self)
    951         raise ValueError("The truth value of a {0} is ambiguous. "
    952                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 953                          .format(self.__class__.__name__))
    954 
    955     __bool__ = __nonzero__

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

我怎样才能纠正它?你知道吗


Tags: oftheinselfdfisvaluemean
1条回答
网友
1楼 · 发布于 2024-03-28 10:43:31

解决此问题的一种方法是将其转换为秒,然后只比较整数:

x, y = pd.to_timedelta('14:00:00').seconds, pd.to_timedelta('14:50:00').seconds

df_temp = data3[data1.dt.seconds.between(x, y)].mean()

相关问题 更多 >