I'm using the very convenient pandas dataframe last(..) function. In my use case I'm doing the following to get all the samples belonging to the last month of data (I'm working on daily data, used 2D frequency not to pollute the question):
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': np.random.rand(10)},
index=pd.date_range('2020-06-23', periods=10, freq='2D'))
print(df)
A
2020-06-23 0.893443
2020-06-25 0.256981
2020-06-27 0.544561
2020-06-29 0.712149
2020-07-01 0.500871
2020-07-03 0.948928
2020-07-05 0.816448
2020-07-07 0.939283
2020-07-09 0.760055
2020-07-11 0.394204
print(df.last('1M'))
A
2020-07-01 0.500871
2020-07-03 0.948928
2020-07-05 0.816448
2020-07-07 0.939283
2020-07-09 0.760055
2020-07-11 0.394204
I basically want the last result plus the last sample from the month before the last:
A
2020-06-29 0.712149
2020-07-01 0.500871
2020-07-03 0.948928
2020-07-05 0.816448
2020-07-07 0.939283
2020-07-09 0.760055
2020-07-11 0.394204
I need the last month plus a single sample before (due to computing returns from the first day of the last month need also the t-1 on the first date). I can work out a more convoluted way to do this but was wondering whether there would be an idiomatic and elegant way to do that rather than scrapping the nice last use-case.