繁体   English   中英

时间序列中的前向最小值和最大值

[英]forward min and max in time series

如何获取时间序列数据的最小值和最大值?
我知道滚动获取过去的值,但是,我想获取接下来 3 天的数据。

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?

使用 shift -3 方法给了我以下不起作用班次 -3

您可以将.rolling()与参数center=True一起使用,如下所示:

df['min'] = df['Close'].rolling(7, center=True).apply(lambda x: x[-3:].min())

使用.rolling()的参数center=True ,我们正在查看 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()获取 window x的最后 3 个条目(转发 3 个条目)的最小值。

结果:

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

编辑

如果要获取当前日期加上前 2 天的收盘价的最小值,并且在前几天没有NaN值,可以使用numpy.fmin()如下:

import numpy as np

df['min'] = np.fmin(np.fmin(df['Close'].values, df['Close'].shift(-1).values), df['Close'].shift(-2).values)

结果:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM