Fun with Pandas `rolling_apply` and TypeError

前端 未结 1 1405
死守一世寂寞
死守一世寂寞 2021-01-26 12:06

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.

相关标签:
1条回答
  • 2021-01-26 12:48

    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
    
    0 讨论(0)
提交回复
热议问题