在pandas中使用to_hdf函数存储HDF5文件后再次使用read_hdf方法读取文件报错
在使用pandas以HDF5数据格式进行文件存储时,使用.to_hdf方法将一个dataframe数组写入HDF5文件中,之后使用.read_hdf时报错该如何解决?是不是先要通过什么样的方式把HDF5文件先关闭再读取?还是说在read_hdf方法中设定合适的读取方式参数?
import numpy as np
import pandas as pd
date_series=pd.date_range('2000-01-01',periods=8000)
date_arr=pd.Series(date_series.values)
arr_1=np.random.randint(10000,100000,(8000,5))
sales_df1=pd.DataFrame(arr_1,columns=['A','B','C','D','E'])
arr_2=np.random.randint(100000,1000000,(8000,5))
sales_df2=pd.DataFrame(arr_2,columns=['F','G','H','I','J'])
store=pd.HDFStore('mydata.h5')
store['idx'],store['col_1']=date_arr,sales_df1
store.put('col_2',sales_df2,format='table')
print(store['idx'],store['col_1'],store['col_2'],sep='\n')
df_1=store.select('col_2',where=['index>1000 and index<=2000'],columns=['G','H'])
print(df_1)
arr_3=np.random.randint(100000,1000000,(8000,5))
sales_df3=pd.DataFrame(arr_3,columns=['K','L','M','N','O'])
sales_df3.to_hdf('mydata.h5','col_3',format='table')
df_3=pd.read_hdf('mydata.h5','col_3',where=['index>=1000 and index<2000'],columns=['M','N'])#ValueError: The file 'mydata.h5' is already opened, but not in read-only mode (as requested)
print(df_3) import numpy as np
import pandas as pd
date_series=pd.date_range('2000-01-01',periods=8000)
date_arr=pd.Series(date_series.values)
arr_1=np.random.randint(10000,100000,(8000,5))
sales_df1=pd.DataFrame(arr_1,columns=['A','B','C','D','E'])
arr_2=np.random.randint(100000,1000000,(8000,5))
sales_df2=pd.DataFrame(arr_2,columns=['F','G','H','I','J'])
store=pd.HDFStore('mydata.h5')
store['idx'],store['col_1']=date_arr,sales_df1
store.put('col_2',sales_df2,format='table')
print(store['idx'],store['col_1'],store['col_2'],sep='\n')
df_1=store.select('col_2',where=['index>1000 and index<=2000'],columns=['G','H'])
print(df_1)
arr_3=np.random.randint(100000,1000000,(8000,5))
sales_df3=pd.DataFrame(arr_3,columns=['K','L','M','N','O'])
sales_df3.to_hdf('mydata.h5','col_3',format='table')
store.close() # 先关闭 store
df_3=pd.read_hdf('mydata.h5','col_3',where=['index>=1000 and index<2000'],columns=['M','N'])
print(df_3)
页:
[1]