Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用十进制值计算pandas系列上的pct_更改会引发不支持的操作数类型_Python_Pandas - Fatal编程技术网

Python 使用十进制值计算pandas系列上的pct_更改会引发不支持的操作数类型

Python 使用十进制值计算pandas系列上的pct_更改会引发不支持的操作数类型,python,pandas,Python,Pandas,我有一个用十进制值(本例中为随机值)填充的序列 当我尝试应用pct_更改时: In [242]: s.pct_change() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-243-5c16731f0315

我有一个用十进制值(本例中为随机值)填充的序列

当我尝试应用pct_更改时:

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'

但是我希望在用浮点来牺牲精度之前能找到一些解决方案。

你可以做
ts.diff().div(ts.shift(1))
使用十进制根本没有效率;除非你真的需要固定精度,否则你应该使用floats@behzad.nouri该死很简单,而且很有效。。。pct_变化没有某种形式的影响似乎真的很奇怪option@Jeff我想与财务数据,所以我不知道我是否应该相信浮动。。。我理解小数的效率低下,但数据量不足以产生巨大的差异。@MakisTsantekidis我通常用一个整数64米尔斯(1/1000美元)来赚钱。
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'
s = s.astype(float)