Skip to content

Commit 386b566

Browse files
committed
BUG: Follow-up for pandas-dev#57608: check if metadata is sorted before search
1 parent 55dc32d commit 386b566

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pandas/core/computation/pytables.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,13 @@ def stringify(value):
239239
if conv_val not in metadata:
240240
result = -1
241241
else:
242-
# Find the index of the first match of conv_val in metadata
243-
result = np.where(metadata == conv_val)[0][0]
242+
# Check if metadata is sorted
243+
if np.all(metadata[:-1] <= metadata[1:]):
244+
# If it is, use searchsorted for efficient lookup
245+
result = metadata.searchsorted(conv_val, side="left")
246+
else:
247+
# Find the index of the first match of conv_val in metadata
248+
result = np.flatnonzero(metadata == conv_val)[0]
244249
return TermValue(result, result, "integer")
245250
elif kind == "integer":
246251
try:

0 commit comments

Comments
 (0)