1

I need to select the rows +- 1sec from specific lines (marked as to_pick)

I can do it with loops, but I looking for more elegant way

import pandas as pd
import numpy as np
N = 1000
tidx = pd.date_range('2019-07-01 09:30:00', periods=N, freq='S')
np.random.seed(3)
data = np.random.randn(N)
ts = tidx + pd.to_timedelta(pd.np.random.randn(N), unit='s')

df = pd.DataFrame({'Time': ts, 'to_pick': pd.np.random.randn(N) > 0.98, 'start': ts - pd.to_timedelta(1, 's'), 'end': ts + pd.to_timedelta(1, 's')})
df.loc[~df['to_pick'], 'start'] = pd.np.nan
df.loc[~df['to_pick'], 'end'] = pd.np.nan

I am looking for something like

 df['Time'].between(df['start', df['end'])

that will work to combine the conditions

user1889297
  • 464
  • 5
  • 13

1 Answers1

1

If I understand you correctly, you want to select rows whose Time are between start and end when to_pick = true:

start = df.query('to_pick')['start'].values
end = df.query('to_pick')['end'].values
t = df['Time'].values[:, None]

df.loc[np.any((start <= t) & (t <= end), axis=1)]
Code Different
  • 90,614
  • 16
  • 144
  • 163