5

我正在处理表示向量(幅度和方向)的时间序列数据。我想重新采样我的数据并将describe函数用作how参数。

但是,该describe方法使用标准平均值,我想使用特殊函数来平均方向。因此,我describe基于以下实现实现了自己的方法pandas.Series.describe()

def directionAverage(x):
    result = np.arctan2(np.mean(np.sin(x)), np.mean(np.cos(x)))
    if result < 0:
        result += 2*np.pi
    return result

def directionDescribe(x):
    data = [directionAverage(x), x.std(), x.min(), x.quantile(0.25), x.median(), x.quantile(0.75), x.max()]
    names = ['mean', 'std', 'min', '25%', '50%', '75%', 'max']
    return Series(data, index=names)

问题是当我这样做时:

df['direction'].resample('10Min', how=directionDescribe)

我得到了这个异常(显示了最后几行):

  File "C:\Python26\lib\site-packages\pandas\core\generic.py", line 234, in resample
    return sampler.resample(self)
  File "C:\Python26\lib\site-packages\pandas\tseries\resample.py", line 83, in resample
    rs = self._resample_timestamps(obj)
  File "C:\Python26\lib\site-packages\pandas\tseries\resample.py", line 217, in _resample_timestamps
    result = grouped.aggregate(self._agg_method)
  File "C:\Python26\lib\site-packages\pandas\core\groupby.py", line 1626, in aggregate
    result = self._aggregate_generic(arg, *args, **kwargs)
  File "C:\Python26\lib\site-packages\pandas\core\groupby.py", line 1681, in _aggregate_generic
    return self._aggregate_item_by_item(func, *args, **kwargs)
  File "C:\Python26\lib\site-packages\pandas\core\groupby.py", line 1706, in _aggregate_item_by_item
    result[item] = colg.aggregate(func, *args, **kwargs)
  File "C:\Python26\lib\site-packages\pandas\core\groupby.py", line 1357, in aggregate
    result = self._aggregate_named(func_or_funcs, *args, **kwargs)
  File "C:\Python26\lib\site-packages\pandas\core\groupby.py", line 1441, in _aggregate_named
    raise Exception('Must produce aggregated value')

问题是:我如何实现我自己的describe功能,以便它可以使用resample

4

1 回答 1

3

您可以groupby将组作为时间单位,而不是重新采样。对于这个组,您可以应用您选择的函数,例如您的 directionAverage 函数。

请注意,我正在导入 TimeGrouper 函数以允许按时间间隔进行分组。

import pandas as pd
import numpy as np
from pandas.tseries.resample import TimeGrouper

#group  your data
new_data = df['direction'].groupby(TimeGrouper('10min'))
#apply your function to the grouped data
new_data.apply(directionDescribe)
于 2013-02-14T21:33:00.613 回答