有一个scipy.signal.argrelextrema
函数与ndarray
一起工作,但是当我试图在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'
发布于 2014-12-24 07:23:14
你可能会像这样使用它,
argrelextrema(s.values, np.greater)
您目前正在使用完整的熊猫系列,而ARRER极致期待一个和数组。s.values为您提供了nd.array
发布于 2019-11-28 01:42:41
尽管s.values
仍然运行良好(Pandas0.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
的意思是“不要强制复制”,但它仍然可能发生。
发布于 2020-05-26 20:50:34
当你的代码出现的时候,你的数组已经被熊猫读到了,所以你的数组应该变成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))
https://stackoverflow.com/questions/27632218
复制相似问题