diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-12-08 13:01:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-08 13:01:51 -0700 |
commit | 5f01e54b20e38d483e8bab31bf5f953a860fe8d3 (patch) | |
tree | b4997b30e66aefed07e56dd2ff8baeb563b2545a | |
parent | 1c86cdd469a5034cb136130722a58adf062056fd (diff) | |
parent | 4b49e994e9e2ad53f7f2c98af22605350ceb87b4 (diff) | |
download | numpy-5f01e54b20e38d483e8bab31bf5f953a860fe8d3.tar.gz |
Merge pull request #10177 from eric-wieser/1.14-notes-update
Final 1.14 formatting fixes
-rw-r--r-- | doc/release/1.14.0-notes.rst | 66 | ||||
-rw-r--r-- | numpy/core/records.py | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_records.py | 11 | ||||
-rw-r--r-- | numpy/ma/core.py | 6 |
4 files changed, 66 insertions, 27 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst index cc34e6710..793b20c6d 100644 --- a/doc/release/1.14.0-notes.rst +++ b/doc/release/1.14.0-notes.rst @@ -253,37 +253,59 @@ enabling the new 1.13 "legacy" printing mode. This is enabled by calling In summary, the major changes are: -* The ``repr`` of float arrays often omits a whitespace previously printed - in the sign position. See the new ``sign`` option to ``np.set_printoptions``. -* Floating-point arrays and scalars use a new algorithm for decimal - representations, giving the shortest unique representation. This will - usually shorten ``float16`` fractional output, and sometimes ``float32`` and - ``float128`` output. ``float64`` should be unaffected. See the new - ``floatmode`` option to ``np.set_printoptions``. -* Float arrays printed in scientific notation no longer use fixed-precision, - and now instead show the shortest unique representation. -* The ``str`` of floating-point scalars is no longer truncated in python2. -* Non-finite complex scalars print like ``nanj`` instead of ``nan*j``. +* For floating-point types: + + * The ``repr`` of float arrays often omits a space previously printed + in the sign position. See the new ``sign`` option to ``np.set_printoptions``. + * Floating-point arrays and scalars use a new algorithm for decimal + representations, giving the shortest unique representation. This will + usually shorten ``float16`` fractional output, and sometimes ``float32`` and + ``float128`` output. ``float64`` should be unaffected. See the new + ``floatmode`` option to ``np.set_printoptions``. + * Float arrays printed in scientific notation no longer use fixed-precision, + and now instead show the shortest unique representation. + * The ``str`` of floating-point scalars is no longer truncated in python2. + +* For other data types: + + * Non-finite complex scalars print like ``nanj`` instead of ``nan*j``. + * ``NaT`` values in datetime arrays are now properly aligned. + * Arrays and scalars of ``np.void`` datatype are now printed using hex + notation. + +* For line-wrapping: + + * The "dtype" part of ndarray reprs will now be printed on the next line + if there isn't space on the last line of array output. + * The ``linewidth`` format option is now always respected. + The `repr` or `str` of an array will never exceed this, unless a single + element is too wide. + * All but the last line of array strings will contain the same number of + elements. + * The last line of an array string will never have more elements than earlier + lines. + +* For summarization (the use of ``...`` to shorten long arrays): + + * A trailing comma is no longer inserted for ``str``. + Previously, ``str(np.arange(1001))`` gave + ``'[ 0 1 2 ..., 998 999 1000]'``, which has an extra comma. + * For arrays of 2-D and beyond, when ``...`` is printed on its own line in + order to summarize any but the last axis, newlines are now appended to that + line to match its leading newlines and a trailing space character is + removed. + * ``MaskedArray`` arrays now separate printed elements with commas, always print the dtype, and correctly wrap the elements of long arrays to multiple lines. If there is more than 1 dimension, the array attributes are now printed in a new "left-justified" printing style. -* ``NaT`` values in datetime arrays are now properly aligned. -* Arrays and scalars of ``np.void`` datatype are now printed using hex notation. +* ``recarray`` arrays no longer print a trailing space before their dtype, and + wrap to the right number of columns. * 0d arrays no longer have their own idiosyncratic implementations of ``str`` and ``repr``. The ``style`` argument to ``np.array2string`` is deprecated. * Arrays of ``bool`` datatype will omit the datatype in the ``repr``. -* The "dtype" part of ndarray reprs will now be printed on the next line - if there isn't space on the last line of array output. * User-defined ``dtypes`` (subclasses of ``np.generic``) now need to implement ``__str__`` and ``__repr__``. -* The ``...`` used to summarize long arrays now omits a trailing comma for - ``str``. Previously, ``str(np.arange(1001))`` gave - ``'[ 0 1 2 ..., 998 999 1000]'``, which has an extra comma. -* When a summarization ``...`` would be printed on its own line, e.g., for - summarization along any ndarray dimension but the last, a trailing - whitespace is now removed and trailing newlines added to match - the leading newlines. Some of these changes are described in more detail below. diff --git a/numpy/core/records.py b/numpy/core/records.py index b6ff8bf65..76783bb67 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -42,6 +42,7 @@ import os from . import numeric as sb from . import numerictypes as nt from numpy.compat import isfileobj, bytes, long +from .arrayprint import get_printoptions # All of the functions allow formats to be a dtype __all__ = ['record', 'recarray', 'format_parser'] @@ -525,22 +526,25 @@ class recarray(ndarray): if repr_dtype.type is record: repr_dtype = sb.dtype((nt.void, repr_dtype)) prefix = "rec.array(" - fmt = 'rec.array(%s, %sdtype=%s)' + fmt = 'rec.array(%s,%sdtype=%s)' else: # otherwise represent it using np.array plus a view # This should only happen if the user is playing # strange games with dtypes. prefix = "array(" - fmt = 'array(%s, %sdtype=%s).view(numpy.recarray)' + fmt = 'array(%s,%sdtype=%s).view(numpy.recarray)' # get data/shape string. logic taken from numeric.array_repr if self.size > 0 or self.shape == (0,): - lst = sb.array2string(self, separator=', ', prefix=prefix) + lst = sb.array2string( + self, separator=', ', prefix=prefix, suffix=',') else: # show zero-length shape unless it is (0,) lst = "[], shape=%s" % (repr(self.shape),) lf = '\n'+' '*len(prefix) + if get_printoptions()['legacy'] == '1.13': + lf = ' ' + lf # trailing space return fmt % (lst, lf, repr_dtype) def field(self, attr, val=None): diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 27d35fa65..73cfe3570 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -101,6 +101,17 @@ class TestFromrecords(object): assert_((mine.data1[i] == 0.0)) assert_((mine.data2[i] == 0.0)) + def test_recarray_repr(self): + a = np.array([(1, 0.1), (2, 0.2)], + dtype=[('foo', int), ('bar', float)]) + a = np.rec.array(a) + assert_equal( + repr(a), + textwrap.dedent("""\ + rec.array([(1, 0.1), (2, 0.2)], + dtype=[('foo', '<i4'), ('bar', '<f8')])""") + ) + def test_recarray_from_repr(self): a = np.array([(1,'ABC'), (2, "DEF")], dtype=[('foo', int), ('bar', 'S4')]) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index a09ec6bdb..407869362 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3937,11 +3937,13 @@ class MaskedArray(ndarray): reprs['data'] = np.array2string( self._insert_masked_print(), separator=", ", - prefix=indents['data'] + 'data=') + prefix=indents['data'] + 'data=', + suffix=',') reprs['mask'] = np.array2string( self._mask, separator=", ", - prefix=indents['mask'] + 'mask=') + prefix=indents['mask'] + 'mask=', + suffix=',') reprs['fill_value'] = repr(self.fill_value) if dtype_needed: reprs['dtype'] = np.core.arrayprint.dtype_short_repr(self.dtype) |