Home > Mobile >  Do not include lower border on .loc[] datetimeIndex filtering
Do not include lower border on .loc[] datetimeIndex filtering

Time:10-02

I have following code:

df.loc[f"{lower_border_date}":f"{upper_border_date}"]

So I want to filter my dataframe on the datetimeIndex but I don't want to include the lower_border_date matches in the result. Anyone has an idea how to deal with this? Thanks!

CodePudding user response:

Few options:

  1. Filter based on index:
df.loc[(df.index > lower_border_date) & (df.index <= upper_border_date)]

Directly filtering and taking all indices where index is greater then lower_border_date and less or equal to upper_border_date

  1. use .drop. Less nice in my opinion, but should work:
df.loc[f"{lower_border_date}":f"{upper_border_date}"].drop(index=lower_border_date)
  1. You can also use pd.date_range, filter the first date, and then use .loc. It's also a weird workaround, but that is an option
  • Related