I have the following timeserie:
start = pd.to_datetime('2016-1-1')
end = pd.to_datetime('2016-1-15')
rng = pd.date_range(start, end, freq='2h')
df = pd.DataFrame({'timestamp': rng, 'values': np.random.randint(0,100,len(rng))})
df = df.set_index(['timestamp'])
I would like to drop the rows that are between those 2 timestamps:
start_remove = pd.to_datetime('2016-1-4')
end_remove = pd.to_datetime('2016-1-8')
How can I do that?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With