gpt4 book ai didi

python - Pandas:将日期时间对象分配给时间间隔

转载 作者:行者123 更新时间:2023-12-01 04:26:42 25 4
gpt4 key购买 nike

我正在尝试创建一个新变量,其中将 datetime64[ns] 对象分配给 5 分钟间隔。新的间隔变量应跨越从 00:00 到 23:55 的每 5 分钟时间段。分配的标准是datetime64[ns]对象的时间是否落在相应的5分钟间隔内。我的实际数据在 DateTime 变量中有许多日期,但不应考虑这些不同的日期 - 只有时间元素对于分配很重要。

我在下面模拟了这一点。此示例重点关注大约 23:30 到 23:45 的时间段,但它应该举例说明我在 00:00 到 23:55 的所有时间间隔内试图实现的目标。我添加了两个随机日期来说明这些日期不应有任何影响。

DateTime
2009-02-18 23:32:29 - would map to interval 23:30
2009-02-18 23:34:41 - would map to interval 23:30
2009-02-18 23:35:40 - would map to interval 23.35
2009-02-18 23:39:29 - would map to interval 23:35
2009-02-18 23:39:37 - would map to interval 23:35
2009-02-18 23:40:14 - would map to interval 23:40
2009-02-18 23:43:23 - would map to interval 23:40
2009-02-18 23:44:17 - would map to interval 23:40
...
2010-03-18 23:31:19 - also maps to interval 23:30 regardless of date
2010-03-18 23:33:31 - also maps to interval 23:30 regardless of date
2010-03-18 23:36:30 - also maps to interval 23.35 regardless of date
2010-03-18 23:38:21 - also maps to interval 23:35 regardless of date
2010-03-18 23:39:07 - also maps to interval 23:35 regardless of date
2010-03-18 23:41:44 - also maps to interval 23:40 regardless of date
2010-03-18 23:42:13 - also maps to interval 23:40 regardless of date
2010-03-18 23:43:37 - also maps to interval 23:40 regardless of date

为了清楚起见,我的目标是这个结果:

DateTime             Interval 
2009-02-18 23:32:29 23:30
2009-02-18 23:34:41 23:30
2009-02-18 23:35:40 23.35
2009-02-18 23:39:29 23:35
2009-02-18 23:39:37 23:35
2009-02-18 23:40:14 23:40
2009-02-18 23:43:23 23:40
2009-02-18 23:44:17 23:40
...
2010-03-18 23:31:19 23:30
2010-03-18 23:33:31 23:30
2010-03-18 23:36:30 23.35
2010-03-18 23:38:21 23:35
2010-03-18 23:39:07 23:35
2010-03-18 23:41:44 23:40
2010-03-18 23:42:13 23:40
2010-03-18 23:43:37 23:40

我已经彻底阅读了 pandas 文档以及这里的一些非常松散相关的问题,但我似乎无法得到任何东西来实现正确的结果。

<小时/>

更新

这些是我的库和系统版本:

Pandas: 0.16.2
Numpy: 1.9.2
System version: '3.4.3 |Anaconda 2.3.0 (x86_64)| (default, Mar 6 2015, 12:07:41) \n[GCC 4.2.1 (Apple Inc. build 5577)]

这是完整的错误。在这里您可以看到,根据我的实际数据,我正在使用名为 question_timedatetime64[ns] 系列。

TypeError Traceback (most recent call last)
<ipython-input-416-d5c3256e6b40> in <module>()
----> 1 df_unique['Interval'] = ((df_unique['question_time'] - pd.TimedeltaIndex(df_unique['question_time'].dt.minute % 5, 'm')) - pd.TimedeltaIndex(df_unique['question_time'].dt.second , 's')).dt.time

//anaconda/lib/python3.4/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
2125 else:
2126 # set column
-> 2127 self._set_item(key, value)
2128
2129 def _setitem_slice(self, key, value):

//anaconda/lib/python3.4/site-packages/pandas/core/frame.py in _set_item(self, key, value)
2209 # value exeption to occur first
2210 if len(self):
-> 2211 self._check_setitem_copy()
2212
2213 def insert(self, loc, column, value, allow_duplicates=False):

//anaconda/lib/python3.4/site-packages/pandas/core/generic.py in _check_setitem_copy(self, stacklevel, t, force)
1302 raise SettingWithCopyError(t)
1303 elif value == 'warn':
-> 1304 warnings.warn(t, SettingWithCopyWarning, stacklevel=stacklevel)
1305
1306 def __delitem__(self, key):

TypeError: issubclass() arg 2 must be a class or tuple of classes

问题似乎出在 SettingWithCopyError 上。我尝试重置所有变量,现在我在另一个操作中也收到相同的警告。

最佳答案

不确定更好的方法,但您可以构造 2 个 TimeDeltaIndices 并从您的值中减去它,我使用模数 op % 来计算要减去的分钟数:

In [129]:
df['Interval'] = ((df['DateTime'] - pd.TimedeltaIndex(df['DateTime'].dt.minute % 5, 'm')) - pd.TimedeltaIndex(df['DateTime'].dt.second , 's')).dt.time
df

Out[129]:
DateTime Interval
0 2009-02-18 23:32:29 23:30:00
1 2009-02-18 23:34:41 23:30:00
2 2009-02-18 23:35:40 23:35:00
3 2009-02-18 23:39:29 23:35:00
4 2009-02-18 23:39:37 23:35:00
5 2009-02-18 23:40:14 23:40:00
6 2009-02-18 23:43:23 23:40:00
7 2009-02-18 23:44:17 23:40:00
8 2010-03-18 23:31:19 23:30:00
9 2010-03-18 23:33:31 23:30:00
10 2010-03-18 23:36:30 23:35:00
11 2010-03-18 23:38:21 23:35:00
12 2010-03-18 23:39:07 23:35:00
13 2010-03-18 23:41:44 23:40:00
14 2010-03-18 23:42:13 23:40:00
15 2010-03-18 23:43:37 23:40:00

关于python - Pandas:将日期时间对象分配给时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33014025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com