簡體   English   中英

這是熊貓中的錯誤嗎? ewm()。std()上的FloatingPointError

[英]Is this a bug in Pandas? FloatingPointError on ewm().std()

當我執行以下命令時,我得到一個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

為什么會收到這些警告? 這是熊貓中的錯誤嗎? 如何確定我的計算正確?

np.seterr(all='raise')引起了問題。

檢查一下: 熊貓:帶有np.seterr(all ='raise')和缺少數據的FloatingPointError

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM