Skip to content

Commit

Permalink
Merge pull request numpy#8988 from eric-wieser/document-2522
Browse files Browse the repository at this point in the history
DOC: Explain the behavior of diff on unsigned types
  • Loading branch information
charris authored Apr 27, 2017
2 parents 9071840 + ae3d1fa commit a495bb9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,23 @@ def diff(a, n=1, axis=-1):
will contain `False` when consecutive elements are the same and
`True` when they differ.
For unsigned integer arrays, the results will also be unsigned. This should
not be surprising, as the result is consistent with calculating the
difference directly:
>>> u8_arr = np.array([1, 0], dtype=np.uint8)
>>> np.diff(u8_arr)
array([255], dtype=uint8)
>>> u8_arr[1,...] - u8_arr[0,...]
array(255, np.uint8)
If this is not desirable, then the array should be cast to a larger integer
type first:
>>> i16_arr = u8_arr.astype(np.int16)
>>> np.diff(i16_arr)
array([-1], dtype=int16)
Examples
--------
>>> x = np.array([1, 2, 4, 7, 0])
Expand Down

0 comments on commit a495bb9

Please sign in to comment.