Pandas DataFrame resample() Method

Rate this post


Before any data manipulation can occur, one (1) new library will require installation.

  • The Pandas library enables access to/from a DataFrame.

To install this library, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

💡 Note: The pytz comes packaged with pandas and does not require installation. However, this library is needed for the tz_ localize() and tz_convert() methods to work.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

If the installation was successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required library.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import pytz

DataFrame resample()

The resample() method is useful for manipulating the frequency and time-series data.

This DataFrame/Series must contain a datetime-like index, for example:

The syntax for this method is as follows:

DataFrame.resample(rule, axis=0, closed=None, label=None, convention='start', kind=None, loffset=None, base=None, on=None, level=None, origin='start_day', offset=None)
ruleThis parameter is the offset (string/object) representing a target conversion.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
closedThis parameter determines which side of the bin interval is closed. Default 'left' for all frequency offsets except:
'M', 'A', 'Q', 'BM', 'BA', 'BQ', and 'W', default 'right'.
labelThis parameter determines which bin edge to label bucket. Default 'left' for all frequency offsets except:
'Q', 'BM', 'BA', 'BQ', and 'W', default 'right'.
conventionThis parameter is the PeriodIndex, and it controls whether to use the start/end of the rule. The available options are: 'start', 'end', 's', or 'e'. Default is 'start'.
kindThis parameter is a timestamp/period and is for the PeriodIndex.
loffsetNot in use since v1.1.0. Add this to df.index after resample() has taken place.
baseNot in use since v1.1.0. Use 'offset' or 'origin' instead.
onIf a DataFrame, the datetime column to use instead of index for resampling.
levelA datetime level in a MultiIndex scenario to use for resampling.
originThe timestamp to adjust the grouping. The origin time-zone must match the index. If a string, one of the following: 'epoch', 'start', 'start_day', 'end', and 'end_day'
offsetThis parameter is the offset timedelta which adds to the origin.

Rivers Clothing is having a 3-hour blow-out sale for a new line they have introduced, scarfs. This example resamples the sales data and adds up the total number of scarf sales per hour.

df = pd.read_csv('rivers.csv', parse_dates=['date'], index_col=['date'])

result = df.resample('1H').sum()
  • Line [1] reads in a CSV file, parses the date column, and sets this column as the index. The output saves to df.
  • Line [2] outputs the DataFrame to the terminal.
  • Line [3] resamples the data by grouping the total scarf sales by the hour. The output saves to result.
  • Line [4] outputs the result to the terminal.



2022-01-27 08:17:00scarfred3
2022-01-27 08:23:00 scarfblue2
2022-01-27 08:47:00 scarfpink    1
2022-01-27 09:01:00 scarfblack   11
2022-01-27 09:28:00 scarfbrown    6
2022-01-27 09:51:00 scarfburgundy   15
2022-01-27 10:11:00 scarfblack   21
2022-01-27 10:13:00 scarfbrown   10
2022-01-27 10:22:00 scarfblack    9
2022-01-27 10:28:00 scarfnavy   30


2022-01-27 08:00:00    6
2022-01-27 09:00:0032
2022-01-27 10:00:00   70

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.