Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas: drop rows of a timeserie based on time range

Tags:

python

pandas

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?

like image 804
jim jarnac Avatar asked Jan 06 '17 19:01

jim jarnac


Video Answer


2 Answers

df = df.drop(pd.date_range('2018-01-01', '2018-02-01')), errors='ignore')
like image 120
Joe Heffer Avatar answered Nov 14 '22 05:11

Joe Heffer


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
like image 32
nipy Avatar answered Nov 14 '22 06:11

nipy