Skip to content

Commit a511f80

Browse files
Simplify interface by using a fallback scenario
- first try new behavior - if no result return native HDF5 Tables
1 parent aa65701 commit a511f80

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

pandas/io/pytables.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -580,35 +580,26 @@ def __enter__(self):
580580
def __exit__(self, exc_type, exc_value, traceback):
581581
self.close()
582582

583-
def keys(self, kind: Optional[str] = "pandas") -> List[str]:
583+
def keys(self) -> List[str]:
584584
"""
585585
Return a list of keys corresponding to objects stored in HDFStore.
586-
587-
Parameters
588-
----------
589-
kind : str, default 'pandas'
590-
When kind equals 'pandas' return pandas objects
591-
When kind equals 'table' return Table objects
592-
Otherwise fail with a ValueError
586+
If the store contains pandas native tables, it will return their names.
587+
Otherwise the list of names of HDF5 Table objects will be returned.
593588
594589
Returns
595590
-------
596591
list
597592
List of ABSOLUTE path-names (e.g. have the leading '/').
598-
599-
Raises
600-
------
601-
raises ValueError if kind has an illegal value
602593
"""
603-
if kind == "pandas":
604-
return [n._v_pathname for n in self.groups()]
594+
# if kind == "pandas":
595+
objects = [n._v_pathname for n in self.groups()]
596+
if objects:
597+
return objects
605598

606-
if kind == "tables":
607-
self._check_if_open()
608-
return [
609-
n._v_pathname for n in self._handle.walk_nodes("/", classname="Table")
610-
]
611-
raise ValueError(f"`kind` should be either 'pandas' or 'table' but is [{kind}]")
599+
self._check_if_open()
600+
return [
601+
n._v_pathname for n in self._handle.walk_nodes("/", classname="Table")
602+
]
612603

613604
def __iter__(self):
614605
return iter(self.keys())

pandas/tests/io/pytables/test_store.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ def test_keys(self, setup_path):
299299
assert set(store) == expected
300300

301301
def test_non_pandas_keys(self, setup_path):
302+
# GH 29916
302303
class Table1(tables.IsDescription):
303304
value1 = tables.Float32Col()
304305

@@ -315,10 +316,10 @@ class Table3(tables.IsDescription):
315316
h5file.create_table(group, "table2", Table2, "Table 2")
316317
h5file.create_table(group, "table3", Table3, "Table 3")
317318
with HDFStore(path) as store:
318-
assert len(store.keys(kind="tables")) == 3
319+
assert len(store.keys()) == 3
319320
expected = {"/group/table1", "/group/table2", "/group/table3"}
320-
assert set(store.keys(kind="tables")) == expected
321-
assert set(store.keys(kind="pandas")) == set()
321+
assert set(store.keys()) == expected
322+
assert set(store) == expected
322323

323324
def test_keys_ignore_hdf_softlink(self, setup_path):
324325

0 commit comments

Comments
 (0)