8000 MAINT, BUG: Final 1.14 formatting fixes by eric-wieser · Pull Request #10177 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT, BUG: Final 1.14 formatting fixes #10177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions doc/release/1.14.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 fl 8000 oat 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" pri 8000 nting 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.

Expand Down
10 changes: 7 additions & 3 deletions numpy/core/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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']
Expand Down Expand Up @@ -525,22 +526,25 @@ def __repr__(self):
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):
Expand Down
11 changes: 11 additions & 0 deletions numpy/core/tests/test_records.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ def test_recarray_from_obj(self):
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')])
Expand Down
6 changes: 4 additions & 2 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3937,11 +3937,13 @@ def __repr__(self):
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)
Expand Down
0