Try to replace data_columns=True
with data_columns=df.columns.tolist()
.
Demo:
Original MultiIndex DF:
In [2]: df
Out[2]:
Values
Item N
Items0 Item0 0.25
Item1 0.50
Item2 0.75
Item3 1.00
Items1 Item0 0.25
Item1 0.50
Item2 0.75
Item3 1.00
save it to HDF5 using data_columns=df.columns.tolist()
:
In [3]: df.to_hdf('c:/temp/hdfs.h5','df',format='t',mode='w',complevel=9,complib='blosc',data_columns=df.columns.tolist())
In [4]: df.columns.tolist()
Out[4]: ['Values']
selecting from HDF store:
In [5]: store = pd.HDFStore('c:/temp/hdfs.h5')
both index levels and Values
column are indexed now and can be used in where=<query>
argument:
In [6]: store.select('df',where='Item="Items0" and Values in [0.5, 1]')
Out[6]:
Values
Item N
Items0 Item1 0.5
Item3 1.0
In [7]: store.select('df',where='N="Item3" and Values in [0.5, 1]')
Out[7]:
Values
Item N
Items0 Item3 1.0
Items1 Item3 1.0
storer information:
In [8]: store.get_storer('df').table
Out[8]:
/df/table (Table(8,), shuffle, blosc(9)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"N": StringCol(itemsize=5, shape=(), dflt=b'', pos=1),
"Item": StringCol(itemsize=6, shape=(), dflt=b'', pos=2),
"Values": Float64Col(shape=(), dflt=0.0, pos=3)}
byteorder := 'little'
chunkshape := (2427,)
autoindex := True
colindexes := {
"Values": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"Item": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"N": Index(6, medium, shuffle, zlib(1)).is_csi=False}
storer index levels:
In [9]: store.get_storer('df').levels
Out[9]: ['Item', 'N']
NOTE: if you simply omit data_columns
parameter, then only indexes will be indexed in the HDF store, all other columns will NOT be searchable:
Demo:
In [19]: df.to_hdf('c:/temp/NO_data_columns.h5', 'df', format='t',mode='w',complevel=9,complib='blosc')
In [20]: store = pd.HDFStore('c:/temp/NO_data_columns.h5')
In [21]: store.select('df',where='N == "Item3"')
Out[21]:
Values
Item N
Items0 Item3 1.0
Items1 Item3 1.0
In [22]: store.select('df',where='N == "Item3" and Values == 1')
---------------------------------------------------------------------------
...
skipped
...
ValueError: The passed where expression: N == "Item3" and Values == 1
contains an invalid variable reference
all of the variable refrences must be a reference to
an axis (e.g. 'index' or 'columns'), or a data_column
The currently defined references are: N,index,Item,columns
UPDATE:
What is the real difference in putting
data_columns=df.columns.tolist() ?
In [18]: fn = r'd:/temp/a.h5'
In [19]: df.to_hdf(fn,'dc_true',data_columns=True,format='t',mode='w',complevel=9,complib='blosc')
In [20]: df.to_hdf(fn,'dc_cols',data_columns=df.columns.tolist(),format='t',complevel=9,complib='blosc')
In [21]: store = pd.HDFStore(fn)
In [22]: store
Out[22]:
<class 'pandas.io.pytables.HDFStore'>
File path: d:/temp/a.h5
/dc_cols frame_table (typ->appendable_multi,nrows->8,ncols->3,indexers->[index],dc->[N,Item,Values])
/dc_true frame_table (typ->appendable_multi,nrows->8,ncols->3,indexers->[index],dc->[Values])
In [23]: store.get_storer('dc_true').table.colindexes
Out[23]:
{
"Values": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
In [24]: store.get_storer('dc_cols').table.colindexes
Out[24]:
{
"Item": Index(6, medium, shuffle, zlib(1)).is_csi=False, # <- missing when `data_columns=True`
"N": Index(6, medium, shuffle, zlib(1)).is_csi=False, # <- missing when `data_columns=True`
"Values": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
So the difference is how the index columns will be indexed