Skip to content

Commit 331acaf

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

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

593-
def keys(self, kind: Optional[str] = "pandas") -> List[str]:
593+
def keys(self) -> List[str]:
594594
"""
595595
Return a list of keys corresponding to objects stored in HDFStore.
596-
597-
Parameters
598-
----------
599-
kind : str, default 'pandas'
600-
When kind equals 'pandas' return pandas objects
601-
When kind equals 'table' return Table objects
602-
Otherwise fail with a ValueError
596+
If the store contains pandas native tables, it will return their names.
597+
Otherwise the list of names of HDF5 Table objects will be returned.
603598
604599
Returns
605600
-------
606601
list
607602
List of ABSOLUTE path-names (e.g. have the leading '/').
608-
609-
Raises
610-
------
611-
raises ValueError if kind has an illegal value
612603
"""
613-
if kind == "pandas":
614-
return [n._v_pathname for n in self.groups()]
604+
# if kind == "pandas":
605+
objects = [n._v_pathname for n in self.groups()]
606+
if objects:
607+
return objects
615608

616-
if kind == "tables":
617-
self._check_if_open()
618-
return [
619-
n._v_pathname for n in self._handle.walk_nodes("/", classname="Table")
620-
]
621-
raise ValueError(f"`kind` should be either 'pandas' or 'table' but is [{kind}]")
609+
self._check_if_open()
610+
return [
611+
n._v_pathname for n in self._handle.walk_nodes("/", classname="Table")
612+
]
622613

623614
def __iter__(self):
624615
return iter(self.keys())

pandas/tests/io/pytables/test_store.py

+4-3
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)