Select 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. What is the correct 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' 
+5
source share
1 answer

You might want to use it like this

 argrelextrema(s.values, np.greater) 

You are currently using the full series of pandas, while argrelextrema expects the nth array. s.values ​​provides you with nd.array

+8
source

Source: https://habr.com/ru/post/1209814/


All Articles