<h3 dir="auto">Reproducing code example:</h3>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
x, y = np.array([-969100.0]), np.array([-4457000.0])

# This works
&quot;%.4g, %.4g&quot; % (x, y)

# This errors
&quot;{:.4g}, {:.4g}&quot;.format(x, y)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">x</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">-</span><span class="pl-c1">969100.0</span>]), <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-c1">-</span><span class="pl-c1">4457000.0</span>])

<span class="pl-c"># This works</span>
<span class="pl-s">"%.4g, %.4g"</span> <span class="pl-c1">%</span> (<span class="pl-s1">x</span>, <span class="pl-s1">y</span>)

<span class="pl-c"># This errors</span>
<span class="pl-s">"{:.4g}, {:.4g}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">x</span>, <span class="pl-s1">y</span>)</pre></div>

<h3 dir="auto">Error message:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
TypeError: unsupported format string passed to numpy.ndarray.__format__"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: unsupported format string passed to numpy.ndarray.__format__
</code></pre></div>


<h3 dir="auto">NumPy/Python version information:</h3>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.15.4 3.7.1 (default, Nov  6 2018, 18:45:35)
[Clang 10.0.0 (clang-1000.11.45.5)]"><pre class="notranslate"><code class="notranslate">1.15.4 3.7.1 (default, Nov  6 2018, 18:45:35)
[Clang 10.0.0 (clang-1000.11.45.5)]
</code></pre></div>
<p dir="auto">OS: macOS High Sierra and Linux (Travis CI)</p>
<p dir="auto">This happened when upgrading a codebase using the <a href="https://github.com/asottile/pyupgrade">pyupgrade</a> tool. <code class="notranslate">%g</code> and <code class="notranslate">{:g}</code> work the same for builtin types. Is this a NumPy bug?</p>
<p dir="auto">In many wonderful cases an ndarray can be used in place of a Python float and Just Work.</p>
<p dir="auto">But not in one case:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np

n = 1.23
print('{0:.6} AU'.format(n))

n = np.array([1.23, 4.56])
print('{0:.6} AU'.format(n))"><pre class="notranslate"><code class="notranslate">import numpy as np

n = 1.23
print('{0:.6} AU'.format(n))

n = np.array([1.23, 4.56])
print('{0:.6} AU'.format(n))
</code></pre></div>
<p dir="auto">The output of the above code, at least under Python 3.4, is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.23 AU
Traceback (most recent call last):
  File &quot;tmp9.py&quot;, line 7, in &lt;module&gt;
    print('{0:.6} AU'.format(n))
TypeError: non-empty format string passed to object.__format__"><pre class="notranslate"><code class="notranslate">1.23 AU
Traceback (most recent call last):
  File "tmp9.py", line 7, in &lt;module&gt;
    print('{0:.6} AU'.format(n))
TypeError: non-empty format string passed to object.__format__
</code></pre></div>
<p dir="auto">It would be a great convenience if the ndarray grew a <code class="notranslate">__format__()</code> method that understood the tiny mini-language of float formatting, and used the number of digits of precision specified there to make its own call to the standard NumPy vector array formatting. Users could control array appearance on the screen using a Python standard that many programmers already understand.</p>