从 pandas.Series 中选择局部最小值和最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27632218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting local minima and maxima from pandas.Series
提问by Stas Busygin
There is scipy.signal.argrelextremafunction that works with ndarray, but when I try to use it on pandas.Series, it returns an error. What's the right way to use it with pandas?
有一个scipy.signal.argrelextrema与ndarray一起使用的函数,但是当我尝试在 上使用它时pandas.Series,它返回一个错误。将它与Pandas一起使用的正确方法是什么?
import numpy as np
import pandas as pd
from scipy.signal import argrelextrema
s = pd.Series(randn(10), range(10))
s
argrelextrema(s, np.greater)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-f3812e58bbe4> in <module>()
4 s = pd.Series(randn(10), range(10))
5 s
----> 6 argrelextrema(s, np.greater)
/usr/lib/python2.7/dist-packages/scipy/signal/_peak_finding.pyc in argrelextrema(data, comparator, axis, order, mode)
222 """
223 results = _boolrelextrema(data, comparator,
--> 224 axis, order, mode)
225 return np.where(results)
226
/usr/lib/python2.7/dist-packages/scipy/signal/_peak_finding.pyc in _boolrelextrema(data, comparator, axis, order, mode)
60
61 results = np.ones(data.shape, dtype=bool)
---> 62 main = data.take(locs, axis=axis, mode=mode)
63 for shift in xrange(1, order + 1):
64 plus = data.take(locs + shift, axis=axis, mode=mode)
TypeError: take() got an unexpected keyword argument 'mode'
回答by nitin
You probably want to use it like so,
你可能想像这样使用它,
argrelextrema(s.values, np.greater)
You are currently using the complete pandas Series while argrelextrema expects an nd array. s.values provides you with the nd.array
您当前正在使用完整的Pandas系列,而 argrelextrema 需要一个 nd 数组。s.values 为您提供 nd.array
回答by Tomasz Gandor
While s.valuesstill works fine (Pandas 0.25), the recommended way is:
虽然s.values仍然可以正常工作(Pandas 0.25),但推荐的方法是:
argrelextrema(s.to_numpy(), np.greater)
# equivalent to:
argrelextrema(s.to_numpy(copy=False), np.greater)
While there is also a s.arrayproperty, using it here will fail with: TypeError: take() got an unexpected keyword argument 'axis'.
虽然也有一个s.array特性,在这里使用它会失败,并:TypeError: take() got an unexpected keyword argument 'axis'。
Note: copy=Falsemeans "don't force a copy", but it can still happen.
注意:copy=False表示“不要强制复制”,但它仍然可能发生。
回答by Mir.Emad
Late reply
As your code showed up, your array which has been read by pandas should be turning to numpy array. So just try to change the data frame to numpy array by np.array
迟到的回复 当你的代码显示出来时,你的已被Pandas读取的数组应该转向 numpy 数组。所以只需尝试将数据框更改为 numpy 数组 np.array
g = np.array(s) # g is new variable notation
argrelextrema(g, np.greater)
or in a different shape
或以不同的形状
g = np.array(s) # g is new variable notation
argrelextrema(g, lambda a,b: (a>b) | (a<b))

