熊猫浮点错误

Posted

技术标签:

【中文标题】熊猫浮点错误【英文标题】:Pandas FloatingPoint Error 【发布时间】:2012-05-27 03:01:06 【问题描述】:

我在 pandas 的一个简单时间序列中遇到浮点错误。我正在尝试进行移位操作...但这也发生在像 rolling_mean 这样的窗口函数中。

编辑:有关更多信息...我昨天在错误发生之前尝试从源代码实际构建它。我不确定在构建尝试之前是否会发生错误,因为我从来没有搞砸过这些功能。

EDIT2:我以为我已经解决了这个问题,但是当我在 python 中运行它时它可以工作,但是当它在 ipython 中时我得到了错误。

EDIT3:Numpy 1.7.0、iPython 0.13、pandas 0.7.3

In [35]: ts = Series(np.arange(12), index=DateRange('1/1/2000', periods=12, freq='T'))

In [36]: ts.shift(0)
Out[36]: 
2000-01-03     0
2000-01-04     1
2000-01-05     2
2000-01-06     3
2000-01-07     4
2000-01-10     5
2000-01-11     6
2000-01-12     7
2000-01-13     8
2000-01-14     9
2000-01-17    10
2000-01-18    11

In [37]: ts.shift(1)
Out[37]: ---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)
/Users/trenthauck/Repository/work/SQS/analysis/campaign/tv2/data/<ipython-input-37-2b7cec97d440> in <module>()
----> 1 ts.shift(1)

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/displayhook.pyc in __call__(self, result)
    236             self.start_displayhook()
    237             self.write_output_prompt()
--> 238             format_dict = self.compute_format_data(result)
    239             self.write_format_data(format_dict)
    240             self.update_user_ns(result)

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/displayhook.pyc in compute_format_data(self, result)
    148             MIME type representation of the object.
    149         """
--> 150         return self.shell.display_formatter.format(result)
    151 
    152     def write_format_data(self, format_dict):

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/formatters.pyc in format(self, obj, include, exclude)
    124                     continue
    125             try:
--> 126                 data = formatter(obj)
    127             except:
    128                 # FIXME: log the exception

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/formatters.pyc in __call__(self, obj)
    445                 type_pprinters=self.type_printers,
    446                 deferred_pprinters=self.deferred_printers)
--> 447             printer.pretty(obj)
    448             printer.flush()
    449             return stream.getvalue()

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/lib/pretty.pyc in pretty(self, obj)
    353                 if callable(obj_class._repr_pretty_):
    354                     return obj_class._repr_pretty_(obj, self, cycle)
--> 355             return _default_pprint(obj, self, cycle)
    356         finally:
    357             self.end_group()

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
    473     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
    474         # A user-provided repr.
--> 475         p.text(repr(obj))
    476         return
    477     p.begin_group(1, '<')

/Library/Python/2.7/site-packages/pandas/core/series.pyc in __repr__(self)
    696             result = self._get_repr(print_header=True,
    697                                     length=len(self) > 50,
--> 698                                     name=True)
    699         else:
    700             result = '%s' % ndarray.__repr__(self)

/Library/Python/2.7/site-packages/pandas/core/series.pyc in _get_repr(self, name, print_header, length, na_rep, float_format)
    756                                         length=length, na_rep=na_rep,
    757                                         float_format=float_format)
--> 758         return formatter.to_string()
    759 
    760     def __str__(self):

/Library/Python/2.7/site-packages/pandas/core/format.pyc in to_string(self)
     99 
    100         fmt_index, have_header = self._get_formatted_index()
--> 101         fmt_values = self._get_formatted_values()
    102 
    103         maxlen = max(len(x) for x in fmt_index)

/Library/Python/2.7/site-packages/pandas/core/format.pyc in _get_formatted_values(self)
     90         return format_array(self.series.values, None,
     91                             float_format=self.float_format,
---> 92                             na_rep=self.na_rep)
     93 
     94     def to_string(self):

/Library/Python/2.7/site-packages/pandas/core/format.pyc in format_array(values, formatter, float_format, na_rep, digits, space, justify)
    431                         justify=justify)
    432 
--> 433     return fmt_obj.get_result()
    434 
    435 

/Library/Python/2.7/site-packages/pandas/core/format.pyc in get_result(self)
    528 
    529             # this is pretty arbitrary for now
--> 530             has_large_values = (np.abs(self.values) > 1e8).any()
    531 
    532             if too_long and has_large_values:

FloatingPointError: invalid value encountered in absolute

In [38]: ts.shift(-1)
Out[38]: ---------------------------------------------------------------------------
FloatingPointError                        Traceback (most recent call last)
/Users/myusername/Repository/work/SQS/analysis/campaign/tv2/data/<ipython-input-38-314ec815a7c5> in <module>()
----> 1 ts.shift(-1)

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/displayhook.pyc in __call__(self, result)
    236             self.start_displayhook()
    237             self.write_output_prompt()
--> 238             format_dict = self.compute_format_data(result)
    239             self.write_format_data(format_dict)
    240             self.update_user_ns(result)

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/displayhook.pyc in compute_format_data(self, result)
    148             MIME type representation of the object.
    149         """
