Reputation: 1327
I have a dataframe that has one variable and an equally spaced date time index (the index is at 1 second granularity).Say there are 1000 samples overall:
dates = pd.date_range('2015-1-1', periods=(1000) ,freq='S')
df = pd.DataFrame(np.random.rand(1000),index=dates, columns=['X'])
X
2015-01-01 00:00:00 2.2
2015-01-01 00:00:01 2.5
2015-01-01 00:00:02 1.2
2015-01-01 00:00:03 1.5
2015-01-01 00:00:04 3.7
2015-01-01 00:00:05 3.1
etc
I want to determine the start of the rolling window (of a given length) that contains the largest set that contains the smallest valued numbers within the given window size.
So in the example above, if the window was of size two, the answer would be:
start_index = 2015-01-01 00:00:02
end_index = 2015-01-01 00:00:03
I've tried to read the pandas
document to see if there is a rolling computation that can help, but no luck! Thanks.
Upvotes: 1
Views: 182
Reputation: 78011
You just need to do rolling_sum
over df['X'] == df['X'].min()
. Then the end of window is simply:
>>> ts = df['X'] == df['X'].min()
>>> pd.rolling_sum(ts, win_size).argmax()
and in order to obtain start of the window you may either shift the end of window or alternatively shift the series:
>>> pd.rolling_sum(ts.shift(-win_size), win_size).argmax()
Upvotes: 1