Python 与pickle 0.14.1和0.15.2的向后兼容性问题

Python 与pickle 0.14.1和0.15.2的向后兼容性问题,python,mongodb,pandas,pickle,Python,Mongodb,Pandas,Pickle,我们使用熊猫数据帧作为时间序列数据的主要数据容器。我们将数据帧打包成二进制blob,并将其放入mongoDB文档中进行存储,同时还提供了有关时间序列blob的元数据键 从0.14.1升级到0.15.2时,我们遇到了一个错误 创建数据帧的二进制blob(0.14.1) 错误案例:使用pandas 0.15.2从mongoDB读回 cPickle.loads(lz4.decompress(bd)) ---------------------------------------------------

我们使用熊猫数据帧作为时间序列数据的主要数据容器。我们将数据帧打包成二进制blob,并将其放入mongoDB文档中进行存储,同时还提供了有关时间序列blob的元数据键

从0.14.1升级到0.15.2时,我们遇到了一个错误

创建数据帧的二进制blob(0.14.1)

错误案例:使用pandas 0.15.2从mongoDB读回

cPickle.loads(lz4.decompress(bd))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-76f7b0b41426> in <module>()
----> 1 cPickle.loads(lz4.decompress(bd))
TypeError: ('_reconstruct: First argument must be a sub-type of ndarray', <built-in function _reconstruct>, (<class 'pandas.core.index.Index'>, (0,), 'b'))
在0.15.2环境中创建错误:

cPickle.load(open('cp0141.p','r'))
TypeError: ('_reconstruct: First argument must be a sub-type of ndarray', <built-in function_reconstruct>, (<class 'pandas.core.index.Int64Index'>, (0,), 'b'))
cPickle.load(打开('cp0141.p','r'))
TypeError:(“”(U):第一个参数必须是ndarray’,(,(0,),“b”)的子类型

这是明确提到的
索引
类现在不再是子类
ndarray
,而是熊猫对象,请参见


您只需使用
pd.read\u pickle
即可读取pickle。

+1!,重要的是:即使pickle不是数据帧,它也可以工作:)!至少在我的例子中是这样,我存储了一个目录,其中包含字符串键和数据帧作为值。
df = pd.DataFrame(np.random.randn(10,10))
cPickle.dump(df,open("cp0141.p","wb"))
cPickle.load(open('cp0141.p','r')) # no error
cPickle.load(open('cp0141.p','r'))
TypeError: ('_reconstruct: First argument must be a sub-type of ndarray', <built-in function_reconstruct>, (<class 'pandas.core.index.Int64Index'>, (0,), 'b'))