[Numpy-discussion] Numpy set_printoptions, silent failure, bug?

John Ladasky jladasky at itu.edu
Tue Jul 19 17:41:04 EDT 2016

Hi there,

I've been using Numpy for several years and appreciate it very much.

The following minimal code has been tried on Python 3.4 and 3.5, with Numpy
1.8 and Numpy 1.11, respectively.  I want to temporarily change the way
that a Numpy array is printed, then change it back.

import numpy as np

a = np.random.random((4,3))
print(a, "\n")
opt = np.get_printoptions()
np.set_printoptions(precision = 3, suppress = True)
print(a, "\n")
print(a, "\n\nDone.\n")

Here is the traceback:

Traceback (most recent call last):
  File "set_printoptions test.py", line 11, in <module>
    print(a, "\n\nDone.\n")
  File "/usr/lib/python3/dist-packages/numpy/core/numeric.py", line 1615,
in array_str
    return array2string(a, max_line_width, precision, suppress_small, ' ',
"", str)
  File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 454,
in array2string
    separator, prefix, formatter=formatter)
  File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 328,
in _array2string
    _summaryEdgeItems, summary_insert)[:-1]
  File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 523,
in _formatArray
  File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 497,
in _formatArray
    word = format_function(a[-i]) + separator
  File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 616,
in __call__
    s = self.format % x
AttributeError: 'FloatFormat' object has no attribute 'format'

It took me a while to discover the bug, on the second to last line.  That
line should read:


This unpacks the dictionary, opt, which was retrieved earlier in the
program with the call to get_printoptions.

I am wondering why set_printoptions would accept the dictionary as a single
argument.  Shouldn't that raise a TypeError or something?

The AttributeError that is raised the next time that one attempts to print
a numpy array is remote from the problem, and thus rather uninformative,
which is why I did not immediately diagnose my error.

Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single

*John J. Ladasky Jr., Ph.D.*
*Research Scientist*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160719/b0747aa5/attachment.html>

More information about the NumPy-Discussion mailing list