8000 DOC: clarify int type constraint in numpy.bincount by onitake · Pull Request #23552 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC: clarify int type constraint in numpy.bincount #23552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DOC: correct text as requested in review
  • Loading branch information
onitake committed May 11, 2025
commit 782351eb109a2fb0610ad655049c675557d7e0af
11 changes: 3 additions & 8 deletions numpy/_core/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,8 @@ def bincount(x, weights=None, minlength=None):
If the input is not 1-dimensional, or contains elements with negative
values, or if `minlength` is negative.
TypeError
If the type of the input is float or complex.
If the type of the input is float or complex, or larger than the
native integer type.

See Also
--------
Expand All @@ -972,7 +973,7 @@ def bincount(x, weights=None, minlength=None):
True

The input array needs have an integer dtype smaller or equal to the
native Python array index type, otherwise a TypeError is raised:
native array index type, otherwise a TypeError is raised:

>>> np.bincount(np.arange(5, dtype=float))
Traceback (most recent call last):
Expand All @@ -988,12 +989,6 @@ def bincount(x, weights=None, minlength=None):
... # TypeError: Cannot cast array data from dtype('int64') to
... # dtype('int32') according to the rule 'safe'

The easiest way to solve this is by always using the Python int type
(or np.int_):

>>> np.bincount(np.arange(5, dtype=int))
array([1, 1, 1, 1, 1])

A possible use of ``bincount`` is to perform sums over
variable-size chunks of an array, using the ``weights`` keyword.

Expand Down
Loading
0