gpt4 book ai didi

python - 来自 TimeGrouper 的组从第一个索引开始

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:56 32 4
gpt4 key购买 nike

我正在尝试以 3 小时为间隔对具有时间索引的数据帧进行分组。它以 1.5 秒的频率采样。我希望以下内容返回单个长度为 4323 的组。

import pandas as pd
time_grouper = pd.TimeGrouper("3H");
dataframe.groupby(time_grouper).count()

输出:

2013-02-23 06:00:00    1733
2013-02-23 09:00:00 1149
Freq: 3H, Name: roll, dtype: int64

如果我将时间分组频率更改为 1000 秒,我会得到:

2013-02-23 08:03:20    133
2013-02-23 08:20:00 667
2013-02-23 08:36:40 666
2013-02-23 08:53:20 667
2013-02-23 09:10:00 667
2013-02-23 09:26:40 82
Freq: 1000S, Name: roll, dtype: int64

编辑从评论中我了解到重采样从 00h00:00 开始,这解释了看似不均匀的箱子。如何让重采样从索引覆盖的时间范围开始?

最佳答案

这是使用 pd.cut() 手动构建分类组的一种可能的解决方法。

import pandas as pd
import datetime as dt

# simulate some artificial data
# ==================================================
df = pd.DataFrame(np.random.randn(4500), columns=['col'], index=pd.date_range(dt.datetime.now(), periods=4500, freq=pd.Timedelta(1.5, 's')))

col
2015-07-15 11:41:05.987156 -0.1191
2015-07-15 11:41:07.487156 -0.4531
2015-07-15 11:41:08.987156 1.2682
2015-07-15 11:41:10.487156 -1.3194
2015-07-15 11:41:11.987156 0.2690
2015-07-15 11:41:13.487156 0.3139
2015-07-15 11:41:14.987156 1.3467
2015-07-15 11:41:16.487156 -0.0090
2015-07-15 11:41:17.987156 -1.4792
2015-07-15 11:41:19.487156 -0.6973
... ...
2015-07-15 13:33:20.987156 -0.6072
2015-07-15 13:33:22.487156 0.2621
2015-07-15 13:33:23.987156 -1.1274
2015-07-15 13:33:25.487156 0.9305
2015-07-15 13:33:26.987156 0.4124
2015-07-15 13:33:28.487156 -0.8061
2015-07-15 13:33:29.987156 -0.0065
2015-07-15 13:33:31.487156 -1.3291
2015-07-15 13:33:32.987156 1.1309
2015-07-15 13:33:34.487156 -0.6444

[4500 rows x 1 columns]


# processing using pd.cut
# ==================================================
ts_rng = pd.date_range(df.index[0], df.index[-1], freq='3H')
# string format for labels
ts_rng_iso = [x.isoformat() for x in ts_rng]
# groupby the categorical variables
df.groupby(pd.cut(df.index, bins=ts_rng, labels=ts_rng_iso[:-1], right=True, include_lowest=True)).count()


col
2015-07-15T11:41:05.987156 4500

关于python - 来自 TimeGrouper 的组从第一个索引开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31424898/

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