Python 从熊猫系列中选择局部极小值和极大值

Python 从熊猫系列中选择局部极小值和极大值,python,pandas,Python,Pandas,有一个与ndarray一起工作的scipy.signal.argrelextrema函数,但是当我尝试在pandas.Series上使用它时,它返回一个错误。对熊猫来说,正确的使用方法是什么 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) ---------

有一个与
ndarray
一起工作的
scipy.signal.argrelextrema
函数,但是当我尝试在
pandas.Series
上使用它时,它返回一个错误。对熊猫来说,正确的使用方法是什么

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'
将numpy导入为np
作为pd进口熊猫
从scipy.signal导入argrelextrema
s=pd系列(随机数(10),范围(10))
s
ArgreExtrema(s,np.更大)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
4 s=局部放电系列(随机数(10),量程(10))
5秒
---->6 argrelextrema(s,np.更大)
/argrelextrema中的usr/lib/python2.7/dist-packages/scipy/signal//u peak\u finding.pyc(数据、比较器、轴、顺序、模式)
222     """
223结果=_boolrelextrema(数据、比较器、,
-->224轴、顺序、模式)
225返回np.where(结果)
226
/usr/lib/python2.7/dist-packages/scipy/signal//u peak\u finding.pyc in\u boolrelextrema(数据、比较器、轴、顺序、模式)
60
61结果=np.ones(data.shape,dtype=bool)
--->62主=数据。取数(locs,轴=轴,模式=模式)
63用于X档换档(1,订单+1):
64加=数据。取数(locs+移位,轴=轴,模式=模式)
TypeError:take()获得意外的关键字参数“mode”

您可能想这样使用它

argrelextrema(s.values, np.greater)

您当前正在使用完整的pandas系列,而argrelextrema需要nd数组。s.values为您提供nd.array

s.values
仍然可以正常工作(pandas 0.25),建议的方法是:

argrelextrema(s.to_numpy(), np.greater)
# equivalent to:
argrelextrema(s.to_numpy(copy=False), np.greater)
虽然还有一个
s.array
属性,但在此处使用它将失败:
TypeError:take()获得了一个意外的关键字参数“axis”

注意:
copy=False
表示“不要强制复制”,但仍有可能发生。

延迟回复 正如您的代码显示的那样,pandas读取的数组应该变成numpy数组。因此,只需尝试通过
np.array

g = np.array(s) # g is new variable notation
argrelextrema(g, np.greater)

或者以不同的形状

g = np.array(s) # g is new variable notation
argrelextrema(g, lambda a,b: (a>b) | (a<b))
g=np.array(s)#g是新的变量表示法

argrelextrema(g,lambda a,b:(a>b)|(a您可能希望转换数据: