I have a large Pandas Dataframe stored in a HDF5 file. The Dataframe is multi-index with Datetime and PatientID.
store = pd.HDFStore('prepro_mon.h5',mode='r')
print(store.get_storer('raw_import').levels)
Out: ['Datetime', 'PatientID']
But filtering a Select statement by Patient ID doesn't work.
store.select('raw_import', where='PatientID = 88000')
I get this error
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 4660, in generate
return Expr(where, queryables=q, encoding=self.table.encoding)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\pytables.py", line 524, in __init__
self.terms = self.parse()
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 742, in parse
return self._visitor.visit(self.expr)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 312, in visit
return visitor(node, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 318, in visit_Module
return self.visit(expr, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 312, in visit
return visitor(node, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 321, in visit_Expr
return self.visit(node.value, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 312, in visit
return visitor(node, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 643, in visit_Compare
return self.visit(binop)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 312, in visit
return visitor(node, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 415, in visit_BinOp
op, op_class, left, right = self._possibly_transform_eq_ne(node)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 353, in _possibly_transform_eq_ne
left = self.visit(node.left, side='left')
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 312, in visit
return visitor(node, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\expr.py", line 429, in visit_Name
return self.term_type(node.id, self.env, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\pytables.py", line 41, in __init__
super(Term, self).__init__(name, env, side=side, encoding=encoding)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\ops.py", line 58, in __init__
self._value = self._resolve_name()
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\computation\pytables.py", line 47, in _resolve_name
raise NameError('name {0!r} is not defined'.format(self.name))
NameError: name 'PatientID' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:\OxygenationIndex\dataloaderOIFeatures.py", line 51, in <module>
store.select('raw_import', where='PatientID = 88000')
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 724, in select
return it.get_result()
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 1423, in get_result
results = self.func(self.start, self.stop, where)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 717, in func
columns=columns, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 4266, in read
df = super(AppendableMultiFrameTable, self).read(**kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 4086, in read
if not self.read_axes(where=where, **kwargs):
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 3287, in read_axes
self.selection = Selection(self, where=where, **kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 4647, in __init__
self.terms = self.generate(where)
File "C:\Program Files\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 4672, in generate
.format(where, ','.join(q.keys()))
ValueError: The passed where expression: PatientID = 88000
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: SvO2,AWPMean,peep_s,SpO2,fiO2_m,TempCentral,tv_m,fiO2_s,HF,RR_s,TempPeri,etCO2,tv_s,peep_m,peakP_m,RR_m,columns,StayMinutes,VentMode,plateauP,index
Any help is much appreciated. I^m not sure if it might be a bug, because I keep reading that it should be possible to query by index.
I could reproduce this error when saving HDF file using
data_columns=True
parameter.Demo:
yields:
now let's save it to HDF file - once without specifying
data_columns
(default:None
) and once withdata_columns=True
:both have the same index levels:
let's try to select using
where
clause:SOLUTION:
If you want to index columns you can use the following trick:
which produces list of all columns including MultiIndex columns:
Test
select(where="...")
:Store info:
Indexed columns in the HDF Store: