I am trying to get the last x days from my data. I use a DateTimeIndex as an index.
The following is a simplified way I use:
end_date = df.last_valid_index()
start_date = end_date - pd.tseries.offsets.DateOffset(30, "D")
df = df.loc[start_date:]
Sometimes, I get a KeyError
followed by the timestamp of the start date.
I think this occurs when that start_date
is not found on the DateTimeIndex
.