The Pandas dataframe.resample()
function is mainly used for time series data.
Time series — it is a series of data points indexed (or listed or plotted) in order of time. Most often the time series — it is a sequence taken at successive equal intervals of time. It is a convenient technique for frequency conversion and resampling of time series. The object must have a date / time type index (DatetimeIndex, PeriodIndex, or TimedeltaIndex) or pass date / time values to the on or level keyword.
Syntax: DataFrame. resample (rule, how = None, axis = 0, fill_method = None, closed = None, label = None, convention = 'start', kind = None, loffset = None, limit = None, base = 0, on = None, level = None)
Parameters:
rule: the offset string or object representing target conversion
axis: int, optional, default 0
closed: {'right', 'left'}
label: {'right', 'left'}
convention: For PeriodIndex only, controls whether to use the start or end of rule
loffset: Adjust the resampled time labels
base: For frequencies that evenly subdivide 1 day, the “origin” of the aggregated intervals. For example, for '5min' frequency, base could range from 0 through 4. Defaults to 0.
on: For a DataFrame, column to use instead of index for resampling. Column must be datetimelike.
level: For a MultiIndex, level (name or number) to use for resampling. Level must be datetimelike.
Resampling generates a unique distribution of the sample based on the actual data. We can apply different frequencies to resample our time series data. This is a very important method in the field of analytics.
Most commonly used time series frequencies —
W: weekly frequency
M: month ending frequency
SM: semester ending frequency (15 and end of month)
Q: quarterend frequency
There are many other types of time series available. Let's see how to apply the frequency of these time series to the data and change it.
To link to the CSV file used in the code, click here
This is Apple stock price data for a period of 1 year from (131117) to (1311 18)
Example # 1: recalculation of data on a monthly frequency


Output:
Example # 2: data recalculation at weekly frequency
# Resampling time series data based on weekly frequency # we apply it to the opening price of the stock & # 39; W & # 39; indicates a week 
Output:
Example # 3: quarterly frequency conversion
# import pandas as pd
import
pandas as pd
# We know resampling works with time series
# only data, so convert our "date" column to index
# index_col = "date", creates a column & quot; date & quot;
df
=
pd .read_csv (
"apple.csv"
, parse_dates
=
[
"date"
], index_col
=
" date "
)
# Resampling time series data
# based on quarterly frequency
# & # 39; Q & # 39; stands for a quarter
Quarterly_resampled_data
=
df.
open
. resample (
'Q'
). mean ()
# average opening price of each quarter
# within 1 year.
Quarterly_resampled_data
Output: