Reputation: 75
How to get the min and max of a time series data?
I know that the rolling gets the values from the past, however, I want to get the data for the next 3 days.
pip install yfinance
# data
df=yf.download('tsla',start='2015-11-26',end='2021-4-28',interval='1d')
# df['min']=df['Close'].roling(3).min # this gives me the min of the past values is there a way of getting the min from the next three days?
using the shift -3 method gives me the following which does not work
Upvotes: 1
Views: 495
Reputation: 23227
You can use .rolling()
with parameter center=True
, as follows:
df['min'] = df['Close'].rolling(7, center=True).apply(lambda x: x[-3:].min())
With parameter center=True
for .rolling()
, we are looking at the cener (middle) of the window. Now with window size set at 7 and looking at the middle of the window, we are working at both a backward window of size 3 and a forward window of size 3. Hence, we can use x[-3:].min()
to get the min of the last 3 entries (forward 3 entries) of window x
.
Result:
print(df.head(10))
Open High Low Close Adj Close Volume min
Date
2015-11-25 44.268002 46.166000 44.076000 45.928001 45.928001 19954000 NaN
2015-11-27 46.212002 46.450001 45.402000 46.321999 46.321999 9747000 NaN
2015-11-30 46.358002 46.855999 45.816002 46.051998 46.051998 13299000 NaN
2015-12-01 46.212002 47.599998 46.209999 47.438000 47.438000 18670000 46.076000
2015-12-02 47.400002 47.720001 46.245998 46.397999 46.397999 14907500 46.076000
2015-12-03 47.096001 47.490002 46.000000 46.542000 46.542000 14698000 45.344002
2015-12-04 46.492001 46.653999 45.532001 46.076000 46.076000 12868000 44.903999
2015-12-07 45.540001 47.125999 45.230000 46.226002 46.226002 15721000 44.903999
2015-12-08 45.504002 45.759998 44.840000 45.344002 45.344002 13438000 43.403999
2015-12-09 45.340000 45.500000 44.144001 44.903999 44.903999 15289000 43.403999
If you want to get the min of closing prices of current date plus 2 days ahead and without NaN
values at the first few days, you can use numpy.fmin()
as follows:
import numpy as np
df['min'] = np.fmin(np.fmin(df['Close'].values, df['Close'].shift(-1).values), df['Close'].shift(-2).values)
Result:
print(df.head(10))
Open High Low Close Adj Close Volume min
Date
2015-11-25 44.268002 46.166000 44.076000 45.928001 45.928001 19954000 45.928001
2015-11-27 46.212002 46.450001 45.402000 46.321999 46.321999 9747000 46.051998
2015-11-30 46.358002 46.855999 45.816002 46.051998 46.051998 13299000 46.051998
2015-12-01 46.212002 47.599998 46.209999 47.438000 47.438000 18670000 46.397999
2015-12-02 47.400002 47.720001 46.245998 46.397999 46.397999 14907500 46.076000
2015-12-03 47.096001 47.490002 46.000000 46.542000 46.542000 14698000 46.076000
2015-12-04 46.492001 46.653999 45.532001 46.076000 46.076000 12868000 45.344002
2015-12-07 45.540001 47.125999 45.230000 46.226002 46.226002 15721000 44.903999
2015-12-08 45.504002 45.759998 44.840000 45.344002 45.344002 13438000 44.903999
2015-12-09 45.340000 45.500000 44.144001 44.903999 44.903999 15289000 43.403999
Upvotes: 1