Examples
Start by creating a series with 8 one minute timestamps:

In [1]:
import numpy as np
import pandas as pd
In [2]:
index = pd.date_range('1/1/2019', periods=8, freq='T')
series = pd.Series(range(8), index=index)
series
Out[2]:
2019-01-01 00:00:00    0
2019-01-01 00:01:00    1
2019-01-01 00:02:00    2
2019-01-01 00:03:00    3
2019-01-01 00:04:00    4
2019-01-01 00:05:00    5
2019-01-01 00:06:00    6
2019-01-01 00:07:00    7
Freq: T, dtype: int64

Downsample the series into 3 minute bins and sum the values of the timestamps
falling into a bin.

In [3]:
series.resample('3T').sum()
Out[3]:
2019-01-01 00:00:00     3
2019-01-01 00:03:00    12
2019-01-01 00:06:00    13
Freq: 3T, dtype: int64

Downsample the series into 3 minute bins as above, but label each bin using the right edge
instead of the left:

In [4]:
series.resample('3T', label='right').sum()
Out[4]:
2019-01-01 00:03:00     3
2019-01-01 00:06:00    12
2019-01-01 00:09:00    13
Freq: 3T, dtype: int64

Downsample the series into 3 minute bins as above, but close the right side
of the bin interval.

In [5]:
series.resample('3T', label='right', closed='right').sum()
Out[5]:
2019-01-01 00:00:00     0
2019-01-01 00:03:00     6
2019-01-01 00:06:00    15
2019-01-01 00:09:00     7
Freq: 3T, dtype: int64

Upsample the series into 30 second bins.

In [6]:
series.resample('30S').asfreq()[0:5]   # Select first 5 rows
Out[6]:
2019-01-01 00:00:00    0.0
2019-01-01 00:00:30    NaN
2019-01-01 00:01:00    1.0
2019-01-01 00:01:30    NaN
2019-01-01 00:02:00    2.0
Freq: 30S, dtype: float64

Upsample the series into 30 second bins and fill the NaN values using the pad method.

In [7]:
series.resample('30S').pad()[0:5]
Out[7]:
2019-01-01 00:00:00    0
2019-01-01 00:00:30    0
2019-01-01 00:01:00    1
2019-01-01 00:01:30    1
2019-01-01 00:02:00    2
Freq: 30S, dtype: int64

Upsample the series into 30 second bins and fill the NaN values using the bfill method.

In [8]:
series.resample('30S').bfill()[0:5]
Out[8]:
2019-01-01 00:00:00    0
2019-01-01 00:00:30    1
2019-01-01 00:01:00    1
2019-01-01 00:01:30    2
2019-01-01 00:02:00    2
Freq: 30S, dtype: int64

Pass a custom function via apply

In [9]:
def custom_resampler(array_like):
    return np.sum(array_like) + 5

series.resample('3T').apply(custom_resampler)
Out[9]:
2019-01-01 00:00:00     8
2019-01-01 00:03:00    17
2019-01-01 00:06:00    18
Freq: 3T, dtype: int64

For a Series with a PeriodIndex, the keyword convention can be used to control whether
to use the start or end of rule.

Resample a year by quarter using ‘start’ convention. Values are assigned to the first
quarter of the period:

In [10]:
s = pd.Series([1, 2], index=pd.period_range('2018-01-01',
                                            freq='A',
                                            periods=2))
s
Out[10]:
2018    1
2019    2
Freq: A-DEC, dtype: int64
In [11]:
s.resample('Q', convention='start').asfreq()
Out[11]:
2018Q1    1.0
2018Q2    NaN
2018Q3    NaN
2018Q4    NaN
2019Q1    2.0
2019Q2    NaN
2019Q3    NaN
2019Q4    NaN
Freq: Q-DEC, dtype: float64

Resample quarters by month using ‘end’ convention. Values are assigned to the last
month of the period.

In [12]:
q = pd.Series([1, 2, 3, 4], index=pd.period_range('2019-01-01',
                                                   freq='Q',
                                                   periods=4))
q
Out[12]:
2019Q1    1
2019Q2    2
2019Q3    3
2019Q4    4
Freq: Q-DEC, dtype: int64
In [13]:
q.resample('M', convention='end').asfreq()
Out[13]:
2019-03    1.0
2019-04    NaN
2019-05    NaN
2019-06    2.0
2019-07    NaN
2019-08    NaN
2019-09    3.0
2019-10    NaN
2019-11    NaN
2019-12    4.0
Freq: M, dtype: float64

For DataFrame objects, the keyword on can be used to specify the column instead
of the index for resampling:

In [14]:
d = dict({'price': [20, 21, 19, 23, 24, 28, 27, 29],
          'volume': [60, 70, 50, 100, 60, 100, 40, 60]})
df = pd.DataFrame(d)
df['week_starting'] = pd.date_range('01/01/2019',
                                        periods=8,
                                        freq='W')
df
Out[14]:
price volume week_starting
0 20 60 2019-01-06
1 21 70 2019-01-13
2 19 50 2019-01-20
3 23 100 2019-01-27
4 24 60 2019-02-03
5 28 100 2019-02-10
6 27 40 2019-02-17
7 29 60 2019-02-24
In [15]:
df.resample('M', on='week_starting').mean()
Out[15]:
price volume
week_starting
2019-01-31 20.75 70.0
2019-02-28 27.00 65.0

For a DataFrame with MultiIndex, the keyword level can be used to specify on which level
the resampling needs to take place.

In [16]:
days = pd.date_range('1/1/2019', periods=4, freq='D')
d2 = dict({'price': [20, 21, 19, 23, 24, 28, 27, 29],
           'volume': [60, 70, 50, 100, 60, 100, 50, 60]})
df2 = pd.DataFrame(d2,
                   index=pd.MultiIndex.from_product([days,
                                                    ['morning',
                                                    'afternoon']]
                                                    ))
df2
Out[16]:
price volume
2019-01-01 morning 20 60
afternoon 21 70
2019-01-02 morning 19 50
afternoon 23 100
2019-01-03 morning 24 60
afternoon 28 100
2019-01-04 morning 27 50
afternoon 29 60
In [17]:
df2.resample('D', level=0).sum()
Out[17]:
price volume
2019-01-01 41 130
2019-01-02 42 150
2019-01-03 52 160
2019-01-04 56 110