Reputation: 6548
I have a DataFrame with dates as indices:
VL
2018-02-05 101.56093
2018-12-31 95.87728
2019-01-04 96.29820
2019-01-11 97.23475
2019-01-18 98.39828
2019-01-25 98.66896
2019-01-31 99.12407
2019-02-01 99.13224
2019-02-08 99.06382
2019-02-15 99.79966
I need to filter the rows so that, for each row with date D
, keep it if the row with D-7
exists in the DataFrame.
Example:
2019-02-15
would remain, because 2019-02-08
is present
2019-01-31
would be filtered as 2019-01-24
is not present.
I've implemented this already using a loop but I'm wondering if there is a more pandas oriented way of doing this kind of filtering.
Upvotes: 0
Views: 32
Reputation: 153500
IIUC, you can use pd.Timedelta
and isin
:
df[(df['date'] - pd.Timedelta(days=7)).isin(df['date'])]
Output:
date VL
3 2019-01-11 97.23475
4 2019-01-18 98.39828
5 2019-01-25 98.66896
7 2019-02-01 99.13224
8 2019-02-08 99.06382
9 2019-02-15 99.79966
If date is in the index use this:
df[(df.index - pd.Timedelta(days=7)).isin(df.index)]
Output:
VL
date
2019-01-11 97.23475
2019-01-18 98.39828
2019-01-25 98.66896
2019-02-01 99.13224
2019-02-08 99.06382
2019-02-15 99.79966
Upvotes: 2