Early access is open. Free sample datasets are live now. Download a sample

Learn

Cleaning and Resampling Market Data with pandas

8 min read · Updated 19 November 2026 · By the marketdata.co data team

df.resample("5min").mean() runs without an error and gives you garbage OHLC bars. Market data needs a different rule for each column, careful handling of gaps, and a clear idea of which side of the bar a timestamp labels. This tutorial uses two of our free files, 1-minute bars and raw trades, so you can check every number yourself.

Details

The rules for each column

When you turn small bars into bigger ones, each field aggregates differently:

Field Rule
open first
high max
low min
close last
volume, quote_volume, trade_count sum
vwap volume-weighted mean of the smaller bars' vwap

The second decision is labelling. Our timestamps mark the start of each bar, so the 5-minute bar stamped 15:40 covers 15:40:00 up to, but not including, 15:45:00. In pandas that is label="left", closed="left", which is the default for minute frequencies, but write it out anyway so nobody has to guess.

A real example

The first five 1-minute bars in our BTC-USDT file (15:40 to 15:44 UTC on 31 October 2026) resample to one 5-minute bar:

timestamp open high low close volume trade_count
2026-10-31 15:40:00+00:00 77598.76 77628.53 77576.34 77610.56 43.81601 6268
2026-10-31 15:45:00+00:00 77610.57 77612.0 77580.2 77580.21 36.62629 5144

Computed from our free BTC-USDT 1-minute sample. Source: Binance

Sample data

Resample bars, then build bars from trades

"""Example: 1-minute bars to 5-minute bars, and trades to 10-second bars."""
import pandas as pd

bars = pd.read_csv("btc-usdt-1m-bars.csv", parse_dates=["timestamp"])
bars = bars.set_index("timestamp").sort_index()

bars["pv"] = bars["vwap"] * bars["volume"]
agg = {"open": "first", "high": "max", "low": "min", "close": "last",
       "volume": "sum", "quote_volume": "sum", "trade_count": "sum", "pv": "sum"}

b5 = bars.resample("5min", label="left", closed="left").agg(agg)
b5["vwap"] = b5["pv"] / b5["volume"]
b5 = b5.drop(columns="pv").dropna(subset=["open"])
print(b5.head(2))

trades = pd.read_csv("btc-usdt-trades.csv", parse_dates=["timestamp"])
trades = trades.set_index("timestamp").sort_index()

t10 = trades["price"].resample("10s").ohlc()
t10["volume"] = trades["size"].resample("10s").sum()
t10["trade_count"] = trades["price"].resample("10s").count()
print(t10)

The trades file holds the first 500 BTC-USDT trades of 1 November 2026, and they all arrive within 29.5 seconds. That gives three 10-second bars:

timestamp open high low close volume trade_count
2026-11-01 00:00:00+00:00 77582.02 77582.03 77572.66 77572.67 0.61454 258
2026-11-01 00:00:10+00:00 77572.66 77572.67 77565.1 77565.11 1.86292 190
2026-11-01 00:00:20+00:00 77565.11 77565.11 77565.1 77565.1 0.31435 52

Computed from our free BTC-USDT trades sample. Source: Binance

The dropna line on the 5-minute bars matters. When a resample window has no rows, pandas creates an empty bar with NaN prices and zero volume. For crypto that is rare; for stocks, every night and weekend produces thousands of them.

Watch out

Common mistakes

Using .mean() or .last() on everything. Averages smear the high and low. Use the per-column rules above.

Averaging VWAP. A plain mean of 1-minute VWAPs gives quiet minutes the same weight as busy ones. Weight by volume, as the pv column does.

Forward-filling prices into empty bars. It hides gaps and invents trades that never happened. If you need a regular grid, fill the close forward but set volume to zero, and keep a flag so the strategy knows the bar is synthetic.

Mixing time zones. Our timestamps are UTC (the Z suffix). For US stocks, convert with tz_convert("America/New_York") before filtering the 9:30 to 16:00 session, or daylight saving will shift your window by an hour twice a year.

Resampling daily stock bars across sessions. For intraday stock data, resample within each session, or a 1-hour bar can glue after-hours trading to the next morning.

Details

Get the data

Practise on real files. Our free crypto samples use the same column names as every paid dataset.

FAQ

Frequently asked questions

How do I resample stock data in pandas?

Set the timestamp as the index, then call resample with a frequency like "5min" and aggregate each column with its own rule: first open, max high, min low, last close, sum volume. Drop empty bars afterwards.

What do label and closed mean in pandas resample?

closed sets which edge of each window is included, and label sets which edge names the bar. For data stamped at the bar start, use label="left", closed="left".

How do I convert tick data to OHLC bars?

Resample the price column with .ohlc() and the size column with .sum() on the same frequency. Count rows to get trade_count.

Why does my resampled data have NaN rows?

Pandas creates a bar for every window in the range, including windows with no data such as nights and weekends. Drop rows where open is missing, or filter to trading hours first.

Keep reading