2016-12-03 3 views
2

私は次のコードがあります。この場合割り当てはいつPythonでディープコピーを行いますか?

import pandas as pd 
store = pd.HDFStore('cache.h5') 
data = store['data'] 

は、HDF5データのメモリコピーに、data深いです、またはそれは、ディスク上の元のデータへのポインタですか?

+0

は正確にあなたが*ディスク上の元のデータへのポインタ*とはどういう意味ですか? – Tobias

答えて

1

"インメモリオブジェクト"です。これは自動的にディスクに反映(フラッシュ)されません。

デモ:

In [16]: fn = r'D:\temp\.data\test.h5' 

In [17]: store = pd.HDFStore(fn) 

In [18]: store 
Out[18]: 
<class 'pandas.io.pytables.HDFStore'> 
File path: D:\temp\.data\test.h5 
/df2    frame_table (typ->appendable,nrows->7,ncols->4,indexers->[index],dc->[Col1,Col2,Col3,Col4]) 
/test   frame_table (typ->appendable,nrows->7,ncols->4,indexers->[index],dc->[Col1,Col2,Col3,Col4]) 

は、データフレームの中に、ディスク(HDF店)からの読み取り(メモリ内のオブジェクト):

In [19]: data = store['test'] 

In [20]: data 
Out[20]: 
     Col1  Col2 Col3 Col4 
0  what  the  0  0 
1  are curves  1  8 
2  men  of  2 16 
3   to  your  3 24 
4  rocks  lips  4 32 
5  and rewrite  5 40 
6 mountains history.  6 48 

In [21]: data.Col4 = 1000 

In [22]: data 
Out[22]: 
     Col1  Col2 Col3 Col4 
0  what  the  0 1000 
1  are curves  1 1000 
2  men  of  2 1000 
3   to  your  3 1000 
4  rocks  lips  4 1000 
5  and rewrite  5 1000 
6 mountains history.  6 1000 

In [23]: store.close() 

In [24]: store = pd.HDFStore(fn) 

In [25]: store['test'] 
Out[25]: 
     Col1  Col2 Col3 Col4 
0  what  the  0  0 
1  are curves  1  8 
2  men  of  2 16 
3   to  your  3 24 
4  rocks  lips  4 32 
5  and rewrite  5 40 
6 mountains history.  6 48 

UPDATE:以下の小規模なデモがdata DFがないことを示していますがHDFストアから読み込まれた後にそれに依存します:

In [26]: store.close() 

In [27]: store = pd.HDFStore(fn) 

In [28]: del data 

In [29]: data = store['test'] 

はのは

In [30]: del store 

dataがまだあるstoreオブジェクト削除させ

In [31]: data 
Out[31]: 
     Col1  Col2 Col3 Col4 
0  what  the  0  0 
1  are curves  1  8 
2  men  of  2 16 
3   to  your  3 24 
4  rocks  lips  4 32 
5  and rewrite  5 40 
6 mountains history.  6 48 
+0

それは深いコピーであることを意味しますか?私が 'data'から読んだら、私は今RAMかディスクから読んでいますか? – cjm2671

+0

はい、HDFストアの1つのテーブルのディープコピーとして 'data'を考えることができます。基本的には、 'cache.h5'はHDF(h5)ファイル(ディスク上)で、複数のテーブル(DataFrames)を含む可能性があるDataFrame(メモリ内のオブジェクト)です – MaxU

+0

ありがとう、とても役に立ちます! – cjm2671

関連する問題