- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试挑选从 pandas.DataFrame 继承的对象。我添加到数据框的属性在 pickling/unpickling 过程中消失了。有一些明显的解决方法,但是......我做错了什么,还是这是一个错误?
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)
这是我的输出:
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'
(python 3.7,pandas 0.24.2,pickle.format_version 4.0)
最佳答案
Michael 的回答与我在查看他们的代码时的发现相符。 DataFrame 继承自 NDFrame
,后者也覆盖了 __setattr__
,因此这也可能导致此问题。
这里最直接的解决方案是创建一个使用数据框作为属性的类,以便您可以设置自己的属性。
class Foo:
def __init__(self, tag, df):
self.df = df
self._tag = tag
*此外:如果 native pickle
无法 pickle 像这样的复杂对象,我会考虑尝试 dill
。 $ pip install dill
之后,您需要做的就是import dill as pickle
,因为它的方法名称与pickle
相同。
关于python - 无法解开从 pandas DataFrame 继承的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237664/
是否可以在 Delphi 中解开这些名称?如果是这样,我从哪里可以获得更多信息? 在 dbrtl100.bpl 中找不到特定条目的错误消息示例我想知道它找不到哪个确切的函数(单元、类、名称、参数等)。
我是小白,如何从列表中删除引号和逗号?或者我如何“取消字符串”? 在不显示字典 (dic) 的情况下,我使用的代码如下所示: >>>import itertools >>>list(itertools
我正在学习构建器模式,到目前为止我了解到,它是用于初始化的常用模式的一个很好的替代方案: 伸缩构造函数模式 JavaBean 模式 问题是,我真的不喜欢从我的域模型中的对象中删除 getter 和 s
我有这段文字 "Welcome to my city. Hello my good friend" 使用 jQuery(或 javascript),我想解开所有 span 标签,只保留第一级,我的意思
在为大学撰写作业的过程中,我一直享受着学习新的 Haskell monad 的乐趣。耶!!! 我有一个可以很好地进行类型检查的函数: compile :: Prog -> State VarsStat
let myLabel:UILabel = UILabel(frame:CGRectMake(0, 0, 44, 44)) myLabel.text = "Primary"
是否有一个好的 npm 包可以删除在 NodeJS 服务器上运行的 html 字符串中不必要的嵌套标签(无浏览器 DOM)?我尝试过 sanitize-html,但似乎不可能做到这一点。 我收到用户发
我目前正在编写一些日志代码,这些代码应该——除其他外——打印有关调用函数的信息。这应该比较容易,标准C++有一个type_info类(class)。这包含 typeid 的类/函数/等的名称。但它被破
我有以下组件 const ListItem = ({ children, title = '' }) => { const [isActive, setIsActive] = useSta
我正在尝试取消在 python3 中 pickle 的对象。这在 python3 中有效,但在 python2 中无效。该问题可以重现到 pickle 协议(protocol) 0。示例代码: imp
这个问题的灵感来自 How to transform a flow chart into an implementation?它询问从代码中通过算法消除 goto 语句的方法。 answer this
当我的 child 执行 unwind segue 时,我的 Controller 的 viewDidAppear 被调用。 在这个方法中(只有这个方法,我需要知道它是否来自 unwind) 注意:
我用 C 编写了一个小程序来管理 SQLite3 中的书目数据库。到目前为止,这只是一个允许导入和导出 BibTeX 数据的命令行工具。为了使导出/导入功能更加可定制(例如,在导入时始终在日期字段中组
这个问题与How to split an array according to a condition in numpy?密切相关但我正在寻找一种更通用的方法来拆分给定未知数量索引的数组: impor
我是一名优秀的程序员,十分优秀!