如何在 numpy.ndarray 上获得滚动 (window = 3).max()?

tho*_*4ch 3 python numpy

我有一个 numpy.ndarray 如下。这是 talib.RSI 的输出。这是 numpy.ndarray 的类型。我想获得滚动列表(windows=3).max() 和滚动列表(window=3).min

怎么做?

[         nan          nan          nan          nan          nan
          nan          nan          nan          nan          nan
          nan          nan          nan          nan  56.50118203
  60.05461743  56.99068148  55.70899949  59.2299361   64.19044898
  60.62186599  53.96346826  44.06538636  52.04519976  51.32884016
  58.65240379  60.44789401  58.94743634  59.75308787  53.56534397
  54.22091468  47.22502341  51.5425848   50.0923126   49.80608264
  45.69087847  50.07778871  54.21701441  58.79268406  63.59307774
  66.08195696  65.49255218  65.11035657  68.47403716  70.70530564
  73.21955929  76.57474822  65.89852612  66.51497688  72.42658468
  73.80944844  69.56561001]
Run Code Online (Sandbox Code Playgroud)

Peq*_*que 6

如果您负担得起添加新的依赖项,我宁愿用Pandas来做。

import numpy
import pandas


x = numpy.array([0, 1, 2, 3, 4])
s = pandas.Series(x)

print(s.rolling(3).min())
print(s.rolling(3).max())
print(s.rolling(3).mean())
print(s.rolling(3).std())
Run Code Online (Sandbox Code Playgroud)

请注意,将 NumPy 数组转换为 Pandas 系列不会创建该数组的副本,因为 Pandas 在其系列内部使用 NumPy 数组。