I\'m really struggling with the Pandas rolling_apply function
. I\'m trying to apply a filter to some time series data like below and make a new series for outliers.
It's less than a helpful message, but I believe the error is happening because rolling_apply
currently expects a like typed return array (may even have to be float). But, if you break your three operations (mean, std, outlier logic) into steps, it should work ok.
ts.name = 'value'
df = pd.DataFrame(ts)
def trimmed_apply(arr, alpha, f):
np.sort(arr)
n = len(arr)
k = int(round(n*float(alpha))/2)
return f(arr[k+1:n-k])
df['trimmed_mean'] = pd.rolling_apply(df['value'], window, trimmed_apply, args=(alpha, np.mean))
df['trimmed_std'] = pd.rolling_apply(df['value'], window, trimmed_apply, args=(alpha, np.std))
df['outlier'] = np.abs(arr - df['trimmed_mean']) < 3 * df['trimmed_std'] + gamma