--> 150         return self.shell.display_formatter.format(result)
    151 
    152     def write_format_data(self, format_dict):

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/formatters.pyc in format(self, obj, include, exclude)
    124                     continue
    125             try:
--> 126                 data = formatter(obj)
    127             except:
    128                 # FIXME: log the exception

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/core/formatters.pyc in __call__(self, obj)
    445                 type_pprinters=self.type_printers,
    446                 deferred_pprinters=self.deferred_printers)
--> 447             printer.pretty(obj)
    448             printer.flush()
    449             return stream.getvalue()

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/lib/pretty.pyc in pretty(self, obj)
    353                 if callable(obj_class._repr_pretty_):
    354                     return obj_class._repr_pretty_(obj, self, cycle)
--> 355             return _default_pprint(obj, self, cycle)
    356         finally:
    357             self.end_group()

/Library/Python/2.7/site-packages/ipython-0.13.dev-py2.7.egg/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
    473     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
    474         # A user-provided repr.
--> 475         p.text(repr(obj))
    476         return
    477     p.begin_group(1, '<')

/Library/Python/2.7/site-packages/pandas/core/series.pyc in __repr__(self)
    696             result = self._get_repr(print_header=True,
    697                                     length=len(self) > 50,
--> 698                                     name=True)
    699         else:
    700             result = '%s' % ndarray.__repr__(self)

/Library/Python/2.7/site-packages/pandas/core/series.pyc in _get_repr(self, name, print_header, length, na_rep, float_format)
    756                                         length=length, na_rep=na_rep,
    757                                         float_format=float_format)
--> 758         return formatter.to_string()
    759 
    760     def __str__(self):

/Library/Python/2.7/site-packages/pandas/core/format.pyc in to_string(self)
     99 
    100         fmt_index, have_header = self._get_formatted_index()
--> 101         fmt_values = self._get_formatted_values()
    102 
    103         maxlen = max(len(x) for x in fmt_index)

/Library/Python/2.7/site-packages/pandas/core/format.pyc in _get_formatted_values(self)
     90         return format_array(self.series.values, None,
     91                             float_format=self.float_format,
---> 92                             na_rep=self.na_rep)
     93 
     94     def to_string(self):

/Library/Python/2.7/site-packages/pandas/core/format.pyc in format_array(values, formatter, float_format, na_rep, digits, space, justify)
    431                         justify=justify)
    432 
--> 433     return fmt_obj.get_result()
    434 
    435 

/Library/Python/2.7/site-packages/pandas/core/format.pyc in get_result(self)
    528 
    529             # this is pretty arbitrary for now
--> 530             has_large_values = (np.abs(self.values) > 1e8).any()
    531 
    532             if too_long and has_large_values:

FloatingPointError: invalid value encountered in absolute

【问题讨论】:

对我来说很好。 Python 2.7.3,熊猫 0.7.0 谢谢 - 你是在 ipython 还是 python 中尝试过这个? 1.7.0,使用 scipysuperpack PS 安装,刚刚购买了您的 Python for Data Analysis 早期版本,这很棒,但也是我遇到这个问题的原因。 是的,我尝试使用 ipython 没有任何问题。虽然我的是 0.12.1。您正在使用 ipython 和 numpy 的开发版本。这些很可能是不稳定的。这可能是问题所在。 【参考方案1】:

我会将此添加为评论,但我还没有权限这样做:)

它适用于 python 和 iPython 0.12; iPython 0.13 仍在开发中(请参阅 http://ipython.org/ ),并且由于您遇到的错误似乎涉及 iPython 0.13 egg 中的格式,我怀疑这可能是原因。尝试改用 iPython 0.12 - 如果可行,请使用 iPython 提交错误报告,然后可能坚持使用 0.12,直到 0.13(更)稳定为止。

【讨论】:

感谢您的回复,0.12 上我仍然收到错误消息。所以它似乎在我的系统中更加普遍。奇怪的是在 bpython 中工作。 嗯。只是一个刺,但是,如果你在 iPython 中尝试 np.abs([np.nan, 1, 2, 3]) 会发生什么? 是这样想的。您确定您的 iPython numpy 与其他安装相同吗?在工作环境和非工作环境中尝试 np.__version__。 同版本1.7.0.dev-3cb783e 我已经解决了这个问题……尽管我猜版本号是一些构建问题。我清理了我拥有的包,现在它可以工作了......感谢他们的帮助。

以上是关于熊猫浮点错误的主要内容,如果未能解决你的问题,请参考以下文章

如何格式化熊猫数据框并保留原始浮点精度值

熊猫方式将一天中的时间(有效的 datetime.time)转换为浮点变量

Python将包含科学值和浮点值的熊猫数据框与浮点数相乘

js中浮点计算错误原因及解决办法

浮点错误 gfortran

为啥启用浮点异常后出现多个陷阱错误