【发布时间】: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
为什么我会收到这些警告?这是熊猫中的错误吗?如何确定我的计算是正确的?
【问题讨论】: