2015-01-14 9 views
6

私たちは、時系列データのプライマリデータコンテナとしてpandas Dataframeを使用しています。私たちは、データフレームをバイナリブロブにmongoDBドキュメントに格納して、時系列ブロブに関するメタデータのキーと共に格納します。パンダ0.142.1と0.15.2との下位互換性問題

パンダ0.14.1から0.15.2にアップグレードしたときにエラーが発生しました。

パンダDATAFRAME(0.14.1)

import lz4 
import cPickle 

bd = lz4.compress(cPickle.dumps(df,cPickle.HIGHEST_PROTOCOL)) 

エラーケースのバイナリブロブを作成します:読む:パンダ0.15.2

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')) 

成功事例でのMongoDBから戻って読みますpandas 0.14.1でmongoDBからエラーなしで戻ってください。

これは、回避策や解決策上の任意のアイデアhttps://stackoverflow.com/users/644898/jeff

The error message you are seeing `TypeError: _reconstruct: First argument must be a sub-type of ndarray is that the python default unpickler makes sure that the class hierarchy that was pickled is exactly the same what it is recreating. Since Series has changed between versions this is no longer possible with the default unpickler, (this IMHO is a bug in the way pickle works). In any event, pandas will unpickle pre-0.13 pickles that have Series objects."

から役に立つコメントで古いスタックスレッドPandas compiled from source: default pickle behavior changed に似ているようですか?パンダ0.14.1 ENVに

セットアップ:エラーを再現する

df = pd.DataFrame(np.random.randn(10,10)) 
cPickle.dump(df,open("cp0141.p","wb")) 
cPickle.load(open('cp0141.p','r')) # no error 

はパンダ0.15.2 ENVでエラーを作成します。

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')) 

答えて

6

これは、として言及明示的でしたIndexクラスは今やサブクラスndarrayではなく、パンダオブジェクトです(hereを参照)。 。

ピクルスを読むには、単にpd.read_pickleを使用する必要があります。

+0

+1!重要なのは、ピクルがデータフレームでない場合でも機能します:)!少なくとも私の場合はうまくいっていて、文字列キーとデータフレームを含むディレクトリを値として格納していました。 – ntg