python - 无论哪一天,都按 5 分钟时间段对日期进行分组

标签 python pandas datetime dataframe

我有一个 DataFrame,其数据类似于以下内容

import pandas as pd; import numpy as np; import datetime; from datetime import timedelta;

df = pd.DataFrame(index=pd.date_range(start='20160102', end='20170301', freq='5min'))
df['value'] = np.random.randn(df.index.size)
df.index += pd.Series([timedelta(seconds=np.random.randint(-60, 60)) 
                       for _ in range(df.index.size)])

看起来像这样

In[37]: df
Out[37]: 
                        value
2016-01-02 00:00:33  0.546675
2016-01-02 00:04:52  1.080558
2016-01-02 00:10:46 -1.551206
2016-01-02 00:15:52 -1.278845
2016-01-02 00:19:04 -1.672387
2016-01-02 00:25:36 -0.786985
2016-01-02 00:29:35  1.067132
2016-01-02 00:34:36 -0.575365
2016-01-02 00:39:33  0.570341
2016-01-02 00:44:56 -0.636312
                      ...
2017-02-28 23:14:57 -0.027981
2017-02-28 23:19:51  0.883150
2017-02-28 23:24:15 -0.706997
2017-02-28 23:30:09 -0.954630
2017-02-28 23:35:08 -1.184881
2017-02-28 23:40:20  0.104017
2017-02-28 23:44:10 -0.678742
2017-02-28 23:49:15 -0.959857
2017-02-28 23:54:36 -1.157165
2017-02-28 23:59:10  0.527642

现在,我的目标是获取一天 24 小时内每 5 分钟的平均值 - 而不考虑这些值实际来自哪一天。

如何有效地做到这一点?我想我可以以某种方式从索引中删除实际日期,然后使用 pd.TimeGrouper 之类的东西,但我还没有弄清楚如何做到这一点。


我不太好的解决方案

到目前为止,我的解决方案是在这样的循环中使用 Between_time ,仅使用任意一天。

aggregates = []
start_time = datetime.datetime(1990, 1, 1, 0, 0, 0)

while start_time < datetime.datetime(1990, 1, 1, 23, 59, 0):
    aggregates.append(
        (
            start_time, 
            df.between_time(start_time.time(), 
                            (start_time + timedelta(minutes=5)).time(),
                            include_end=False).value.mean()
        )
    )
    start_time += timedelta(minutes=5)

result = pd.DataFrame(aggregates, columns=['time', 'value'])

按预期工作

In[68]: result
Out[68]: 
                   time     value
0   1990-01-01 00:00:00  0.032667
1   1990-01-01 00:05:00  0.117288
2   1990-01-01 00:10:00 -0.052447
3   1990-01-01 00:15:00 -0.070428
4   1990-01-01 00:20:00  0.034584
5   1990-01-01 00:25:00  0.042414
6   1990-01-01 00:30:00  0.043388
7   1990-01-01 00:35:00  0.050371
8   1990-01-01 00:40:00  0.022209
9   1990-01-01 00:45:00 -0.035161
..                  ...       ...
278 1990-01-01 23:10:00  0.073753
279 1990-01-01 23:15:00 -0.005661
280 1990-01-01 23:20:00 -0.074529
281 1990-01-01 23:25:00 -0.083190
282 1990-01-01 23:30:00 -0.036636
283 1990-01-01 23:35:00  0.006767
284 1990-01-01 23:40:00  0.043436
285 1990-01-01 23:45:00  0.011117
286 1990-01-01 23:50:00  0.020737
287 1990-01-01 23:55:00  0.021030

[288 rows x 2 columns]

但这感觉不像是一个对 Pandas 非常友好的解决方案。

最佳答案

IIUC 那么以下应该可以工作:

In [62]:
df.groupby(df.index.floor('5min').time).mean()

Out[62]:
             value
00:00:00 -0.038002
00:05:00 -0.011646
00:10:00  0.010701
00:15:00  0.034699
00:20:00  0.041164
00:25:00  0.151187
00:30:00 -0.006149
00:35:00 -0.008256
00:40:00  0.021389
00:45:00  0.016851
00:50:00 -0.074825
00:55:00  0.012861
01:00:00  0.054048
01:05:00  0.041907
01:10:00 -0.004457
01:15:00  0.052428
01:20:00 -0.021518
01:25:00 -0.019010
01:30:00  0.030887
01:35:00 -0.085415
01:40:00  0.002386
01:45:00 -0.002189
01:50:00  0.049720
01:55:00  0.032292
02:00:00 -0.043642
02:05:00  0.067132
02:10:00 -0.029628
02:15:00  0.064098
02:20:00  0.042731
02:25:00 -0.031113
...            ...
21:30:00 -0.018391
21:35:00  0.032155
21:40:00  0.035014
21:45:00 -0.016979
21:50:00 -0.025248
21:55:00  0.027896
22:00:00 -0.117036
22:05:00 -0.017970
22:10:00 -0.008494
22:15:00 -0.065303
22:20:00 -0.014623
22:25:00  0.076994
22:30:00 -0.030935
22:35:00  0.030308
22:40:00 -0.124668
22:45:00  0.064853
22:50:00  0.057913
22:55:00  0.002309
23:00:00  0.083586
23:05:00 -0.031043
23:10:00 -0.049510
23:15:00  0.003520
23:20:00  0.037135
23:25:00 -0.002231
23:30:00 -0.029592
23:35:00  0.040335
23:40:00 -0.021513
23:45:00  0.104421
23:50:00 -0.022280
23:55:00 -0.021283

[288 rows x 1 columns]

我在这里floor将索引设置为“5 分钟”间隔,然后根据时间属性进行分组并聚合平均值

关于python - 无论哪一天,都按 5 分钟时间段对日期进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42653598/

相关文章:

python - 类方法的并行执行

vb.net - 使用 GMT 偏移量的时间格式

python - 如何在 pandas 中存储仅时间时间戳?

java - Joda 日期时间、格式化和 Mysql 时间戳

python - tox 如何通过需求文件安装模块?

python - python中的列表索引比较

python - 翻转图像

python - Pandas - 使用多个值填充 NaN

python-3.x - 隐藏/显示列时调整 Matplotlib 中的轴大小

Python pandas 数据帧聚合输出