RK1
RK1

Reputation: 2532

pandas filter on DatetimeIndex by excluding date range

I currently have a pandas.DataFrame which has a pandas.DatetimeIndex and a set of values.

I would like to exclude all the dates in a given pandas.date_range from this pandas.DataFrame.

Example code:

dates = pd.date_range(start='04/01/2012', end='04/01/2019', freq='MS')
df = pd.DataFrame(data=[100]*len(dates),index=dates,columns=["val"])

exclusion_dates = pd.date_range(start='04/01/2012', end='04/01/2019', freq=pd.offsets.DateOffset(months=12))

My attempt:

df.loc[~exclusion_dates,:]

Ideally this would lead to df containing all dates except for 1st April YYYY

However, this leads to the below error:

TypeError: bad operand type for unary ~: 'DatetimeIndex'

I looked at the below thread, however could not find anything: Filtering Pandas DataFrames on dates

Upvotes: 7

Views: 5792

Answers (1)

andrew_reece
andrew_reece

Reputation: 21264

Use isin():

df.loc[~df.index.isin(exclusion_dates)]

            val
2012-02-01  100
2012-03-01  100   <-- April excluded
2012-05-01  100
2012-06-01  100
2012-07-01  100
2012-08-01  100
2012-09-01  100
2012-10-01  100
2012-11-01  100
2012-12-01  100
2013-01-01  100
2013-02-01  100
2013-03-01  100   <-- April excluded
2013-05-01  100
...

Note: The default format treats your date strings as mm/dd/yyyy. So use:

pd.date_range(start='04/01/2012', end='04/01/2019', ...)

Upvotes: 4

Related Questions