Skip to content

Commit 55dc32d

Browse files
committed
BUG: Fix pandas-dev#57608: queries on categorical string columns in
HDFStore.select() return unexpected results. In function __init__() of class Selection (pandas/core/io/pytables.py), the method self.terms.evaluate() was not returning the correct value for the where condition. The issue stemmed from the function convert_value() of class BinOp (pandas/core/computation/pytables.py), where the function searchedsorted() did not return the correct index when matching the where condition in the metadata (categories table). Replacing searchsorted() with np.where() resolves this issue.
1 parent 7e4d306 commit 55dc32d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ I/O
736736
- Bug in :meth:`DataFrame.to_stata` when writing more than 32,000 value labels. (:issue:`60107`)
737737
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
738738
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
739+
- Bug in :meth:`HDFStore.select` causing queries on categorical string columns to return unexpected results (:issue:`57608`)
739740
- Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`)
740741
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
741742
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)

pandas/core/computation/pytables.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ def stringify(value):
239239
if conv_val not in metadata:
240240
result = -1
241241
else:
242-
result = metadata.searchsorted(conv_val, side="left")
242+
# Find the index of the first match of conv_val in metadata
243+
result = np.where(metadata == conv_val)[0][0]
243244
return TermValue(result, result, "integer")
244245
elif kind == "integer":
245246
try:

pandas/tests/io/pytables/test_store.py

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
timedelta_range,
2626
)
2727
import pandas._testing as tm
28+
from pandas.api.types import (
29+
CategoricalDtype,
30+
)
2831
from pandas.conftest import has_pyarrow
2932
from pandas.tests.io.pytables.common import (
3033
_maybe_remove,
@@ -1106,3 +1109,23 @@ def test_store_bool_index(tmp_path, setup_path):
11061109
df.to_hdf(path, key="a")
11071110
result = read_hdf(path, "a")
11081111
tm.assert_frame_equal(expected, result)
1112+
1113+
1114+
@pytest.mark.parametrize("model", ["name", "longname", "verylongname"])
1115+
def test_select_categorical_string_columns(tmp_path, model):
1116+
# Corresponding to BUG: 57608
1117+
1118+
path = tmp_path / "test.h5"
1119+
1120+
models = CategoricalDtype(categories=["name", "longname", "verylongname"])
1121+
df = DataFrame(
1122+
{"modelId": ["name", "longname", "longname"], "value": [1, 2, 3]}
1123+
).astype({"modelId": models, "value": int})
1124+
1125+
with HDFStore(path, "w") as store:
1126+
store.append("df", df, data_columns=["modelId"])
1127+
1128+
with HDFStore(path, "r") as store:
1129+
result = store.select("df", "modelId == model")
1130+
expected = df[df["modelId"] == model]
1131+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)