Pandas to pickle 错误 - 超过最大递归深度

lsh*_*eng 2 python pickle pandas

我是 Python 及其泡菜格式的新手。

所以我在写 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
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

Wou*_*ter 7

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

注释“在行中 return (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
Run Code Online (Sandbox Code Playgroud)

  • 不仅如此,您可能会发现您已将所有 NavigableString 对象从 DataFrame 的数据区域中清除,但不要忘记标题。就我而言,我已将 NavigableString 对象添加到列标题中。对 DataFrame(本例中为“df”)执行“[type(c) for c in df.columns]”以查看类型。 (2认同)