I am working with stock price data and would like to get resample()
to return every 2nd row rather than every 2nd business day (resample('2B')
). The obstacle is any holiday that lands on a weekday. See below, MLK Day is Monday, Jan 15, 2018:
import pandas as pd
data = '''\
date,price
2018-01-08,88.28
2018-01-09,88.22
2018-01-10,87.82
2018-01-11,88.08
2018-01-12,89.6
2018-01-16,88.35
2018-01-17,90.14
2018-01-18,90.1
2018-01-19,90.0
2018-01-22,91.61
2018-01-23,91.9
2018-01-24,91.82
2018-01-25,92.33
2018-01-26,94.06'''
fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, parse_dates=['date'], index_col=[0])
df_resample = df.resample('2B').min()
print(df_resample)
Output:
price
2018-01-08 88.22
2018-01-10 87.82
2018-01-12 89.60
2018-01-16 88.35
2018-01-18 90.00
2018-01-22 91.61
2018-01-24 91.82
2018-01-26 94.06
I'd like the resample to jump from 1/12 to 1/17. I know that I can use df['price'].loc[::2]
to deliver df.resample('2B').last()
but I need to use min()
, max()
and sum()
as well.
Thanks.
Expected Output: