File tree 1 file changed +15
-1
lines changed
Filter options
1 file changed +15
-1
lines changed
Original file line number Diff line number Diff line change @@ -945,7 +945,7 @@ def bincount(x, weights=None, minlength=None):
945
945
>>> np.bincount(x).size == np.amax(x)+1
946
946
True
947
947
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
949
949
TypeError is raised:
950
950
951
951
>>> np.bincount(np.arange(5, dtype=float))
@@ -954,6 +954,20 @@ def bincount(x, weights=None, minlength=None):
954
954
TypeError: Cannot cast array data from dtype('float64') to dtype('int64')
955
955
according to the rule 'safe'
956
956
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
+
957
971
A possible use of ``bincount`` is to perform sums over
958
972
variable-size chunks of an array, using the ``weights`` keyword.
959
973
You can’t perform that action at this time.
0 commit comments