Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 11df95e

Browse filesBrowse files
committed
DOC: clarify int type constraint in numpy.bincount
1 parent d79fe43 commit 11df95e
Copy full SHA for 11df95e

File tree

1 file changed

+15
-1
lines changed
Filter options

1 file changed

+15
-1
lines changed

‎numpy/core/multiarray.py

Copy file name to clipboardExpand all lines: numpy/core/multiarray.py
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ def bincount(x, weights=None, minlength=None):
945945
>>> np.bincount(x).size == np.amax(x)+1
946946
True
947947
948-
The input array needs to be of integer dtype, otherwise a
948+
The input array needs to be of the native integer index type, otherwise a
949949
TypeError is raised:
950950
951951
>>> np.bincount(np.arange(5, dtype=float))
@@ -954,6 +954,20 @@ def bincount(x, weights=None, minlength=None):
954954
TypeError: Cannot cast array data from dtype('float64') to dtype('int64')
955955
according to the rule 'safe'
956956
957+
This also means that the following code will not work on a 32-bit system:
958+
959+
>>> import sys
960+
>>> if sys.maxsize <= 2**32:
961+
... np.bincount(np.arange(5, dtype=np.int64))
962+
... # TypeError: Cannot cast array data from dtype('int64') to
963+
... # dtype('int32') according to the rule 'safe'
964+
965+
The easiest way to solve this is by always using the Python int type
966+
(or np.int_):
967+
968+
>>> np.bincount(np.arange(5, dtype=int))
969+
array([1, 1, 1, 1, 1])
970+
957971
A possible use of ``bincount`` is to perform sums over
958972
variable-size chunks of an array, using the ``weights`` keyword.
959973

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.