gpt4 book ai didi

python - Pandas 将时间序列数据重新采样为 15 分钟和 45 分钟 - 使用多索引或列

转载 作者:太空狗 更新时间:2023-10-30 00:04:43 25 4
gpt4 key购买 nike

我有一些时间序列数据作为 Pandas 数据框,它从每小时过去 15 分钟和过去 45 分钟(时间间隔为 30 分钟)的观察开始,然后将频率更改为每分钟。我想对数据重新采样,以使其在整个数据帧的过去 15 小时和过去 ​​45 小时的固定频率为每 30 分钟一次。

我想到了两种实现方式。
1. 简单地过滤 dataframe 以获取 15min 和 45min 的所有观察值,使用时间序列数据作为 dataframe 中的一列。
2.重新设置索引,使时间序列数据成为多索引的一部分(索引的第0级是气象站,第1级是观测时间)并使用Pandas date-time timeseries resample() 等功能。

原始数据框,天气,看起来像这样:

                  parsed_time           Pressure  Temp    Hum
Station (index)
Bow 1 2018-04-15 14:15:00 1012 20.0 87
2 2018-04-15 14:45:00 1013 20.0 87
3 2018-04-15 15:15:00 1012 21.0 87
4 2018-04-15 15:45:00 1014 22.0 86
5 2018-04-15 16:00:00 1015 22.0 86
6 2018-04-15 16:01:00 1012 25.0 86
7 2018-04-15 16:02:00 1012 25.0 86
Stratford 8 2018-04-15 14:15:00 1011 18.0 87
9 2018-04-15 14:45:00 1011 18.0 87
10 2018-04-15 15:15:00 1012 18.0 87
11 2018-04-15 15:45:00 1014 19.0 86
12 2018-04-15 16:00:00 1014 19.0 86
13 2018-04-15 16:01:00 1015 19.0 86
14 2018-04-15 16:02:00 1016 20.0 86
15 2018-04-15 16:04:00 1016 20.0 86

使用方法 1 时,我遇到了一个问题,即我的 bool 选择操作似乎没有按预期工作。例如

weather_test = weather[weather['parsed_time'].dt.minute == (15 & 45)]

像这样给出 parsed_time 值:

2018-04-15 14:13:00
2018-04-15 15:13:00

weather_test = weather[weather['parsed_time'].dt.minute == (15 | 45)]

结果是这样的 parsed_time 值:

2018-04-15 14:47:00
2018-04-15 14:47:00

我在文档中找不到任何内容来解释这种行为。我想要的是以下时间站的压力、温度、湿度:

2018-04-15 14:45:00    
2018-04-15 15:15:00
2018-04-15 15:45:00
2018-04-15 16:15:00

等等。

对于方法 2,我想到了对数据进行重新采样,以便将我拥有每分钟数据的观察值替换为前 30 分钟的平均值。此功能似乎仅在 parsed_time 列是索引的一部分时才起作用,因此我使用以下代码将 parsed_time 设置为多索引的一部分:

weather.set_index('parsed_time', append=True, inplace=True)
weather.index.set_names('station', level=0, inplace=True)
weather = weather.reset_index(level=1, drop=True)

最终得到一个如下所示的数据框:

                                  Pressure   Temp    Hum
Station parsed_time
Bow 2018-04-15 14:15:00 1012 20.0 87
2018-04-15 14:45:00 1013 20.0 87
2018-04-15 15:15:00 1012 21.0 87
2018-04-15 15:45:00 1014 22.0 86
2018-04-15 16:00:00 1015 22.0 86
2018-04-15 16:01:00 1012 25.0 86
2018-04-15 16:02:00 1012 25.0 86
Stratford 2018-04-15 14:15:00 1011 18.0 87
2018-04-15 14:45:00 1011 18.0 87
2018-04-15 15:15:00 1012 18.0 87
2018-04-15 15:45:00 1014 19.0 86
2018-04-15 16:00:00 1014 19.0 86
2018-04-15 16:01:00 1015 19.0 86
2018-04-15 16:02:00 1016 20.0 86
2018-04-15 16:04:00 1016 20.0 86

请注意,观察的抽样从每 30 分钟的过去 :15 和过去的 :45 到每分钟(例如:01、:02、:14 等)不等,而且它也因站点而异——并非所有站点都有每一次观察。

我试过这个:

