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)
Creating DataFrame and adding datetime index.
import pandas as pd
my_dict={
'my_date':['2022-06-01 00:00:00','2022-06-01 00:01:00',
'2022-06-01 00:02:00','2022-06-01 00:03:00',
'2022-06-01 00:04:00','2022-06-01 00:05:00'],
'value':[2,2,3,1,3,2]
}
df = pd.DataFrame(data=my_dict) # create dataframe
df['my_date']=pd.to_datetime(df['my_date']) # column to datetime dtype
df.set_index('my_date',inplace=True) # add index to column
print(df)
Output
value
my_date
2022-06-01 00:00:00 2
2022-06-01 00:01:00 2
2022-06-01 00:02:00 3
2022-06-01 00:03:00 1
2022-06-01 00:04:00 3
2022-06-01 00:05:00 2
By using to_datetime() we got datetime column my_date df = pd.DataFrame(data=my_dict)
df['my_date']=pd.to_datetime(df['my_date'])
df.set_index('my_date',inplace=True)
df=df.resample('2min').sum()
print(df)
Output is here
value
my_date
2022-06-01 00:00:00 4
2022-06-01 00:02:00 4
2022-06-01 00:04:00 5
Here the value rule='2min' can be changed with different frequencies . Here is the list of formats can be used to resample the DataFrame in different units.
df=df.resample(rule='2D5H2min10S').mean() # 2 days, 5 hours, 2 min 10 Seconds
closed: Which side is included ? Defalut value is None. If it is right then left side is not included.
df['c_left']=df.resample('2min',closed='left').mean()
Output
value c_left
my_date
2022-06-01 00:00:00 2 2.0
2022-06-01 00:01:00 2 NaN
2022-06-01 00:02:00 3 2.0
2022-06-01 00:03:00 1 NaN
2022-06-01 00:04:00 3 2.5
2022-06-01 00:05:00 2 NaN
df['c_right']=df.resample('2min',closed='right').mean()
Output
value c_right
my_date
2022-06-01 00:00:00 2 2.5
2022-06-01 00:01:00 2 NaN
2022-06-01 00:02:00 3 2.0
2022-06-01 00:03:00 1 NaN
2022-06-01 00:04:00 3 2.0
2022-06-01 00:05:00 2 NaN
As per the manualdf['l_left']=df.resample('2min',label='left').sum()
Output
value l_left
my_date
2022-06-01 00:00:00 2 4.0
2022-06-01 00:01:00 2 NaN
2022-06-01 00:02:00 3 4.0
2022-06-01 00:03:00 1 NaN
2022-06-01 00:04:00 3 5.0
2022-06-01 00:05:00 2 NaN
df['l_right']=df.resample('2min',label='right').sum()
Output
value l_right
my_date
2022-06-01 00:00:00 2 NaN
2022-06-01 00:01:00 2 NaN
2022-06-01 00:02:00 3 4.0
2022-06-01 00:03:00 1 NaN
2022-06-01 00:04:00 3 4.0
2022-06-01 00:05:00 2 NaN
on: In our examples above the date time column ( my_date ) is the index coloumn. If the resample is to be applied to any other column ( must be date-time ) then on can be used to provide column name. df['offset_3min']=df.resample(rule='2min',offset='3min').sum()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.