-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
xarray.DataArray.expand_dims() can only expand dimension for a point coordinate #2710
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, this feels in scope for
I think we would want both to be supported -- you should not be required to supply coordinate labels in order to expand to a dimension of size > 1. We can imagine the first being spelled like
This feels a little different from I would probably write this with
|
Hi, Would it be alright if I opened a PR sometime soon that upgraded I would use your suggested API, i.e. not requiring explicit coordinate names -- that makes sense. However, it feels like the dimension kwargs (i.e. the new dimension/dimensions), should be allowed to be given implicit or explicit coordinates, in case the user doesn't want 0-based integer coordinates for the new dimension. For example,
is equivalent to
but this will also work
where
Does that make sense? Thank you! |
|
Those would be equivalent, I think, assuming they're both manipulating the same |
Well then I think they should be different. Currently,
i.e. in this last case, the user has specified co-ordinate labels and so the returned DataArray has a new co-ordinate a. |
Oh I see what you're saying. Yeah, that makes sense. To get the equivalent of
|
Yes, that sounds welcome to me! I think much of the underlying logic should already exist on the |
Unfortunately this most recent change has broken my workflow. I was using import xarray as xr
da = xr.DataArray([0,1,2], dims=['dim1'], coords={'dim1':['a','b','c']})
print(da.dims) # returns ('dim1',)
da = da.sel({'dim1':'a'})
print(da.dims) # returns ()
da = da.expand_dims(da.coords) # fails in 0.12.1
print(da.dims) # returns ('dim1',) in 0.12.0 |
@barkls I think Previously, we only used the argument to I don't know what the best resolution would be here, but this seems to be a hazard of duck-typing. I did not anticipate that some users would already be iterating over mappings like |
Another solution could be adding support for |
Or equivalently, you could just do
|
@pletchm that is the solution I found as well. Thanks all for the suggestions! |
Yes, @TomNicholas. My PR got merged but I forgot to close the issue -- closing it now. Thanks for checking. |
Current
expand_dims
functionalityApparently,
expand_dims
can only create a dimension for a point coordinate, i.e. it promotes a scalar coordinate into 1D coordinate. Here is an example:Problem description
I want to be able to do 2 more things with
expand_dims
or maybe a related/similar method:Here is the code I currently use to accomplish this
Here's an example of broadcasting data across a new dimension:
Here's an example of expanding an existing dimension to include new coordinates:
Final Note
If no one else is already working on this, and if it seems like a useful addition to XArray, then I would more than happy to work on this. Please let me know.
Thank you,
Martin
The text was updated successfully, but these errors were encountered: