滚动30分钟,熊猫重塑形状?

问题描述 投票:0回答:1

我正在尝试将 kbars 数据框从 1 分钟重塑为 30 分钟。我需要一个滚动窗口。 不过,这似乎是一个固定的时间范围。 例如,现在是晚上 9:28。重塑数据 = df.resample('30min') 的最新行是晚上 9:00。这不能满足我的需要 但如果我更改为 data = df.resample('29min').有用!!最后一排是晚上 9:09 那么,重采样的规则是什么?

我可以使用 30 分钟滚动窗口重新采样吗?

pandas reshape
1个回答
0
投票
## The following is an example code to show the difference of resample 30m and 29m
## resample of 30 minutes is fixed on the clock, not a rolling window

import pandas as pd
import numpy as np

recent_hours = 2
date_index = pd.date_range(end=pd.to_datetime('now'), periods=recent_hours * 60 // 3.4, freq='3T')
# Create a sample DataFrame with random OHLC data
data = pd.DataFrame({
    'open': np.random.rand(len(date_index)),
    'high': np.random.rand(len(date_index)),
    'low': np.random.rand(len(date_index)),
    'close': np.random.rand(len(date_index))
}, index=date_index)

# The datetime index of sample_30m is precisely aligned with the half-hour and the top of every hour.
sample_30m = data.resample('30min').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
})

# The datetime index of sample_29m is a rolling windows  
sample_29m = data.resample('29min').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
})
© www.soinside.com 2019 - 2024. All rights reserved.