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

Skip to content

BUG: Inconsistent behavior with overflows #30215

@Thijss

Description

@Thijss

Describe the issue:

I noticed that numpy arrays (ndarray/structured) handle overflow errors on assignment differently depending on the type of the value assigned.
Example with integers:

  • If the assigned value is also a numpy array, the assigned value wraps around (truncates).
  • If the assigned value is a Python integer, an OverflowError is raised.

I have two questions on this:

  • Is this intended behavior?
  • If yes, is there documentation on this? I couldn't find any

Reproduce the code example:

import numpy as np

# example 1:
x = np.zeros(10, dtype=np.int8)
x[0] = np.array(500)
assert x[0] == -12

x[0] = 500  # --> raises OverflowError


# example 2 (structured array incl. list assignment)
x = np.array([('Rex', 9), ('Fido', 3)], dtype=[('name', 'U10'), ('age', np.int8)])

# broadcast
x["age"] = np.array(500)
assert np.array_equal([-12, -12], x['age'])

x["age"] = 500  # --> raises OverflowError

# update by array/list
x["age"] = np.array([500, 501])
assert np.array_equal([-12, -11], x['age'])

x["age"] = [500, 501]  # --> raises OverflowError

Error message:

Python and NumPy Versions:

Python 3.12.11
Numpy 2.3.4

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions