gpt4 book ai didi

python-3.x - 包含 Pandas 数据帧的 Unpickling 字典抛出 AttributeError : 'Dataframe' object has no attribute '_data'

转载 作者:行者123 更新时间:2023-12-03 10:08:08 25 4
gpt4 key购买 nike

我有一个执行分析并附加结果的类,这些结果是 Pandas 数据帧,作为对象属性:

>>> print(test.image.locate_DF)
y x mass ... raw_mass ep frame
0 60.177142 59.788709 33.433414 ... 242.080256 NaN 0
1 60.651991 59.773904 33.724308 ... 242.355784 NaN 1
2 60.790437 60.190234 31.117164 ... 236.276671 NaN 2
3 60.771933 60.048123 33.558372 ... 240.981395 NaN 3
4 60.251282 59.775139 31.881009 ... 239.239022 NaN 4
... ... ... ... ... ... ... ...
7212 68.186380 76.477449 18.122817 ... 176.523091 NaN 9410
7213 68.764444 76.574091 17.486454 ... 173.448306 NaN 9415
7214 68.191152 76.473477 17.402975 ... 172.848119 0.868326 9429
7215 67.034103 76.025885 17.010951 ... 170.928067 -0.600854 9431
7216 68.583276 75.309592 17.852992 ... 178.271558 NaN 9432
随后,我将所有重要的对象属性保存在一个字典中,并将其pickle以备后用:
def save_parameters(self, filepath):

param_dict = {}

try:
self.image.locate_DF
except AttributeError:
pass
else:
param_dict['optical_locate_DF'] = self.image.locate_DF

with open(filepath, 'wb') as handle:
pickle.dump(param_dict, handle, 5)
尝试加载该 pickle 文件时,我完全没有问题,数据框加载完美:
>>> test.save_parameters('test.pickle')
>>> with open('test.pickle', 'rb') as handle:
... result = pickle.load(handle)
...
>>> print(result.keys())
dict_keys(['optical_path', 'optical_feature_diameter', 'optical_feature_minmass', 'optical_locate_DF', 'electrical_path', 'electrical_raw_data', 'electrical_processed_data', 'electrical_mean_voltage'])
>>> print(result['optical_locate_DF'])
y x mass ... raw_mass ep frame
0 60.177142 59.788709 33.433414 ... 242.080256 NaN 0
1 60.651991 59.773904 33.724308 ... 242.355784 NaN 1
2 60.790437 60.190234 31.117164 ... 236.276671 NaN 2
3 60.771933 60.048123 33.558372 ... 240.981395 NaN 3
4 60.251282 59.775139 31.881009 ... 239.239022 NaN 4
... ... ... ... ... ... ... ...
7212 68.186380 76.477449 18.122817 ... 176.523091 NaN 9410
7213 68.764444 76.574091 17.486454 ... 173.448306 NaN 9415
7214 68.191152 76.473477 17.402975 ... 172.848119 0.868326 9429
7215 67.034103 76.025885 17.010951 ... 170.928067 -0.600854 9431
7216 68.583276 75.309592 17.852992 ... 178.271558 NaN 9432

[7217 rows x 9 columns]
但是,在 hpc 上对一堆这些文件运行我的分析之后,然后尝试打开同一个 pickle 文件(它现在的名称不同,但它与上面显示的文件相同,对其执行的分析相同),我得到 Pandas 抛出了一个属性错误。它指出数据帧没有“_data”属性。字典具有相同的键,并且非数据框的键被打印出来没有任何问题:
>>> resultfile = '../results/diam_15_minmass_17_dist_50_mem_5000_tracklength_500/R9_DNA_50mV_001.pickle'
>>> with open(resultfile, 'rb') as handle:
... result = pickle.load(handle)
...
>>> print(result.keys())
dict_keys(['optical_path', 'optical_feature_diameter', 'optical_feature_minmass', 'optical_locate_DF', 'optical_tracking_distance', 'optical_tracking_memory', 'optical_tracking_DF', 'optical_kinetics_DF', 'electrical_path', 'electrical_raw_data', 'electrical_processed_data', 'electrical_mean_voltage'])
>>> print(result['optical_locate_DF'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/stevenvanuytsel/miniconda3/envs/simultaneous_measurements/lib/python3.8/site-packages/pandas/core/frame.py", line 680, in __repr__
self.to_string(
File "/Users/stevenvanuytsel/miniconda3/envs/simultaneous_measurements/lib/python3.8/site-packages/pandas/core/frame.py", line 801, in to_string
formatter = fmt.DataFrameFormatter(
File "/Users/stevenvanuytsel/miniconda3/envs/simultaneous_measurements/lib/python3.8/site-packages/pandas/io/formats/format.py", line 593, in __init__
self.max_rows_displayed = min(max_rows or len(self.frame), len(self.frame))
File "/Users/stevenvanuytsel/miniconda3/envs/simultaneous_measurements/lib/python3.8/site-packages/pandas/core/frame.py", line 1041, in __len__
return len(self.index)
File "/Users/stevenvanuytsel/miniconda3/envs/simultaneous_measurements/lib/python3.8/site-packages/pandas/core/generic.py", line 5270, in __getattr__
return object.__getattribute__(self, name)
File "pandas/_libs/properties.pyx", line 63, in pandas._libs.properties.AxisProperty.__get__
File "/Users/stevenvanuytsel/miniconda3/envs/simultaneous_measurements/lib/python3.8/site-packages/pandas/core/generic.py", line 5270, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute '_data'
我查看了 pickle 手册,并通过了一堆 SO 问题,但我似乎无法找出这里出了什么问题。有没有人知道如何解决这个问题,以及我是否仍然可以访问该数据?

最佳答案

我有同样的问题。我在 Pandas 1.1.1 的环境中生成了一个 Pandas 数据框,并将其保存到一个 pickle 文件中。

with open('file.pkl', 'wb') as f:
pickle.dump(data_frame_object, f)
在另一个 session 中解压它并打印数据帧后,我得到了同样的错误。在不同环境中的一些测试显示了以下模式:
  • Pandas >= 1.1.0 的环境:有效
  • 使用 Pandas == 1.0.5 的环境:错误消息如上
  • 使用 Pandas == 1.0.3 的环境:内核崩溃

  • 我在使用 HDF5 格式时遇到了同样的错误,所以这似乎是数据帧和不同 Pandas 版本的兼容性问题。
    在受影响的环境中将 Pandas 更新到 1.1.1 为我解决了这个问题。

    关于python-3.x - 包含 Pandas 数据帧的 Unpickling 字典抛出 AttributeError : 'Dataframe' object has no attribute '_data' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63559007/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com