From e08eced7990fbdcecb2bd81d3fc736f69bad6dfd Mon Sep 17 00:00:00 2001 From: Allan Haldane Date: Sun, 10 Jun 2018 21:54:21 -0400 Subject: MAINT: push back multifield copy->view changes to 1.16 --- numpy/doc/structured_arrays.py | 74 ++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 18 deletions(-) (limited to 'numpy/doc/structured_arrays.py') diff --git a/numpy/doc/structured_arrays.py b/numpy/doc/structured_arrays.py index ba667da59..ab97c5df6 100644 --- a/numpy/doc/structured_arrays.py +++ b/numpy/doc/structured_arrays.py @@ -133,10 +133,9 @@ summary they are: Offsets may be chosen such that the fields overlap, though this will mean that assigning to one field may clobber any overlapping field's data. As - an exception, fields of :class:`numpy.object` type .. (see - :ref:`object arrays `) cannot overlap with other fields, - because of the risk of clobbering the internal object pointer and then - dereferencing it. + an exception, fields of :class:`numpy.object` type cannot overlap with + other fields, because of the risk of clobbering the internal object + pointer and then dereferencing it. The optional 'aligned' value can be set to ``True`` to make the automatic offset computation use aligned offsets (see :ref:`offsets-and-alignment`), @@ -235,6 +234,11 @@ If the offsets of the fields and itemsize of a structured array satisfy the alignment conditions, the array will have the ``ALIGNED`` :ref:`flag ` set. +A convenience function :func:`numpy.lib.recfunctions.repack_fields` converts an +aligned dtype or array to a packed one and vice versa. It takes either a dtype +or structured ndarray as an argument, and returns a copy with fields re-packed, +with or without padding bytes. + .. _titles: Field Titles @@ -396,27 +400,61 @@ typically a non-structured array, except in the case of nested structures. Accessing Multiple Fields ``````````````````````````` -One can index a structured array with a multi-field index, where the index is a -list of field names:: +One can index and assign to a structured array with a multi-field index, where +the index is a list of field names. + +.. warning:: + The behavior of multi-field indexes will change from Numpy 1.15 to Numpy + 1.16. - >>> a = np.zeros(3, dtype=[('a', 'i8'), ('b', 'i4'), ('c', 'f8')]) +In Numpy 1.16, the result of indexing with a multi-field index will be a view +into the original array, as follows:: + + >>> a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'i4'), ('c', 'f4')]) >>> a[['a', 'c']] - array([(0, 0.0), (0, 0.0), (0, 0.0)], - dtype={'names':['a','c'], 'formats':['>> a[['a','c']].view('i8') # will fail in Numpy 1.16 + ValueError: When changing to a smaller dtype, its size must be a divisor of the size of original dtype + + will need to be changed. This code has raised a ``FutureWarning`` since + Numpy 1.12. + + The following is a recommended fix, which will behave identically in Numpy + 1.15 and Numpy 1.16:: + + >>> from numpy.lib.recfunctions import repack_fields + >>> repack_fields(a[['a','c']]).view('i8') # supported 1.15 and 1.16 + array([0, 0, 0]) + +Assigning to an array with a multi-field index will behave the same in Numpy +1.15 and Numpy 1.16. In both versions the assignment will modify the original +array:: + >>> a[['a', 'c']] = (2, 3) >>> a array([(2, 0, 3.0), (2, 0, 3.0), (2, 0, 3.0)], dtype=[('a', '>> a[['a', 'c']] = a[['c', 'a']] -- cgit v1.2.1