Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if key is in HDF5Store without path

Using pandas/pytables, a list of keys can be easily returned using store.keys().

>>> store.keys()
['/df_coord', '/metaFrame']

Using the standard dictionary check to see if a key exists, if 'df_coord' in store.keys():, returns false unless the / is included. Is there another simple way to evaluate for the existence of a key without having to join strings?

like image 703
riddley_w Avatar asked Jan 23 '14 09:01

riddley_w


1 Answers

Check against the store itself; they .keys() returns a string dictionary of the exact keys.

In [1]: store = pd.HDFStore('test.h5',mode='w')

In [2]: store['foo'] = DataFrame(np.random.randn(10,2))

In [3]: store['bar'] = DataFrame(np.random.randn(10,2))

In [4]: store
Out[4]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/bar            frame        (shape->[10,2])
/foo            frame        (shape->[10,2])

In [5]: 'bar' in store
Out[5]: True

In [6]: 'foo' in store
Out[6]: True

In [7]: '/foo' in store
Out[7]: True

In [8]: 'bah' in store
Out[8]: False
like image 59
Jeff Avatar answered Nov 20 '22 20:11

Jeff