Python Pandas: drop rows of a timeserie based on time range
df = df.drop(pd.date_range('2018-01-01', '2018-02-01')), errors='ignore')
Another one to try. Exclude the dates in the date_range
:
Edit: Added frequency to date_range
. This is now the same as original data.
dropThis = pd.date_range(start_remove,end_remove,freq='2h')
df[~df.index.isin(dropThis)]
We can see the rows are now dropped.
len(df)
169
len(df[~pd.to_datetime(df.index).isin(dropThis)])
120
using query
df.query('index < @start_remove or index > @end_remove')
using loc
df.loc[(df.index < start_remove) | (df.index > end_remove)]
using date slicing
This includes the end points
pd.concat([df[:start_remove], df[end_remove:]])
And without the end points
pd.concat([df[:start_remove], df[end_remove:]]).drop([start_remove, end_remove])