Data Automation & Cleaning Beginner

Resample 1-Minute Intraday OHLCV Data to 5m, 15m, 1H & Daily in Python Pandas

Correctly aggregate 1-minute historical candlestick CSVs into 5-minute, 15-minute, or custom market timeframes in Python using Pandas `.resample()`.

#pandas

Problem Statement

Multi-timeframe backtesting requires converting raw 1-minute broker data into 5-minute, 15-minute, or 75-minute bars while preserving exact Open, High, Low, Close, and summed Volume.

Formula / Core Logic

Open = first(), High = max(), Low = min(), Close = last(), Volume = sum()

Installation

pip install pandas
resample-1-minute-intraday-data-to-5m-15m-1h-python.py
import pandas as pd

def resample_ohlcv(df, timeframe='5min'):
    # Assumes DatetimeIndex
    ohlc_dict = {
        'Open': 'first',
        'High': 'max',
        'Low': 'min',
        'Close': 'last',
        'Volume': 'sum'
    }
    resampled = df.resample(timeframe, label='left', closed='left').agg(ohlc_dict).dropna()
    return resampled

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Why set `label='left', closed='left'`?

Trading terminals label the 9:15:00 to 9:19:59 candle as the `09:15` bar. Left-labeling matches TradingView timestamps.