在pandas上计算pct_change具有十进制值的系列会引发不支持的操作数类型

时间:2014-06-29 23:28:11

标签: python pandas

我有一个系列,我用十进制值填充(在这种情况下为随机值)

In [240]:  
s = pandas.Series([Decimal("423234.53463"),Decimal("4234.53463"),Decimal("4223434.53463"),Decimal("42654234.53463")])

In [241]: s
Out[241]:
0      423234.53463
1        4234.53463
2     4223434.53463
3    42654234.53463
dtype: object

当我尝试应用pct_change时:

In [242]: s.pct_change()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-243-5c16731f0315> in <module>()
----> 1 s.pct_change()

/Users/mike/.virtualenvs/dev/lib/python2.7/site-packages/pandas/core/generic.pyc in pct_change(self, periods, fill_method, limit, freq, **kwds)
   3665
   3666         rs = (data.div(data.shift(periods=periods, freq=freq,
-> 3667                                   axis=axis, **kwds)) - 1)
   3668         if freq is None:
   3669             mask = com.isnull(_values_from_object(self))

/Users/mike/.virtualenvs/dev/lib/python2.7/site-packages/pandas/core/ops.pyc in flex_wrapper(self, other, level, fill_value, axis)
    676         self._get_axis_number(axis)
    677         if isinstance(other, pd.Series):
--> 678             return self._binop(other, op, level=level, fill_value=fill_value)
    679         elif isinstance(other, (pa.Array, pd.Series, list, tuple)):
    680             if len(other) != len(self):

/Users/mike/.virtualenvs/dev/lib/python2.7/site-packages/pandas/core/series.pyc in _binop(self, other, func, level, fill_value)
   1452             other_vals[other_mask & mask] = fill_value
   1453
-> 1454         result = func(this_vals, other_vals)
   1455         name = _maybe_match_name(self, other)
   1456         return self._constructor(result, index=new_index).__finalize__(self)

TypeError: unsupported operand type(s) for /: 'Decimal' and 'float'

this issue on pandas github

外,初始搜索未返回任何类似问题

我考虑过使用

s = s.astype(float)

但我想在使用float牺牲精度之前先看一些解决方案。

0 个答案:

没有答案