【问题标题】:Is this a bug in Pandas? FloatingPointError on ewm().std()这是熊猫中的错误吗? ewm().std() 上的 FloatingPointError
【发布时间】:2016-06-29 09:27:23
【问题描述】:

当我执行以下操作时,我得到一个 FloatingPointError。

import traceback
import warnings
import sys
import pandas as pd
import numpy as np
np.seterr(all='raise')
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
    traceback.print_stack()
    log = file if hasattr(file,'write') else sys.stderr
    log.write(warnings.formatwarning(message, category, filename, lineno, line))

warnings.showwarning = warn_with_traceback

pd.Series(np.random.randn(50)).pct_change().ewm(span=35, min_periods=35).std()

我收到以下错误:

FloatingPointErrorTraceback (most recent call last)
<ipython-input-2-3db1ff4816cf> in <module>()
----> 1 pd.Series(np.random.randn(50)).pct_change().ewm(span=35, min_periods=35).std()

/projects/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in std(self, bias, **kwargs)
   1285     def std(self, bias=False, **kwargs):
   1286         """exponential weighted moving stddev"""
-> 1287         return _zsqrt(self.var(bias=bias, **kwargs))
   1288 
   1289     vol = std

/projects/anaconda3/lib/python3.5/site-packages/pandas/core/window.py in _zsqrt(x)
   1487 def _zsqrt(x):
   1488     result = np.sqrt(x)
-> 1489     mask = x < 0
   1490 
   1491     from pandas import DataFrame

/projects/anaconda3/lib/python3.5/site-packages/pandas/core/ops.py in wrapper(self, other, axis)
    761                 other = np.asarray(other)
    762 
--> 763             res = na_op(values, other)
    764             if isscalar(res):
    765                 raise TypeError('Could not compare %s type with Series' %

/projects/anaconda3/lib/python3.5/site-packages/pandas/core/ops.py in na_op(x, y)
    714 
    715             try:
--> 716                 result = getattr(x, name)(y)
    717                 if result is NotImplemented:
    718                     raise TypeError("invalid type comparison")

FloatingPointError: invalid value encountered in less

为什么我会收到这些警告?这是熊猫中的错误吗?如何确定我的计算是正确的?

【问题讨论】:

    标签: python numpy pandas


    【解决方案1】:

    np.seterr(all='raise') 导致了这个问题。

    看看这个:pandas: FloatingPointError with np.seterr(all='raise') and missing data

    没有np.seterr(all='raise')pd.Series(np.random.randn(50)).pct_change().ewm(span=35, min_periods=35).std() 的输出是:

    0          NaN
    1          NaN
    2          NaN
    3          NaN
    4          NaN
    5          NaN
    6          NaN
    7          NaN
    8          NaN
    9          NaN
    10         NaN
    11         NaN
    12         NaN
    13         NaN
    14         NaN
    15         NaN
    16         NaN
    17         NaN
    18         NaN
    19         NaN
    20         NaN
    21         NaN
    22         NaN
    23         NaN
    24         NaN
    25         NaN
    26         NaN
    27         NaN
    28         NaN
    29         NaN
    30         NaN
    31         NaN
    32         NaN
    33         NaN
    34         NaN
    35    9.927862
    36    9.636469
    37    9.360825
    38    9.061078
    39    8.792150
    40    8.549347
    41    8.284875
    42    8.026411
    43    7.779943
    44    7.563633
    45    7.331553
    46    7.114672
    47    6.898717
    48    6.716513
    49    6.514064
    dtype: float64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2021-06-04
      • 2021-09-06
      • 2016-06-10
      • 2016-10-21
      相关资源
      最近更新 更多