<filename>notebook/pandas_time_series_resample_agg.py<gh_stars>100-1000
import pandas as pd

df = pd.read_csv('data/src/sample_date.csv', index_col=0, parse_dates=True)
print(df)
#             val_1  val_2
# date                    
# 2017-11-01     65     76
# 2017-11-07     26     66
# 2017-11-18     47     47
# 2017-11-27     20     38
# 2017-12-05     65     85
# 2017-12-12      4     29
# 2017-12-22     31     54
# 2017-12-29     21      8
# 2018-01-03     98     76
# 2018-01-08     48     64
# 2018-01-19     18     48
# 2018-01-23     86     70

print(type(df.index))
# <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

print(df.resample('W').sum())
#             val_1  val_2
# date                    
# 2017-11-05     65     76
# 2017-11-12     26     66
# 2017-11-19     47     47
# 2017-11-26      0      0
# 2017-12-03     20     38
# 2017-12-10     65     85
# 2017-12-17      4     29
# 2017-12-24     31     54
# 2017-12-31     21      8
# 2018-01-07     98     76
# 2018-01-14     48     64
# 2018-01-21     18     48
# 2018-01-28     86     70

print(df.resample('M').sum())
#             val_1  val_2
# date                    
# 2017-11-30    158    227
# 2017-12-31    121    176
# 2018-01-31    250    258

print(df.resample('Q').sum())
#             val_1  val_2
# date                    
# 2017-12-31    279    403
# 2018-03-31    250    258

print(df.resample('Y').sum())
#             val_1  val_2
# date                    
# 2017-12-31    279    403
# 2018-12-31    250    258

print(df.resample('10D').sum())
#             val_1  val_2
# date                    
# 2017-11-01     91    142
# 2017-11-11     47     47
# 2017-11-21     20     38
# 2017-12-01     65     85
# 2017-12-11      4     29
# 2017-12-21     52     62
# 2017-12-31    146    140
# 2018-01-10     18     48
# 2018-01-20     86     70

print(df.resample('3W').sum())
#             val_1  val_2
# date                    
# 2017-11-05     65     76
# 2017-11-26     73    113
# 2017-12-17     89    152
# 2018-01-07    150    138
# 2018-01-28    152    182

print(df.resample('Y').mean())
#              val_1   val_2
# date                      
# 2017-12-31  34.875  50.375
# 2018-12-31  62.500  64.500

print(df.resample('Y').agg(['sum', 'mean', 'max', 'min']))
#            val_1                 val_2                
#              sum    mean max min   sum    mean max min
# date                                                  
# 2017-12-31   279  34.875  65   4   403  50.375  85   8
# 2018-12-31   250  62.500  98  18   258  64.500  76  48

<filename>notebook/pandas_time_series_resample_agg.py<gh_stars>100-1000
import pandas as pd

df = pd.read_csv('data/src/sample_date.csv', index_col=0, parse_dates=True)
print(df)
#             val_1  val_2
# date                    
# 2017-11-01     65     76
# 2017-11-07     26     66
# 2017-11-18     47     47
# 2017-11-27     20     38
# 2017-12-05     65     85
# 2017-12-12      4     29
# 2017-12-22     31     54
# 2017-12-29     21      8
# 2018-01-03     98     76
# 2018-01-08     48     64
# 2018-01-19     18     48
# 2018-01-23     86     70

print(type(df.index))
# <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

print(df.resample('W').sum())
#             val_1  val_2
# date                    
# 2017-11-05     65     76
# 2017-11-12     26     66
# 2017-11-19     47     47
# 2017-11-26      0      0
# 2017-12-03     20     38
# 2017-12-10     65     85
# 2017-12-17      4     29
# 2017-12-24     31     54
# 2017-12-31     21      8
# 2018-01-07     98     76
# 2018-01-14     48     64
# 2018-01-21     18     48
# 2018-01-28     86     70

print(df.resample('M').sum())
#             val_1  val_2
# date                    
# 2017-11-30    158    227
# 2017-12-31    121    176
# 2018-01-31    250    258

print(df.resample('Q').sum())
#             val_1  val_2
# date                    
# 2017-12-31    279    403
# 2018-03-31    250    258

print(df.resample('Y').sum())
#             val_1  val_2
# date                    
# 2017-12-31    279    403
# 2018-12-31    250    258

print(df.resample('10D').sum())
#             val_1  val_2
# date                    
# 2017-11-01     91    142
# 2017-11-11     47     47
# 2017-11-21     20     38
# 2017-12-01     65     85
# 2017-12-11      4     29
# 2017-12-21     52     62
# 2017-12-31    146    140
# 2018-01-10     18     48
# 2018-01-20     86     70

print(df.resample('3W').sum())
#             val_1  val_2
# date                    
# 2017-11-05     65     76
# 2017-11-26     73    113
# 2017-12-17     89    152
# 2018-01-07    150    138
# 2018-01-28    152    182

print(df.resample('Y').mean())
#              val_1   val_2
# date                      
# 2017-12-31  34.875  50.375
# 2018-12-31  62.500  64.500

print(df.resample('Y').agg(['sum', 'mean', 'max', 'min']))
#            val_1                 val_2                
#              sum    mean max min   sum    mean max min
# date                                                  
# 2017-12-31   279  34.875  65   4   403  50.375  85   8
# 2018-12-31   250  62.500  98  18   258  64.500  76  48

#             val_1  val_2 # date # 2017-11-01     65     76 # 2017-11-07     26     66 # 2017-11-18     47     47 # 2017-11-27     20     38 # 2017-12-05     65     85 # 2017-12-12      4     29 # 2017-12-22     31     54 # 2017-12-29     21      8 # 2018-01-03     98     76 # 2018-01-08     48     64 # 2018-01-19     18     48 # 2018-01-23     86     70 # <class 'pandas.core.indexes.datetimes.DatetimeIndex'> #             val_1  val_2 # date # 2017-11-05     65     76 # 2017-11-12     26     66 # 2017-11-19     47     47 # 2017-11-26      0      0 # 2017-12-03     20     38 # 2017-12-10     65     85 # 2017-12-17      4     29 # 2017-12-24     31     54 # 2017-12-31     21      8 # 2018-01-07     98     76 # 2018-01-14     48     64 # 2018-01-21     18     48 # 2018-01-28     86     70 #             val_1  val_2 # date # 2017-11-30    158    227 # 2017-12-31    121    176 # 2018-01-31    250    258 #             val_1  val_2 # date # 2017-12-31    279    403 # 2018-03-31    250    258 #             val_1  val_2 # date # 2017-12-31    279    403 # 2018-12-31    250    258 #             val_1  val_2 # date # 2017-11-01     91    142 # 2017-11-11     47     47 # 2017-11-21     20     38 # 2017-12-01     65     85 # 2017-12-11      4     29 # 2017-12-21     52     62 # 2017-12-31    146    140 # 2018-01-10     18     48 # 2018-01-20     86     70 #             val_1  val_2 # date # 2017-11-05     65     76 # 2017-11-26     73    113 # 2017-12-17     89    152 # 2018-01-07    150    138 # 2018-01-28    152    182 #              val_1   val_2 # date # 2017-12-31  34.875  50.375 # 2018-12-31  62.500  64.500 #            val_1                 val_2 #              sum    mean max min   sum    mean max min # date # 2017-12-31   279  34.875  65   4   403  50.375  85   8 # 2018-12-31   250  62.500  98  18   258  64.500  76  48