summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2022-03-17 14:57:58 -0700
committerSebastian Berg <sebastian@sipsolutions.net>2022-05-09 17:13:13 +0200
commit8f8e4465bfdb2ca570cc5be3018913d39f27c76c (patch)
tree75147d6bd812c200e250a2bb87e1ecc64197186a
parent9de1ca82f835ce41c027af0f68ecdf4015809cb6 (diff)
downloadnumpy-8f8e4465bfdb2ca570cc5be3018913d39f27c76c.tar.gz
WIP: docs... Need to start a whole new "promotion" probably based on some stuff in the NEP 50 draft...
-rw-r--r--doc/source/user/basics.rec.rst33
-rw-r--r--numpy/core/_add_newdocs.py11
2 files changed, 33 insertions, 11 deletions
diff --git a/doc/source/user/basics.rec.rst b/doc/source/user/basics.rec.rst
index eec2394e9..4efe88412 100644
--- a/doc/source/user/basics.rec.rst
+++ b/doc/source/user/basics.rec.rst
@@ -556,23 +556,36 @@ Structure Comparison
If the dtypes of two void structured arrays are equal, testing the equality of
the arrays will result in a boolean array with the dimensions of the original
arrays, with elements set to ``True`` where all fields of the corresponding
-structures are equal. Structured dtypes are equal if the field names,
-dtypes and titles are the same, ignoring endianness, and the fields are in
-the same order::
+structures are equal::
- >>> a = np.zeros(2, dtype=[('a', 'i4'), ('b', 'i4')])
- >>> b = np.ones(2, dtype=[('a', 'i4'), ('b', 'i4')])
+ >>> a = np.array([(1, 1), (2, 2)], dtype=[('a', 'i4'), ('b', 'i4')])
+ >>> b = np.array([(1, 1), (2, 3)], dtype=[('a', 'i4'), ('b', 'i4')])
>>> a == b
- array([False, False])
+ array([True, False])
-Currently, if the dtypes of two void structured arrays are not equivalent the
-comparison fails, returning the scalar value ``False``. This behavior is
-deprecated as of numpy 1.10 and will raise an error or perform elementwise
-comparison in the future.
+NumPy will promote individual field datatypes to perform the comparison.
+So the following is also valid (note the ``'f4'`` dtype for the ``'a'`` field):
+
+ >>> b = np.array([(1.0, 1), (2.5, 2)], dtype=[("a", "f4"), ("b", "i4")])
+ >>> a == b
+ array([True, False])
+
+To compare two structured arrays, it must be possible to promote them to a
+common dtype as returned by `numpy.result_type` and `np.promote_types`.
+This enforces that the number of fields, the field names, and the field titles
+must match precisely.
+When promotion is not possible, for example due to mismatching field names,
+NumPy will raise an error.
The ``<`` and ``>`` operators always return ``False`` when comparing void
structured arrays, and arithmetic and bitwise operations are not supported.
+.. versionchanged::
+ Before NumPy 1.23, a warning was given and ``False`` returned when
+ promotion to a common dtype failed.
+ Further, promotion was much more restrictive: It would reject the mixed
+ float/integer comparison example above.
+
Record Arrays
=============
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index dc0285a11..ea7b96fd4 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -1808,7 +1808,8 @@ add_newdoc('numpy.core.multiarray', 'promote_types',
Returns the data type with the smallest size and smallest scalar
kind to which both ``type1`` and ``type2`` may be safely cast.
- The returned data type is always in native byte order.
+ The returned data type is always considered "canonical", this mainly
+ means that the promoted dtype will always be in native byte order.
This function is symmetric, but rarely associative.
@@ -1826,6 +1827,8 @@ add_newdoc('numpy.core.multiarray', 'promote_types',
Notes
-----
+ Please see `numpy.result_type` for additional information about promotion.
+
.. versionadded:: 1.6.0
Starting in NumPy 1.9, promote_types function now returns a valid string
@@ -1834,6 +1837,12 @@ add_newdoc('numpy.core.multiarray', 'promote_types',
dtype, even if it wasn't long enough to store the max integer/float value
converted to a string.
+ .. versionchanged:: 1.23.0
+
+ NumPy now supports promotion for more structured dtypes. It will now
+ remove unnecessary padding from a structure dtype and promote included
+ fields individually.
+
See Also
--------
result_type, dtype, can_cast