我有一个执行分析并将结果附加为对象属性的类,这些结果是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随后,我将所有重要的对象属性保存在一个字典中,并将其保留以供以后使用:
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)当我尝试装入这个酸洗过的文件时,完全没有问题,数据帧装入得很好:
>>> 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上对一堆这样的文件运行分析,然后尝试打开相同的pickled文件(它现在的名称不同,但它与上面显示的文件相同,并对其执行了相同的分析),我得到了pandas抛出的一个属性错误。它声明dataframe没有“_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问题,但我似乎找不到哪里出了问题。有没有人知道如何解决这个问题,以及我是否仍然可以访问这些数据?
发布于 2020-08-25 23:23:26
我也有同样的问题。我在一个使用Pandas 1.1.1的环境中生成了一个Pandas数据帧,并将其保存到一个pickle文件中。
with open('file.pkl', 'wb') as f:
pickle.dump(data_frame_object, f)在另一个会话中对其进行解选并打印数据帧后,我得到了相同的错误。在不同环境中的一些测试显示了以下模式:
使用Pandas
我在使用HDF5格式时也遇到了同样的错误,所以它似乎与数据帧和不同的Pandas版本之间存在兼容性问题。
在受影响的环境中更新Pandas到1.1.1为我解决了这个问题。
发布于 2020-08-25 06:30:16
经过一个漫长而痛苦的模块版本交叉检查过程,我发现这个错误是由于pandas版本的更新造成的。我的mac仍然运行pandas 1.0.5,而hpc运行的是pandas 1.1.0。显然,两者之间存在不匹配(不确定是在酸洗之后,还是用于保存的其他文件格式)。
发布于 2020-11-05 13:34:52
也许问题已经解决了。
嗯,但我还是想补充一些意见。
我将pkl文件保存在服务器上,但是当我将它加载到我的MAC上时,它崩溃了,显示为'Dataframe' object has no attribute '_data'。
最后,我发现我的Mac上的pandas是1.0.5,而服务器上的pandas是1.1.5。当我把它更新到最新版本时,它就能工作了。
https://stackoverflow.com/questions/63559007
复制相似问题