【问题标题】:Pandas to pickle error - maximum recursion depth exceededPandas to pickle 错误 - 超出最大递归深度
【发布时间】:2014-04-24 06:57:26
【问题描述】:

我是 Python 及其 pickle 格式的新手。

所以我在写 to_pickle 时遇到了一条错误消息。

>>> import pandas as pd
>>> old = pd.read_pickle('vol.pkl')
>>> old = old.append(updates)
>>> pd.to_pickle(old,'vol.pkl')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "\\\python-site-packages\win64_vc11\Lib\site-packages\pandas-0.13.0-py2.7-win-amd64.egg\pandas\io\pickle.py", line 15, in to_pickle
    pkl.dump(obj, f, protocol=pkl.HIGHEST_PROTOCOL)
  File "\\\python-site-packages\win64_vc11\Lib\site-packages\bs4\element.py", line 664, in __getnewargs__
    return (unicode(self),)
RuntimeError: maximum recursion depth exceeded while calling a Python object

有谁知道为什么会发生这种情况以及如何解决这个问题?

谢谢。

【问题讨论】:

  • 1.在这种情况下,自我是什么? 2. 你用 Python 2.7 吗?
  • 可能还有其他问题,但我认为您不需要pd.to_pickle('vol.pkl')。你想要old.to_pickle('vol.pkl')pd.to_pickle(old,'vol.pkl')
  • @User 是的,我使用的是 2.7.6。我没有得到你的第一个问题..
  • @KarlD。谢谢我已经修改了..
  • return (unicode(self),)这一行:waht是变量self的值吗?

标签: python pandas pickle


【解决方案1】:

我遇到了同样的问题。在我的情况下,它是由向数据框添加 bs4.element.NavigableString 元素引起的。将其转换为字符串解决了这个问题!

注释“在返回 (unicode(self),) 行中:变量 self 的值是多少?”让我解决,谢谢!我注意到错误消息中的 bs4\element.py 。我意识到我(不小心)也在我的数据框中添加了 bs4 项。我错误地认为soup.title.string 返回了一个字符串,但它返回了一个bs4.element.NavigableString 类型。

    # the issue: type(title) = bs4.element.NavigableString

    ~\Anaconda3\lib\site-packages\bs4\element.py in __getnewargs__(self)
    784 
    785     def __getnewargs__(self):
--> 786         return (str(self),)
    787 
    788     def __getattr__(self, attr):

RecursionError: maximum recursion depth exceeded while getting the str of an object

【讨论】:

  • 不仅如此,您可能会发现您已将所有 NavigableString 对象从 DataFrame 的数据区域中清除,但不要忘记标题。就我而言,我已将 NavigableString 对象添加到列标题中。为您的 DataFrame(在本例中为 df)执行 [type(c) for c in df.columns] 以查看类型。
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 2018-12-03
  • 2017-07-18
  • 2021-12-11
  • 2019-07-03
  • 2017-08-24
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多