ZetCode

Pandas Resampling

last modified November 17, 2026 Python 3.15

Resampling means changing the frequency of a time series — a sequence of values indexed by dates or times. Downsampling combines many observations into fewer, larger buckets, such as turning hourly readings into daily totals. Upsampling does the opposite, creating more, smaller time slots from fewer observations. Pandas provides the resample method for both, and the examples below were verified with pandas 3.1.1.

Every example builds its own index with date_range, which creates a regular sequence of dates, so nothing depends on an external file. Series with a datetime index expose resample directly.

Frequency aliases

Resampling is controlled by a frequency alias, a short code for a time interval. The common ones are 'D' for a day, 'W' for a week, 'M' for a month end and 'h' for an hour. In pandas 3.x the month-end alias 'M' is deprecated for resampling, so we use 'ME' instead; it produces the same month-end dates. The snippet uses these aliases to build small date ranges and print them.

resample_aliases.py
import pandas as pd

print('D:')
print(pd.date_range('2026-01-01', periods=3, freq='D'))
print('W:')
print(pd.date_range('2026-01-01', periods=3, freq='W'))
print('ME:')
print(pd.date_range('2026-01-01', periods=3, freq='ME'))
print('h:')
print(pd.date_range('2026-01-01', periods=3, freq='h'))

Daily dates advance one day at a time. The weekly alias defaults to Sunday, so the dates are successive Sundays starting on 4 January 2026. The month-end alias gives the last day of each month, and the hourly alias advances by one hour. These same strings are what we pass to resample.

$ python resample_aliases.py
D:
DatetimeIndex(['2026-01-01', '2026-01-02', '2026-01-03'],
              dtype='datetime64[us]', freq='D')
W:
DatetimeIndex(['2026-01-04', '2026-01-11', '2026-01-18'],
              dtype='datetime64[us]', freq='W-SUN')
ME:
DatetimeIndex(['2026-01-31', '2026-02-28', '2026-03-31'],
              dtype='datetime64[us]', freq='ME')
h:
DatetimeIndex(['2026-01-01 00:00:00', '2026-01-01 01:00:00',
               '2026-01-01 02:00:00'],
              dtype='datetime64[us]', freq='h')

Downsampling with sum and mean

Here the source has twelve readings spaced six hours apart, four per day. Calling resample('D') groups them by calendar day; on its own it only defines the groups, so we finish with an aggregation method such as sum or mean to produce values. The sum adds the four readings in a day, and the mean averages them.

resample_downsample.py
import pandas as pd

idx = pd.date_range('2026-01-01', periods=12, freq='6h')
s = pd.Series(range(1, 13), index=idx)
print('sum:')
print(s.resample('D').sum())
print()
print('mean:')
print(s.resample('D').mean())

For 1 January the four values are 1, 2, 3 and 4, which add up to 10 and average 2.5. The following days contain 5 to 8 and 9 to 12, giving totals of 26 and 42 and means of 6.5 and 10.5. The result is a new Series indexed one entry per day, with Freq: D confirming the daily frequency.

$ python resample_downsample.py
sum:
2026-01-01    10
2026-01-02    26
2026-01-03    42
Freq: D, dtype: int64

mean:
2026-01-01     2.5
2026-01-02     6.5
2026-01-03    10.5
Freq: D, dtype: float64

OHLC aggregation

The ohlc method is a special aggregation borrowed from financial charts. Its name stands for open, high, low and close: the first value in each bucket, the largest, the smallest and the last. Calling it after resample returns a DataFrame with one column for each of those four numbers.

resample_ohlc.py
import pandas as pd

idx = pd.date_range('2026-01-01', periods=12, freq='6h')
s = pd.Series(range(1, 13), index=idx)
print(s.resample('D').ohlc())

On 1 January the readings rise from 1 to 4, so the bucket opens at 1, reaches a high of 4, a low of 1 and closes at 4. Because the values increase throughout, open equals low and high equals close on every day. On data that moves up and down the four columns differ, which is why this view is popular for prices.

$ python resample_ohlc.py
            open  high  low  close
2026-01-01     1     4    1      4
2026-01-02     5     8    5      8
2026-01-03     9    12    9     12

Upsampling with asfreq and ffill

Upsampling inserts new time slots that have no observation. The asfreq method creates those slots and leaves them as NaN, the marker for a missing value. The ffill method instead fills each new slot with the last known value — the initials stand for "forward fill".

resample_upsample.py
import pandas as pd

idx = pd.date_range('2026-01-01', periods=3, freq='D')
s = pd.Series([10, 20, 30], index=idx)
print('asfreq:')
print(s.resample('12h').asfreq())
print()
print('ffill:')
print(s.resample('12h').ffill())

Three daily values become six slots at twelve-hour intervals. With asfreq the original values stay at midnight and the new noon slots are NaN, which is why the dtype becomes float. With ffill each noon slot repeats the value from midnight, so the series is complete and stays as integers.

$ python resample_upsample.py
asfreq:
2026-01-01 00:00:00    10.0
2026-01-01 12:00:00     NaN
2026-01-02 00:00:00    20.0
2026-01-02 12:00:00     NaN
2026-01-03 00:00:00    30.0
Freq: 12h, dtype: float64

ffill:
2026-01-01 00:00:00    10
2026-01-01 12:00:00    10
2026-01-02 00:00:00    20
2026-01-02 12:00:00    20
2026-01-03 00:00:00    30
Freq: 12h, dtype: int64

Closed and label

Every resampling bucket has two edges, and the closed argument decides which edge belongs to the bucket: 'left', the default, includes the start but not the end, while 'right' includes the end but not the start. The label argument decides which edge is used as the bucket's label. Changing both shifts values between adjacent days.

resample_closed.py
import pandas as pd

idx = pd.date_range('2026-01-01', periods=5, freq='6h')
s = pd.Series([1, 2, 3, 4, 5], index=idx)
print('default:')
print(s.resample('D').sum())
print()
print('closed=right, label=right:')
print(s.resample('D', closed='right', label='right').sum())

By default each day starts at midnight, so 1 January collects the four values 1 to 4 and totals 10, while the midnight reading on 2 January forms the next day's total of 5. With closed='right' a midnight reading belongs to the bucket ending at that midnight: the value 1 closes the first bucket, and 2, 3, 4 and 5 add up to 14 in the second. The label='right' setting keeps each bucket labelled by its closing edge.

$ python resample_closed.py
default:
2026-01-01    10
2026-01-02     5
Freq: D, dtype: int64

closed=right, label=right:
2026-01-01     1
2026-01-02    14
Freq: D, dtype: int64

Resampling, groupby and windowing

Resampling is related to groupby, but the grouping is done for us by time. With groupby we supply a column of labels and decide how rows are grouped; with resample the datetime index is divided into regular, contiguous intervals automatically. Resampling also fills in empty intervals, which a groupby on dates would simply omit.

It is also different from a rolling window, the topic of the Pandas windowing functions tutorial. A rolling window computes a statistic over a fixed number of recent rows, such as the last seven days, and produces one result per row. Resampling produces one result per bucket, combining every row inside it. Use resampling to change the series' frequency and rolling windows to smooth it without changing that frequency.

Source

Pandas Series.resample Documentation, Pandas date_range Documentation, and Pandas Time Series Documentation

In this article we have used frequency aliases, downsampled a time series with sum, mean and ohlc, upsampled with asfreq and ffill, and controlled bucket edges with closed and label. For creating and parsing date values see Pandas datetimes, and for smoothing without changing frequency see Pandas windowing functions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pandas tutorials.