-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
formatting of singleton DataArrays #2791
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
Comments
Yes, I think this would be a nice addition. This would entail implementing a |
I agree that it is a bit annoying that 1d DataArray prints much information especially we want to embed the value into a string. |
On the one hand I agree, but note that the same behavior works for numpy arrays import numpy as np
a=np.array([1,2,3,4])
' '.join('{:d}'.format(v) for v in a)
# prints '1 2 3 4' |
Here's a related NumPy issue: numpy/numpy#5543 I guess there are two possible behaviors for
These behaviors would definitely conflict for scalar objects -- in the second case, we would still want to include some indication that it's an |
@yohai , sorry, I misunderstood
as I feel it more consistent with the existing xarray I sometimes want a 0d-dataarray to behave as a native scalar. I am a bit worrying if printing 0d-dataarray as a scalar would confuse me as it is a scalar not a 0d-array. |
I tend towards the former, to coerce singleton arrays to behave as scalars of their
@shoyer I don't see why would that be the case. If I format something as |
To make things concrete, the solution that I have in mind is as simple as adding this function to def __format__(self, format_spec):
return self.values.__format__(format_spec) Here's one use case I have encountered: ds=xr.Dataset({'A':(['x','y','z'], np.random.rand(40,40,3)),
'B':(['z'], np.random.randn(3))},
coords={'z':[31,42,45]})
fg=ds.A.plot(col='z')
for ax, d in zip(fg.axes.flat, fg.name_dicts.flat):
t=ax.get_title()
ax.set_title('{} and B(z)={:1.2}'.format(t, ds.sel(**d).B)) This way, if you want to vectorize a ar = xr.DataArray([39, 103, id(xr)])
print('{:3.3f} {:3.3e} {:x}'.format(*ar))
#prints `39.000 1.030e+02 10e5bb548` |
This seems to work now, closing |
Uh oh!
There was an error while loading. Please reload this page.
Code Sample, a copy-pastable example if possible
Problem description
I think it would be useful for
repr(a)
to returnrepr(a.values)
whena.ndim==0
and perhaps also whena.size==1
. For example, this would make such code work:while currently one has to write
I tried to think whether this will break something else but I couldn't think of anything.
The text was updated successfully, but these errors were encountered: