簡體   English   中英

如何在熊貓中創建具有偏移量的滾動時間窗口

[英]How to create a rolling time window with offset in Pandas

我想對具有時間偏移的時間窗口內的記錄應用一些統計信息。 我的數據如下所示:

                             lon        lat  stat  ...   speed  course  head
ts                                                 ...                      
2016-09-30 22:00:33.272  5.41463  53.173161    15  ...     0.0     0.0   511
2016-09-30 22:01:42.879  5.41459  53.173180    15  ...     0.0     0.0   511
2016-09-30 22:02:42.879  5.41461  53.173161    15  ...     0.0     0.0   511
2016-09-30 22:03:44.051  5.41464  53.173168    15  ...     0.0     0.0   511
2016-09-30 22:04:53.013  5.41462  53.173141    15  ...     0.0     0.0   511

[5 rows x 7 columns]

我需要600秒的時間窗口內的記錄,步長為300秒。 例如,這些窗口:

start                     end
2016-09-30 22:00:00.000   2016-09-30 22:10:00.000
2016-09-30 22:05:00.000   2016-09-30 22:15:00.000
2016-09-30 22:10:00.000   2016-09-30 22:20:00.000

我看着熊貓滾動這樣做。 但似乎它沒有選擇添加我上面描述的偏移量。 我是否正在忽略某些內容,還是應該為此創建自定義函數?

通過將DataFrame.resampleDataFrame.shift結合DataFrame.resample ,可以實現您想要的目標。

import pandas as pd

index = pd.date_range('1/1/2000', periods=9, freq='T')
series = pd.Series(range(9), index=index)
df = pd.DataFrame(series)

這將為您提供原始的時間序列(示例取自api docs DataFrame.resample )。

2000-01-01 00:00:00  0                                                                                                                                                                        
2000-01-01 00:01:00  1                                                                                                                                                                        
2000-01-01 00:02:00  2                                                                                                                                                                        
2000-01-01 00:03:00  3                                                                                                                                                                        
2000-01-01 00:04:00  4                                                                                                                                                                        
2000-01-01 00:05:00  5                                                                                                                                                                        
2000-01-01 00:06:00  6                                                                                                                                                                        
2000-01-01 00:07:00  7                                                                                                                                                                        
2000-01-01 00:08:00  8

現在按照長重新采樣(請參閱DataFrame.shift )。

sampled = df.resample('90s').sum()

這將為您提供步長大小不重疊的窗口。

2000-01-01 00:00:00   1                                                                                                                                                                       
2000-01-01 00:01:30   2                                                                                                                                                                       
2000-01-01 00:03:00   7                                                                                                                                                                       
2000-01-01 00:04:30   5                                                                                                                                                                       
2000-01-01 00:06:00  13                                                                                                                                                                       
2000-01-01 00:07:30   8

最后,將采樣的df移一級並將其與先前創建的df相加。 窗口大小是步長大小的兩倍有幫助。

sampled.shift(1, fill_value=0) + sampled

這將產生:

2000-01-01 00:00:00   1                                                                                                                                                                       
2000-01-01 00:01:30   3                                                                                                                                                                       
2000-01-01 00:03:00   9                                                                                                                                                                       
2000-01-01 00:04:30  12                                                                                                                                                                       
2000-01-01 00:06:00  18                                                                                                                                                                       
2000-01-01 00:07:30  21 

可能會有更優雅的解決方案,但我希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM