Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: np.searchsorted gives flaky results with StringDType arrays #31533

@ev-br

Description

@ev-br

Describe the issue:

np.searchsorted with string arrays sometimes gives inconsistent results when run several times:
The problem is intermittent: The script below fails quickly in main in a fresh terminal, and stops failing after several reruns. Open a new terminal, and it fails again.

Reproduce the code example:

$ cat np.search.str.py 
import numpy as np
print(np.__version__)

a = np.array(['a'*36, 'b'*36, 'c'*17], dtype='T')
res = np.searchsorted(a, 'c'*17)

for _ in range(3_000_000):
    this = np.searchsorted(a, 'c'*17)
    assert this == res, f"Failed in {_} run: {this} != {res}"

# and again
for _ in range(1000_000):
    this = np.searchsorted(a, 'c'*17)
    assert this == res, f"Failed in {_} run: {this} != {res}"

# and again
for _ in range(1000_000):
    this = np.searchsorted(a, 'c'*17)
    assert this == res, f"Failed in {_} run: {this} != {res}"

# and again
for _ in range(1000_000):
    this = np.searchsorted(a, 'c'*17)
    assert this == res, f"Failed in {_} run: {this} != {res}"

Error message:

$ spin python  np.search.str.py 
Invoking `build` prior to invoking Python:
$ /home/br/miniforge3/envs/numpy-dev/bin/python3.12 vendored-meson/meson/meson.py compile -C build
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /home/br/miniforge3/envs/numpy-dev/bin/ninja -C /home/br/repos/numpy/build
ninja: Entering directory `/home/br/repos/numpy/build'
[1/1] Generating numpy/generate-version with a custom command
Saving version to numpy/version.py
$ /home/br/miniforge3/envs/numpy-dev/bin/python3.12 vendored-meson/meson/meson.py install --only-changed -C build --destdir ../build-install
$ export PYTHONPATH="/home/br/repos/numpy/build-install/usr/lib/python3.12/site-packages"
🐍 Launching Python with PYTHONPATH="/home/br/repos/numpy/build-install/usr/lib/python3.12/site-packages"
$ /home/br/miniforge3/envs/numpy-dev/bin/python3.12 -P np.search.str.py
2.6.0.dev0+git20260527.a20ef19
Traceback (most recent call last):
  File "/home/br/repos/numpy/np.search.str.py", line 9, in <module>
    assert this == res, f"Failed in {_} run: {this} != {res}"
           ^^^^^^^^^^^
AssertionError: Failed in 0 run: 3 != 2

Python and NumPy Versions:

Observed with

2.6.0.dev0+git20260527.a20ef19
3.12.10 | packaged by conda-forge | (main, Apr 10 2025, 22:21:13) [GCC 13.3.0]

and python 3.11 with numpy versions down to 2.4.1 (it seems to fail less with 2.4.1 than 2.4.6 but that might not mean anything)

Runtime Environment:

No response

How does this issue affect you or how did you find it:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions