Rex*_*x D 5 python pickle pandas
我正在尝试腌制从 pandas.DataFrame 继承的对象。我添加到数据帧的属性在酸洗/解酸过程中消失了。有一些明显的解决方法,但是......我做错了什么,还是这是一个错误?
import pandas as pd
import pickle
class Foo(pd.DataFrame):
def __init__(self,tag,df):
super().__init__(df)
self._tag = tag
foo = Foo('mytag', pd.DataFrame({'a':[1,2,3],'b':[4,5,6]}))
print(foo)
print(foo._tag)
print("-------------------------------------")
with open("foo.pkl", "wb") as pkl:
pickle.dump(foo, pkl)
with open("foo.pkl", "rb") as pkl:
foo1 = pickle.load(pkl)
print(type(foo1))
print(foo1)
print(foo1._tag)
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
a b
0 1 4
1 2 5
2 3 6
mytag
-------------------------------------
<class '__main__.Foo'>
a b
0 1 4
1 2 5
2 3 6
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-1e7e89e199c8> in <module>
21 print(type(foo1))
22 print(foo1)
---> 23 print(foo1._tag)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5066 return self[name]
-> 5067 return object.__getattribute__(self, name)
5068
5069 def __setattr__(self, name, value):
AttributeError: 'Foo' object no attribute '_tag'
Run Code Online (Sandbox Code Playgroud)
(python 3.7,熊猫 0.24.2,pickle.format_version 4.0)
我认为这是 Pandas 如何处理属性的问题。即使是简化的继承尝试也行不通:
class Foo(pd.DataFrame):
def __init__(self, tag, df):
self._tag = tag
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "c:\Users\Michael\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\Michael\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\__main__.py", line 434, in main
run()
File "c:\Users\Michael\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\__main__.py", line 312, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\Michael\Desktop\sandbox\sandbox.py", line 8, in <module>
foo = Foo('mytag', pd.DataFrame({'a':[1,2,3],'b':[4,5,6]}))
File "c:\Users\Michael\Desktop\sandbox\sandbox.py", line 6, in __init__
self._tag = tag
File "c:\Users\Michael\Desktop\sandbox\venv\lib\site-packages\pandas\core\generic.py", line 5205, in __setattr__
existing = getattr(self, name)
File "c:\Users\Michael\Desktop\sandbox\venv\lib\site-packages\pandas\core\generic.py", line 5178, in __getattr__
if self._info_axis._can_hold_identifiers_and_holds_name(name):
File "c:\Users\Michael\Desktop\sandbox\venv\lib\site-packages\pandas\core\generic.py", line 5178, in __getattr__
if self._info_axis._can_hold_identifiers_and_holds_name(name):
File "c:\Users\Michael\Desktop\sandbox\venv\lib\site-packages\pandas\core\generic.py", line 5178, in __getattr__
if self._info_axis._can_hold_identifiers_and_holds_name(name):
[Previous line repeated 487 more times]
File "c:\Users\Michael\Desktop\sandbox\venv\lib\site-packages\pandas\core\generic.py", line 489, in _info_axis
return getattr(self, self._info_axis_name)
File "c:\Users\Michael\Desktop\sandbox\venv\lib\site-packages\pandas\core\generic.py", line 5163, in __getattr__
def __getattr__(self, name):
File "c:\Users\Michael\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_trace_dispatch_regular.py", line 362, in __call__
is_stepping = pydev_step_cmd != -1
RecursionError: maximum recursion depth exceeded in comparison
Run Code Online (Sandbox Code Playgroud)
我认为这是他们对 的使用__getattribute__(),当它发现未知属性时会抛出错误。他们正在重写默认__getattr__()行为,我猜这会扰乱继承。
| 归档时间: |
|
| 查看次数: |
460 次 |
| 最近记录: |