gpt4 book ai didi

python - 如何对包含 dtype Decimal 值的 python pandas 时间序列进行重采样?

转载 作者:太空狗 更新时间:2023-10-29 23:55:25 25 4
gpt4 key购买 nike

我有一个 pandas Series 对象,其中填充了 dtype Decimal 的十进制数。我想使用新的 pandas 0.8 函数对十进制时间序列进行重新采样,如下所示:

resampled = ts.resample('D', how = 'mean')

尝试此操作时,出现“GroupByError:没有要聚合的数字类型”错误。我假设问题是 np.mean 在内部用于对值重新采样,而 np.mean 期望 float 而不是小数。

感谢这个论坛的帮助,我设法使用 groupBy 和 apply 函数解决了一个类似的问题,但我也很想使用很酷的 resample 函数。

How use the mean method on a pandas TimeSeries with Decimal type values?

知道如何解决这个问题吗?

这是创建错误的完整 ipython session :

In [37]: from decimal import Decimal

In [38]: from pandas import *

In [39]: rng = date_range('1.1.2012',periods=48, freq='H')

In [40]: rnd = np.random.randn(len(rng))

In [41]: rnd_dec = [Decimal(x) for x in rnd]

In [42]: ts = Series(rnd_dec, index=rng)

In [43]: ts[0:3]

Out[43]:
2012-01-01 00:00:00 -0.1020591335576267189022559023214853368699550628
2012-01-01 01:00:00 0.99245713975437366283216533702216111123561859130
2012-01-01 02:00:00 1.80080710727195758558139004890108481049537658691
Freq: H

In [44]: type(ts[0])
Out[44]: decimal.Decimal

In [45]: ts.resample('D', how = 'mean')
---------------------------------------------------------------------------
GroupByError Traceback (most recent call last)
C:\Users\THM\Documents\Python\<ipython-input-45-09c898403ddd> in <module>()
----> 1 ts.resample('D', how = 'mean')

C:\Python27\lib\site-packages\pandas\core\generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, l
imit, base)
187 fill_method=fill_method, convention=convention,
188 limit=limit, base=base)
--> 189 return sampler.resample(self)
190
191 def first(self, offset):

C:\Python27\lib\site-packages\pandas\tseries\resample.pyc in resample(self, obj)
65
66 if isinstance(axis, DatetimeIndex):
---> 67 rs = self._resample_timestamps(obj)
68 elif isinstance(axis, PeriodIndex):
69 offset = to_offset(self.freq)

C:\Python27\lib\site-packages\pandas\tseries\resample.pyc in _resample_timestamps(self, obj)
184 if len(grouper.binlabels) < len(axlabels) or self.how is not None:
185 grouped = obj.groupby(grouper, axis=self.axis)
--> 186 result = grouped.aggregate(self._agg_method)
187 else:
188 # upsampling shortcut


C:\Python27\lib\site-packages\pandas\core\groupby.pyc in aggregate(self, func_or_funcs, *args, **kwargs)
1215 """
1216 if isinstance(func_or_funcs, basestring):
-> 1217 return getattr(self, func_or_funcs)(*args, **kwargs)
1218
1219 if hasattr(func_or_funcs,'__iter__'):

C:\Python27\lib\site-packages\pandas\core\groupby.pyc in mean(self)
290 """
291 try:
--> 292 return self._cython_agg_general('mean')
293 except GroupByError:
294 raise

C:\Python27\lib\site-packages\pandas\core\groupby.pyc in _cython_agg_general(self, how)
376
377 if len(output) == 0:
--> 378 raise GroupByError('No numeric types to aggregate')
379
380 return self._wrap_aggregated_output(output, names)

GroupByError: No numeric types to aggregate

感谢任何帮助。谢谢,托马斯

最佳答案

我自己找到了答案。可以为 resample 的“how”参数提供一个函数:

f = lambda x: Decimal(np.mean(x))
ts.resample('D', how = f)

关于python - 如何对包含 dtype Decimal 值的 python pandas 时间序列进行重采样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11762815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com