1df.resample("W").agg(['min','max','mean','std'])
2
3# resample("3T") ==> 3 minutes
4# resample("30S") ==> 30 seconds
5# resample("1H") ==> 1 hour
6# resample("D") ==> day
7# resample("W") ==> week
8# resample("M") ==> month
9# resample("Y") ==> year
10# resample("Q") ==> quarter
11# Ex. 2018-01-01 ==> 2018-03-01 , 2018-06-01 , 2018-09-01 , 2018-12-01
12#####################################
13# .mean()
14# .max()
15# .min()
16# .sum()
17......
18# .agg(['min','max',...]) specified functions are applied for every column
1In [101]: df.resample('1H').agg({'openbid': 'first',
2 'highbid': 'max',
3 'lowbid': 'min',
4 'closebid': 'last'})
5Out[101]:
6 lowbid highbid closebid openbid
7ctime
82015-09-30 23:00:00 1.11687 1.11712 1.11708 1.117
9
1#We can also use custom functions and apply them when resampling using the .apply(method_name) method
2#This is an example used in a downsampling example
3def custom_resampler(arraylike):
4 return np.sum(arraylike) + 5
5data.resample('Q').apply(custom_resampler)
1>>> d = dict({'price': [10, 11, 9, 13, 14, 18, 17, 19],
2... 'volume': [50, 60, 40, 100, 50, 100, 40, 50]})
3>>> df = pd.DataFrame(d)
4>>> df['week_starting'] = pd.date_range('01/01/2018',
5... periods=8,
6... freq='W')
7>>> df
8 price volume week_starting
90 10 50 2018-01-07
101 11 60 2018-01-14
112 9 40 2018-01-21
123 13 100 2018-01-28
134 14 50 2018-02-04
145 18 100 2018-02-11
156 17 40 2018-02-18
167 19 50 2018-02-25
17# try below code when you want resample on datetime column to other all columns in dataframe
18>>> df.resample('M', on='week_starting').mean()
19 price volume
20week_starting
212018-01-31 10.75 62.5
222018-02-28 17.00 60.0
23
1B business day frequency
2C custom business day frequency (experimental)
3D calendar day frequency
4W weekly frequency
5M month end frequency
6SM semi-month end frequency (15th and end of month)
7BM business month end frequency
8CBM custom business month end frequency
9MS month start frequency
10SMS semi-month start frequency (1st and 15th)
11BMS business month start frequency
12CBMS custom business month start frequency
13Q quarter end frequency
14BQ business quarter endfrequency
15QS quarter start frequency
16BQS business quarter start frequency
17A year end frequency
18BA, BY business year end frequency
19AS, YS year start frequency
20BAS, BYS business year start frequency
21BH business hour frequency
22H hourly frequency
23T, min minutely frequency
24S secondly frequency
25L, ms milliseconds
26U, us microseconds
27N nanoseconds