Reputation: 6048
i have a time series that look like this
open high low close
date
1982-10-06 191.98 191.98 191.98 191.98 191.98
1982-10-07 195.59 195.59 195.59 195.59 195.59
1982-10-08 198.89 198.89 198.89 198.89 198.89
...
I find them minimum value over that time series for window = 20. That low can happen on any of the previous 19 days. id like to add to the dataframe either the date or number of days back that that low occurred. Can anyone help with this?
df['window_low'] = df['low'].rolling(min_periods=1, window=20, center=False).min()
df['low_date'] = ?
df['number_of_days_back'] = ?
Upvotes: 0
Views: 666
Reputation: 323266
You can do with idxmin
df = df.reset_index()
df['date'] = pd.to_datetime(df['date'])
id = df['low'].rolling(min_periods=1, window=20, center=False).apply(lambda x: x.idxmin())
df['low_date'] = df['date'].reindex(id).values
df['number_of_days_back'] = df['date'] - df['low_date']
Upvotes: 1