Skip to content

[Python] Weird exception when slicing an array with non-trivial step #46606

Closed

Description

@pitrou

Describe the bug, including details regarding any error messages, version, and platform.

I don't know if we can easily support this, but it should at least fail with a more readable error:

>>> a = pa.array([1.2, 3.5, None])
>>> a
<pyarrow.lib.DoubleArray object at 0x7d05b7f25900>
[
  1.2,
  3.5,
  null
]
>>> a[::-1]
Traceback (most recent call last):
  Cell In[4], line 1
    a[::-1]
  File pyarrow/array.pxi:1523 in pyarrow.lib.Array.__getitem__
  File pyarrow/array.pxi:587 in pyarrow.lib._normalize_slice
AttributeError: 'NoneType' object has no attribute 'arange'

Component(s)

Python

Activity

  1. pitrou commented on May 27, 2025

    @pitrou
    MemberAuthor
  2. AlenkaF commented on May 27, 2025

    @AlenkaF
    Member

    Hm, I get a different behaviour:

    Python 3.12.10 (main, Apr  8 2025, 11:35:47) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pyarrow as pa
    >>> a = pa.array([1.2, 3.5, None])
    >>> a
    <pyarrow.lib.DoubleArray object at 0x10c9747c0>
    [
      1.2,
      3.5,
      null
    ]
    >>> a[-1:]
    <pyarrow.lib.DoubleArray object at 0x10c974820>
    [
      null
    ]
    >>> a[::-1]
    <pyarrow.lib.DoubleArray object at 0x10c9748e0>
    [
      null,
      3.5,
      1.2
    ]
    >>> pa.__version__
    '20.0.0.dev356+g7c820dcbda.d20250520'
  3. pitrou commented on May 27, 2025

    @pitrou
    MemberAuthor

    It seems the error happens when NumPy is not installed (arange is a NumPy function).

    (@raulcd FYI)

  4. AlenkaF commented on May 27, 2025

    @AlenkaF
    Member

    Ah yes. So the fix would be updating _normalize_slice:

    def _normalize_slice(object arrow_obj, slice key):
    """
    Slices with step not equal to 1 (or None) will produce a copy
    rather than a zero-copy view
    """
    cdef:
    Py_ssize_t start, stop, step
    Py_ssize_t n = len(arrow_obj)
    start, stop, step = key.indices(n)
    if step != 1:
    indices = np.arange(start, stop, step)
    return arrow_obj.take(indices)
    else:
    length = max(stop - start, 0)
    return arrow_obj.slice(start, length)

    so that it doesn't use numpy. We could use list(range(start, stop, step)) instead of np.arange(start, stop, step).
    @thisisnic this could be a nice next issue when/if you have the capacity.

  5. added a commit that references this issue on Jun 10, 2025
  6. added this to the 21.0.0 milestone on Jun 10, 2025
  7. raulcd commented on Jun 10, 2025

    @raulcd
    Member

    Issue resolved by pull request 46732
    #46732

  8. added a commit that references this issue on Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions