What is the rule of pandas resample with rolling 30 minutes?

84 Views Asked by At

I am trying to resample a kbars dataframe from 1 minute to 30 minutes. And I need a rolling window. However, it seems that's a fixed time frame.

For example, it's now 9:28 pm. The latest row of the reshape data = df.resample('30min') is 9:00 pm. That does not meet my needs. But it works if I change to data = df.resample('29min'). The latest row is 9:09 pm So, what is the rule of resample?

Can I resample with a 30 minute rolling window?

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',
})

Another example:

import pandas as pd
import datetime

df= [[192, 203, 188, 193],
     [195, 201, 178, 196],
     [196, 210, 182, 201],
     [199, 211, 198, 203],
     [201, 221, 197, 212],
     [203, 218, 195, 209]]

data = pd.DataFrame(df, columns=['open', 'high', 'low', 'close'], 
                  index=[
                      datetime.datetime(2024, 1, 11, 8, 57, 32),
                      datetime.datetime(2024, 1, 11, 9, 13, 12),
                      datetime.datetime(2024, 1, 11, 9, 25, 8),
                      datetime.datetime(2024, 1, 11, 9, 32, 56),
                      datetime.datetime(2024, 1, 11, 9, 42, 32),
                      datetime.datetime(2024, 1, 11, 10, 2, 11)                      
                  ])
sample_30m = data.resample('30min').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
})
sample_29m = data.resample('29min').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
})
1

There are 1 best solutions below

2
Chen Sullivan On
## another example
import pandas as pd
import datetime

df= [[192, 203, 188, 193],
     [195, 201, 178, 196],
     [196, 210, 182, 201],
     [199, 211, 198, 203],
     [201, 221, 197, 212],
     [203, 218, 195, 209]]

data = pd.DataFrame(df, columns=['open', 'high', 'low', 'close'], 
                  index=[
                      datetime.datetime(2024, 1, 11, 8, 57, 32),
                      datetime.datetime(2024, 1, 11, 9, 13, 12),
                      datetime.datetime(2024, 1, 11, 9, 25, 8),
                      datetime.datetime(2024, 1, 11, 9, 32, 56),
                      datetime.datetime(2024, 1, 11, 9, 42, 32),
                      datetime.datetime(2024, 1, 11, 10, 2, 11)                      
                  ])
sample_30m = data.resample('30min').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
})
sample_29m = data.resample('29min').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
})**strong text**