summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-04-27 17:27:55 -0600
committerGitHub <noreply@github.com>2017-04-27 17:27:55 -0600
commita495bb9a8bdd8e30372d9dcf7e3ca777a8670ab2 (patch)
tree4c591d52830be60f38bf17b0201596c0cef29d9c /numpy/lib
parent90718401efc0206af894628dc4e70f3b9413748e (diff)
parentae3d1fa3e0f4a048618cb4e7c8fd694185df69b6 (diff)
downloadnumpy-a495bb9a8bdd8e30372d9dcf7e3ca777a8670ab2.tar.gz
Merge pull request #8988 from eric-wieser/document-2522
DOC: Explain the behavior of diff on unsigned types
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/function_base.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 4a07815e8..3c39d1a7b 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -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])