Skip to content

Commit f9931e5

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 d33b002 commit f9931e5

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
@@ -590,16 +590,33 @@ def __enter__(self):
590590
def __exit__(self, exc_type, exc_value, traceback):
591591
self.close()
592592

593-
def keys(self) -> List[str]:
593+
def keys(self, kind='pandas') -> List[str]:
594594
"""
595595
Return a list of keys corresponding to objects stored in HDFStore.
596596
597+
Parameters
598+
----------
599+
kind : str, default 'pandas'
600+
When kind equals 'pandas' return pandas objects
601+
When kind equals 'table' return Tables
602+
Otherwise fail with a ValueError
603+
604+
Raises
605+
------
606+
raises ValueError if kind has an illegal value
607+
597608
Returns
598609
-------
599610
list
600611
List of ABSOLUTE path-names (e.g. have the leading '/').
601612
"""
602-
return [n._v_pathname for n in self.groups()]
613+
if kind == 'pandas':
614+
return [n._v_pathname for n in self.groups()]
615+
616+
if kind == 'tables':
617+
self._check_if_open()
618+
return [n._v_pathname for n in self._handle.walk_nodes('/', classname='Table')]
619+
raise ValueError(f"kind should be either pandas' or 'table' but is {kind}")
603620

604621
def __iter__(self):
605622
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
@@ -298,6 +298,26 @@ def test_keys(self, setup_path):
298298
assert set(store.keys()) == expected
299299
assert set(store) == expected
300300

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

303323
# GH 20523

0 commit comments

Comments
 (0)