Skip to content

BUG: numpy.roll() does not work with unsigned shift value #27282

@epmojo

Description

@epmojo

Describe the issue:

Calling numpy.roll() will raise an unexpected exception if the shift argument is an unsigned int or a tuple with an unsigned int.

This issue occurs with numpy v2.0.1 and 2.1.0, but does not occur on numpy 1.26.4 (edit: does not with a uint64, but works with other unsigned int types).

Reproduce the code example:

import numpy as np

x = np.arange(10)
y = np.roll(x, np.uint64(1)) # raises an exception

Error message:

C:\Python312-64\Lib\site-packages\numpy\_core\numeric.py:1282: RuntimeWarning: overflow encountered in scalar negative
  rolls[ax] = ((slice(None, -offset), slice(offset, None)),
C:\Python312-64\Lib\site-packages\numpy\_core\numeric.py:1283: RuntimeWarning: overflow encountered in scalar negative
  (slice(-offset, None), slice(None, offset)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python312-64\Lib\site-packages\numpy\_core\numeric.py", line 1265, in roll
    return roll(a.ravel(), shift, 0).reshape(a.shape)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312-64\Lib\site-packages\numpy\_core\numeric.py", line 1288, in roll
    result[res_index] = a[arr_index]
    ~~~~~~^^^^^^^^^^^
ValueError: could not broadcast input array from shape (10,) into shape (9,)

Python and NumPy Versions:

Python: 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)]
Numpy: 2.1.0

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions