In [1]:
import pandas as pd

In [2]:
df = pd.read_csv('data/src/aapl_2015_2019.csv', index_col=0, parse_dates=True)['2017']
print(df)


              open      high      low   close    volume
2017-01-03  115.80  116.3300  114.760  116.15  28781865
2017-01-04  115.85  116.5100  115.750  116.02  21118116
2017-01-05  115.92  116.8642  115.810  116.61  22193587
2017-01-06  116.78  118.1600  116.470  117.91  31751900
2017-01-09  117.95  119.4300  117.940  118.99  33561948
...            ...       ...      ...     ...       ...
2017-12-22  174.68  175.4240  174.500  175.01  16052615
2017-12-26  170.80  171.4700  169.679  170.57  32968167
2017-12-27  170.10  170.7800  169.710  170.60  21672062
2017-12-28  171.00  171.8500  170.480  171.08  15997739
2017-12-29  170.52  170.5900  169.220  169.23  25643711

[251 rows x 5 columns]

In [3]:
d_ohlc = {'open': 'first',
          'high': 'max',
          'low': 'min',
          'close': 'last'}

In [4]:
print(df.resample('MS').agg(d_ohlc))


              open      high     low   close
2017-01-01  115.80  122.4400  114.76  121.35
2017-02-01  127.03  137.4800  127.01  136.99
2017-03-01  137.89  144.5000  137.05  143.66
2017-04-01  143.71  145.4600  140.06  143.65
2017-05-01  145.10  156.6500  144.27  152.76
2017-06-01  153.17  155.9800  142.20  144.02
2017-07-01  144.88  153.9900  142.41  148.73
2017-08-01  149.10  164.5200  148.41  164.00
2017-09-01  164.80  164.9400  149.16  154.12
2017-10-01  154.26  169.6499  152.46  169.04
2017-11-01  169.87  176.2400  165.28  171.85
2017-12-01  169.95  177.2000  166.46  169.23

In [5]:
print(df.resample('QS').agg(d_ohlc))


              open    high     low   close
2017-01-01  115.80  144.50  114.76  143.66
2017-04-01  143.71  156.65  140.06  144.02
2017-07-01  144.88  164.94  142.41  154.12
2017-10-01  154.26  177.20  152.46  169.23

In [6]:
print(df.resample('2W-MON', closed='left', label='left').agg(d_ohlc))


               open      high       low   close
2017-01-02  115.800  119.9300  114.7600  119.04
2017-01-16  118.340  122.4400  118.2200  121.95
2017-01-30  120.930  132.9400  120.6200  132.12
2017-02-13  133.080  137.4800  132.7500  136.66
2017-02-27  137.140  140.2786  136.2800  139.14
2017-03-13  138.850  142.8000  138.8200  140.64
2017-03-27  139.390  145.4600  138.6200  143.34
2017-04-10  143.600  143.8792  140.0600  142.27
2017-04-24  143.500  148.9800  143.1800  148.96
2017-05-08  149.030  156.6500  149.0300  153.06
2017-05-22  154.000  155.4500  152.2200  155.45
2017-06-05  154.340  155.9800  142.2000  142.27
2017-06-19  143.660  148.2800  142.2800  144.02
2017-07-03  144.880  149.3300  142.4100  149.04
2017-07-17  148.820  153.9900  147.3000  149.50
2017-07-31  149.900  161.8300  148.1300  157.48
2017-08-14  159.320  162.5100  155.1101  159.86
2017-08-28  160.140  164.9400  158.5300  158.63
2017-09-11  160.500  163.9600  150.5600  151.89
2017-09-25  149.990  155.4900  149.1600  155.30
2017-10-09  155.810  160.8700  155.0200  156.25
2017-10-23  156.890  174.2600  155.2700  172.50
2017-11-06  172.365  176.2400  168.3800  170.15
2017-11-20  170.290  175.5000  167.1600  171.05
2017-12-04  172.480  174.1700  166.4600  173.97
2017-12-18  174.880  177.2000  169.2200  169.23

In [7]:
d_ohlcv = {'open': 'first',
           'high': 'max',
           'low': 'min',
           'close': 'last',
           'volume': 'sum'}

In [8]:
print(df.resample('MS').agg(d_ohlcv))


              open      high     low   close     volume
2017-01-01  115.80  122.4400  114.76  121.35  563331160
2017-02-01  127.03  137.4800  127.01  136.99  574968547
2017-03-01  137.89  144.5000  137.05  143.66  562091214
2017-04-01  143.71  145.4600  140.06  143.65  371280180
2017-05-01  145.10  156.6500  144.27  152.76  635292989
2017-06-01  153.17  155.9800  142.20  144.02  664986406
2017-07-01  144.88  153.9900  142.41  148.73  411377229
2017-08-01  149.10  164.5200  148.41  164.00  638221161
2017-09-01  164.80  164.9400  149.16  154.12  669594016
2017-10-01  154.26  169.6499  152.46  169.04  496135305
2017-11-01  169.87  176.2400  165.28  171.85  581876496
2017-12-01  169.95  177.2000  166.46  169.23  518560008