Skip to content

Commit f4b6e92

Browse files
BeforeFlightjreback
authored andcommitted
Read from HDF with empty where throws an error (#26746)
1 parent 67d6562 commit f4b6e92

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ I/O
666666
- Added ``cache_dates=True`` parameter to :meth:`read_csv`, which allows to cache unique dates when they are parsed (:issue:`25990`)
667667
- :meth:`DataFrame.to_excel` now raises a ``ValueError`` when the caller's dimensions exceed the limitations of Excel (:issue:`26051`)
668668
- :func:`read_excel` now raises a ``ValueError`` when input is of type :class:`pandas.io.excel.ExcelFile` and ``engine`` param is passed since :class:`pandas.io.excel.ExcelFile` has an engine defined (:issue:`26566`)
669+
- Bug while selecting from :class:`HDFStore` with ``where=''`` specified (:issue:`26610`).
669670

670671
Plotting
671672
^^^^^^^^

pandas/io/pytables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _ensure_term(where, scope_level):
9898
where = wlist
9999
elif maybe_expression(where):
100100
where = Term(where, scope_level=level)
101-
return where
101+
return where if where is None or len(where) else None
102102

103103

104104
class PossibleDataLossError(Exception):

pandas/tests/io/test_pytables.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4731,6 +4731,21 @@ def test_read_py2_hdf_file_in_py3(self, datapath):
47314731
result = store['p']
47324732
assert_frame_equal(result, expected)
47334733

4734+
@pytest.mark.parametrize("where", ["", (), (None, ), [], [None]])
4735+
def test_select_empty_where(self, where):
4736+
# GH26610
4737+
4738+
# Using keyword `where` as '' or (), or [None], etc
4739+
# while reading from HDF store raises
4740+
# "SyntaxError: only a single expression is allowed"
4741+
4742+
df = pd.DataFrame([1, 2, 3])
4743+
with ensure_clean_path("empty_where.h5") as path:
4744+
with pd.HDFStore(path) as store:
4745+
store.put("df", df, "t")
4746+
result = pd.read_hdf(store, "df", where=where)
4747+
assert_frame_equal(result, df)
4748+
47344749

47354750
class TestHDFComplexValues(Base):
47364751
# GH10447

0 commit comments

Comments
 (0)