Skip to content

Commit b803644

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

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

pandas/io/pytables.py

+11-20
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

+4-3
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def test_keys(self, setup_path):
297297
assert set(store) == expected
298298

299299
def test_non_pandas_keys(self, setup_path):
300+
# GH 29916
300301
class Table1(tables.IsDescription):
301302
value1 = tables.Float32Col()
302303

@@ -313,10 +314,10 @@ class Table3(tables.IsDescription):
313314
h5file.create_table(group, "table2", Table2, "Table 2")
314315
h5file.create_table(group, "table3", Table3, "Table 3")
315316
with HDFStore(path) as store:
316-
assert len(store.keys(kind="tables")) == 3
317+
assert len(store.keys()) == 3
317318
expected = {"/group/table1", "/group/table2", "/group/table3"}
318-
assert set(store.keys(kind="tables")) == expected
319-
assert set(store.keys(kind="pandas")) == set()
319+
assert set(store.keys()) == expected
320+
assert set(store) == expected
320321

321322
def test_keys_ignore_hdf_softlink(self, setup_path):
322323

0 commit comments

Comments
 (0)