繁体   English   中英

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

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

有一个适用于ndarrayscipy.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'

你可能想像这样使用它,

argrelextrema(s.values, np.greater)

您当前正在使用完整的熊猫系列,而 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() got an unexpected keyword argument 'axis'

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

迟到的回复当你的代码显示出来时,你的被熊猫读取的数组应该转向 numpy 数组。 所以只需尝试通过np.array将数据框更改为 numpy 数组

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

暂无
暂无

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

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