Skip to content

Conversation

@charris
Copy link
Member

@charris charris commented Oct 23, 2025

Python treats slice(-1) differently from slice(-1, None): The first is interpreted as slice(None, -1, None), while the second becomes slice(-1, None, None), according to the logic in slice_new.

However, np.strings.slice treats these identically, as it cannot distinguish unset arguments from arguments set to None. This makes it impossible to get the last characters of each string, for example:

>>> a = np.array(['hello', 'world'])
>>> np.strings.slice(a, -2, None)  # should return last two characters
array(['hel', 'wor'], dtype='<U5')

This commit fixes that behavior:

>>> a = np.array(['hello', 'world'])
>>> np.strings.slice(a, -2, None)  # returns last characters as expected
array(['lo', 'ld'], dtype='<U5')
>>> np.strings.slice(a, -2)  # original behavior preserved if no stop
array(['hel', 'wor'], dtype='<U5')

It does this by adding a stop=np._NoValue default argument to np.strings.slice, which can be overridden with None.

This commit also adds test conditions to
numpy/_core/tests/test_strings.py::TestMethods::test_slice to verify that the slicing behavior matches Python's slice. Note that 4 newly added test conditions are commented out for now, as they cause errors with the "T" dtype. To reproduce:

>>> np.__version__
'2.3.3'
>>> a = np.array(['hello', 'world'], dtype="T")
>>> np.strings.slice(a, 5, 7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "numpy-dev/lib/python3.12/site-packages/numpy/_core/strings.py", line 1823, in slice
    return _slice(a, start, stop, step)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MemoryError: Failed to allocate string in slice

This causes either a MemoryError or kills the process with code 251.

  • BUG: Fix np.strings.slice when start and stop >= len

Allows commented test_slice conditions to be uncommented.

…y#29944)

Python treats `slice(-1)` differently from `slice(-1, None)`:
The first is interpreted as `slice(None, -1, None)`, while the second
becomes `slice(-1, None, None)`, according to the logic in `slice_new`.

However, `np.strings.slice` treats these identically, as it cannot
distinguish unset arguments from arguments set to None. This makes it
impossible to get the last characters of each string, for example:
```python
>>> a = np.array(['hello', 'world'])
>>> np.strings.slice(a, -2, None)  # should return last two characters
array(['hel', 'wor'], dtype='<U5')
```

This commit fixes that behavior:
```python
>>> a = np.array(['hello', 'world'])
>>> np.strings.slice(a, -2, None)  # returns last characters as expected
array(['lo', 'ld'], dtype='<U5')
>>> np.strings.slice(a, -2)  # original behavior preserved if no stop
array(['hel', 'wor'], dtype='<U5')
```

It does this by adding a `stop=np._NoValue` default argument to
`np.strings.slice`, which can be overridden with `None`.

This commit also adds test conditions to
`numpy/_core/tests/test_strings.py::TestMethods::test_slice`
to verify that the slicing behavior matches Python's `slice`.
Note that 4 newly added test conditions are commented out for now,
as they cause errors with the "T" dtype. To reproduce:
```
>>> np.__version__
'2.3.3'
>>> a = np.array(['hello', 'world'], dtype="T")
>>> np.strings.slice(a, 5, 7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "numpy-dev/lib/python3.12/site-packages/numpy/_core/strings.py", line 1823, in slice
    return _slice(a, start, stop, step)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MemoryError: Failed to allocate string in slice
```
This causes either a MemoryError or kills the process with code 251.

* BUG: Fix np.strings.slice when start and stop >= len

Allows commented test_slice conditions to be uncommented.
@charris charris added this to the 2.3.5 release milestone Oct 23, 2025
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Oct 23, 2025
@charris charris merged commit 0902aa6 into numpy:maintenance/2.3.x Oct 23, 2025
75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants