Skip to content

Commit f41662f

Browse files
HDFStore: Fix empty result of keys() method on non-pandas hdf5 file
An additional kind parameter has been added that defaults to pandas original behavior, but with 'tables' value gives you the list of non-pandas tables in the file
1 parent 5da500a commit f41662f

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

pandas/io/pytables.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,33 @@ def __enter__(self):
580580
def __exit__(self, exc_type, exc_value, traceback):
581581
self.close()
582582

583-
def keys(self) -> List[str]:
583+
def keys(self, kind='pandas') -> List[str]:
584584
"""
585585
Return a list of keys corresponding to objects stored in HDFStore.
586586
587+
Parameters
588+
----------
589+
kind : str, default 'pandas'
590+
When kind equals 'pandas' return pandas objects
591+
When kind equals 'table' return Tables
592+
Otherwise fail with a ValueError
593+
594+
Raises
595+
------
596+
raises ValueError if kind has an illegal value
597+
587598
Returns
588599
-------
589600
list
590601
List of ABSOLUTE path-names (e.g. have the leading '/').
591602
"""
592-
return [n._v_pathname for n in self.groups()]
603+
if kind == 'pandas':
604+
return [n._v_pathname for n in self.groups()]
605+
606+
if kind == 'tables':
607+
self._check_if_open()
608+
return [n._v_pathname for n in self._handle.walk_nodes('/', classname='Table')]
609+
raise ValueError(f"kind should be either pandas' or 'table' but is {kind}")
593610

594611
def __iter__(self):
595612
return iter(self.keys())

pandas/tests/io/pytables/test_store.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,26 @@ def test_keys(self, setup_path):
296296
assert set(store.keys()) == expected
297297
assert set(store) == expected
298298

299+
def test_non_pandas_keys(self, setup_path):
300+
301+
class Table1(tables.IsDescription):
302+
value1 = tables.Float32Col()
303+
class Table2(tables.IsDescription):
304+
value2 = tables.Float32Col()
305+
class Table3(tables.IsDescription):
306+
value3 = tables.Float32Col()
307+
with ensure_clean_path(setup_path) as path:
308+
with tables.open_file(path, mode="w") as h5file:
309+
group = h5file.create_group("/", "group")
310+
table1 = h5file.create_table(group, "table1", Table1, "Table 1")
311+
table2 = h5file.create_table(group, "table2", Table2, "Table 2")
312+
table3 = h5file.create_table(group, "table3", Table3, "Table 3")
313+
with HDFStore(path) as store:
314+
assert len(store.keys(kind="tables")) == 3
315+
expected = {"/group/table1", "/group/table2", "/group/table3"}
316+
assert set(store.keys(kind="tables")) == expected
317+
assert set(store) == set()
318+
299319
def test_keys_ignore_hdf_softlink(self, setup_path):
300320

301321
# GH 20523

0 commit comments

Comments
 (0)