Skip to content

Commit 579e89c

Browse files
committed
Merge pull request numpy#2964 from certik/doc2542
DOC: document the casting rules changes
2 parents 563e754 + 13371fd commit 579e89c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

doc/release/1.7.0-notes.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,44 @@ and so the collapsing process only continues so long as it encounters other
213213
``matrix`` objects. It considers ``c``, ``b``, and ``a`` in that order, and
214214
``b`` is the last entry in that list which is a ``matrix`` object.
215215

216+
Casting Rules
217+
-------------
218+
219+
Casting rules have undergone some changes in corner cases, due to the
220+
NA-related work. In particular for combinations of scalar+scalar:
221+
222+
* the `longlong` type (`q`) now stays `longlong` for operations with any other
223+
number (`? b h i l q p B H I`), previously it was cast as `int_` (`l`). The
224+
`ulonglong` type (`Q`) now stays as `ulonglong` instead of `uint` (`L`).
225+
226+
* the `timedelta64` type (`m`) can now be mixed with any integer type (`b h i l
227+
q p B H I L Q P`), previously it raised `TypeError`.
228+
229+
For array + scalar, the above rules just broadcast except the case when
230+
the array and scalars are unsigned/signed integers, then the result gets
231+
converted to the array type (of possibly larger size) as illustrated by the
232+
following examples::
233+
234+
>>> (np.zeros((2,), dtype=np.uint8) + np.int16(257)).dtype
235+
dtype('uint16')
236+
>>> (np.zeros((2,), dtype=np.int8) + np.uint16(257)).dtype
237+
dtype('int16')
238+
>>> (np.zeros((2,), dtype=np.int16) + np.uint32(2**17)).dtype
239+
dtype('int32')
240+
241+
Whether the size gets increased depends on the size of the scalar, for
242+
example::
243+
244+
>>> (np.zeros((2,), dtype=np.uint8) + np.int16(255)).dtype
245+
dtype('uint8')
246+
>>> (np.zeros((2,), dtype=np.uint8) + np.int16(256)).dtype
247+
dtype('uint16')
248+
249+
Also a ``complex128`` scalar + ``float32`` array is cast to ``complex64``.
250+
251+
In NumPy 1.7 the `datetime64` type (`M`) must be constructed by explicitly
252+
specifying the type as the second argument (e.g. ``np.datetime64(2000, 'Y')``).
253+
216254

217255
Deprecations
218256
============

0 commit comments

Comments
 (0)