Skip to content
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

.format does not play well with a 0-D NumPy array #7978

Closed
MechCoder opened this Issue Aug 26, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@MechCoder
Copy link
Contributor

MechCoder commented Aug 26, 2016

Hi,

# This works
a = "%0.3g" % np.array(2)
a
'2'
# This does not
a = "{0:0.3g}".format(np.array(2))
TypeError: non-empty format string passed to object.__format__

Is this a bug?

@fmaussion

This comment has been minimized.

Copy link

fmaussion commented Nov 3, 2016

Just ran into this as well. This is quite bad, isn't it?

@MechCoder

This comment has been minimized.

Copy link
Contributor Author

MechCoder commented Nov 5, 2016

Not as bad as using a 0-D NumPy array in the first place :P

But yeah, this should be fixed.

@fmaussion

This comment has been minimized.

Copy link

fmaussion commented Nov 5, 2016

0D arrays are a standard output in xarray:

In [1]: import xarray as xr

In [2]: '{:.2g}'.format(xr.DataArray([1, 2, 3]).mean())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-e521944932df> in <module>()
----> 1 '{:.2g}'.format(xr.DataArray([1, 2, 3]).mean())

TypeError: non-empty format string passed to object.__format__

(CC @shoyer just fyi)

eric-wieser added a commit to eric-wieser/numpy that referenced this issue Oct 18, 2017

ENH: Implement ndarray.__format__ for 0d arrays
This fixes numpygh-7978

The behavior for other sized arrays is left unchanged, pending discussion in numpygh-5543

theodoregoetz added a commit to theodoregoetz/numpy that referenced this issue Oct 23, 2017

ENH: Implement ndarray.__format__ for 0d arrays
This fixes numpygh-7978

The behavior for other sized arrays is left unchanged, pending discussion in numpygh-5543
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.