weather_test = weather.resample('30min', level=1).mean()

但这会在没有偏移的情况下重新采样,并且还会摆脱多索引中的站点级别。

想要的结果是这样的:

                              Pressure   Temp    Hum
Station parsed_time
Bow 2018-04-15 14:15:00 1012 20.0 87
2018-04-15 14:45:00 1013 20.0 87
2018-04-15 15:15:00 1012 21.0 87
2018-04-15 15:45:00 1014 22.0 86
2018-04-15 16:15:00 1013 24.0 86
Stratford 2018-04-15 14:15:00 1011 18.0 87
2018-04-15 14:45:00 1011 18.0 87
2018-04-15 15:15:00 1012 18.0 87
2018-04-15 15:45:00 1014 19.0 86
2018-04-15 16:15:00 1015 19.5 86

其中每分钟的观察值已重新采样为 30 分钟间隔的平均值,时间为每小时 15 分和 45 分。

将站点保持在多索引中的一个级别是必不可少的。我不能单独使用时间索引作为索引,因为每个站点的值都重复(并且不是唯一的)。

感谢所有帮助,因为我一直在绕着这个圈子转了一段时间。谢谢!

我看了很多以前的帖子,包括: Boolean filter using a timestamp value on a dataframe in Python
How do I round datetime column to nearest quarter hour
和:Resampling a pandas dataframe with multi-index containing timeseries对于应该很简单的东西来说,这似乎有点复杂......

和文档:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html谢谢!

最佳答案

从倒数第二个数据帧开始(在使用 weather.reset_index(Station, inplace=True) 之后):

                           Station  Pressure  Temp   Hum
parsed_time
2018-04-15 14:15:00 Bow 1012.0 20.0 87.0
2018-04-15 14:45:00 Bow 1013.0 20.0 87.0
2018-04-15 15:15:00 Bow 1012.0 21.0 87.0
2018-04-15 15:45:00 Bow 1014.0 22.0 86.0
2018-04-15 16:00:00 Bow 1015.0 22.0 86.0
2018-04-15 16:01:00 Bow 1012.0 25.0 86.0
2018-04-15 16:02:00 Bow 1012.0 25.0 86.0
2018-04-15 14:15:00 Stratford 1011.0 18.0 87.0
2018-04-15 14:45:00 Stratford 1011.0 18.0 87.0
2018-04-15 15:15:00 Stratford 1012.0 18.0 87.0
2018-04-15 15:45:00 Stratford 1014.0 19.0 86.0
2018-04-15 16:00:00 Stratford 1014.0 19.0 86.0
2018-04-15 16:01:00 Stratford 1015.0 19.0 86.0
2018-04-15 16:02:00 Stratford 1016.0 20.0 86.0
2018-04-15 16:04:00 Stratford 1016.0 20.0 86.0

您可以结合使用 groupbyresample:

res = weather.groupby('Station').resample('30min').mean().reset_index('Station')

默认情况下,resample 选择 bin 间隔 [16:00, 16:30)[16:30, 17:00)。正如您已经注意到的,时间索引在没有偏移的情况下重新采样,但您可以在之后使用 DateOffset 将其添加回去:

res.index = res.index + pd.DateOffset(minutes=15)

产生:

                           Station  Pressure  Temp   Hum
parsed_time
2018-04-15 14:15:00 Bow 1012.00 20.0 87.0
2018-04-15 14:45:00 Bow 1013.00 20.0 87.0
2018-04-15 15:15:00 Bow 1012.00 21.0 87.0
2018-04-15 15:45:00 Bow 1014.00 22.0 86.0
2018-04-15 16:15:00 Bow 1013.00 24.0 86.0
2018-04-15 14:15:00 Stratford 1011.00 18.0 87.0
2018-04-15 14:45:00 Stratford 1011.00 18.0 87.0
2018-04-15 15:15:00 Stratford 1012.00 18.0 87.0
2018-04-15 15:45:00 Stratford 1014.00 19.0 86.0
2018-04-15 16:15:00 Stratford 1015.25 19.5 86.0

或者,您可以直接在重采样方法中指定偏移量:

weather.groupby('Station').resample('30min', loffset=pd.Timedelta('15min')).mean()

关于python - Pandas 将时间序列数据重新采样为 15 分钟和 45 分钟 - 使用多索引或列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51705583/

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