Pandas序列化错误 - 超过最大递归深度
我刚接触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 个回答
12
我也遇到过同样的问题。在我的情况下,是因为我把一个叫做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