私はpandas DataFrameから新しいデータをPyTableに保存したpandas DataFrameを更新する関数を作成しようとしています。特定のDatetimeIndexes(値がNaNまたは新しいTimestampが使用可能)のPyTableにデータがないかどうかを確認し、これを特定のpandas DataFrameの新しい値に置き換えて、これをPytableに追加します。基本的には、Pytableを更新するだけです。私は、Pandasのcombine_firstメソッドを使って結合したDataFrameを得ることができます。だから、pytableが作成された他のpandas DataFrameとPytableに格納されたpandas DataFrameを更新します
import pandas as pd import numpy as np import datetime as dt index = pd.DatetimeIndex(start = dt.datetime(2001,1,1,0,0), periods = 20000,freq='10T') data_in_pytable = pd.DataFrame(index=index,data=np.random.randn(20000,2),columns=['value_1','value_2']) data.to_hdf(r'C:\pytable.h5','test',mode='r+',append=True,complevel=9,complib='zlib')
:Pytable以下 は、ダミーデータで作成されます。私は私がPytableを更新したいと別のDATAFRAME持っていると仮定:
new_index = pd.DatetimeIndex(start = dt.datetime(2001,5,1,0,0), periods = 10000,freq='10T') data_to_update=pd.DataFrame(index=new_index,data=np.random.randn(10000,2),columns=['value_1','value_2']) store=pd.HDFStore(r'C:\pytable.h5',mode='r+',complevel=9,complib='zlib') store.append('test',store.select('test').combine_first(data_to_update)) store.close()
問題はPyTableは元の値を保持していることであるが、既存のものを更新しません。元の値が上書きされないため、インデックス(重複したエントリ)が表示されるようになりました。
要約: 別のDataFrameでPyTableを更新するにはどうすればよいですか?
おかげで、 ELV
実際、それは私の一時的な解決策でした。情報をありがとう – Elvin