简体   繁体   English

从 pandas.Series 中选择局部最小值和最大值

[英]Selecting local minima and maxima from pandas.Series

There is a scipy.signal.argrelextrema function that works with ndarray , but when I try to use it on pandas.Series , it returns an error.有一个适用于ndarrayscipy.signal.argrelextrema函数,但是当我尝试在pandas.Series上使用它时,它返回一个错误。 What's the right way to use it with 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'

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.您当前正在使用完整的熊猫系列,而 argrelextrema 需要一个 nd 数组。 s.values provides you with the nd.array s.values 为您提供 nd.array

Even though s.values still works fine (Pandas 0.25), the recommended way is now:即使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 an s.array property, 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=False means "don't force a copy", but it can still happen.注意: copy=False表示“不强制复制”,但它仍然可能发生。

Late reply As your code showed up, your array which has been read by pandas should be turning to numpy array.迟到的回复当你的代码显示出来时,你的被熊猫读取的数组应该转向 numpy 数组。 So just try to change the data frame to numpy array by np.array所以只需尝试通过np.array将数据框更改为 numpy 数组

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))

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

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