assign_coords adds coordinates without a new dimension if the value is scalar
See original GitHub issueProblem
assign_corods
correctly disallows creating a new dimension when assigning list
-like coords with a name that does not match an existing dimension. However, it does allow this operation if the value is scalar.
MCVE
Consider the following DataArray
:
>>> coords = {"fruit": ("x", ["apple", "banana"])}
>>> arr = xr.DataArray([[1, 2, 3], [4, 5, 6]], dims=("x", "y"), coords=coords)
>>> arr
<xarray.DataArray (x: 2, y: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Coordinates:
fruit (x) <U6 'apple' 'banana'
Dimensions without coordinates: x, y
I can assign new coordinates to an existing dimension:
>>> arr.assign_coords(color=("x", ["red", "yellow"]))
<xarray.DataArray (x: 2, y: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Coordinates:
fruit (x) <U6 'apple' 'banana'
color (x) <U6 'red' 'yellow'
Dimensions without coordinates: x, y
And I cannot (correctly) assign coordinates to a new (nonexistent) dimension:
>>> arr.assign_coords(color=["red", "yellow"])
...
ValueError: cannot add coordinates with new dimensions to a DataArray
The above fails because Xarray, in the absence of an explicit dimension, tries to assign the new coordinates to a color
dimension which does not exist. So far so good. But why does this work?
>>> arr = arr.assign_coords(color="red")
>>> arr
<xarray.DataArray (x: 2, y: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Coordinates:
fruit (x) <U6 'apple' 'banana'
color <U3 'red'
Dimensions without coordinates: x, y
I would expect this to fail because color
is not a dimension. But these appear to be newly added coordinates without a dimension?
>>> arr.coords
Coordinates:
fruit (x) <U6 'apple' 'banana'
color <U3 'red'
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
xarray.Dataset.assign_coords
If the values are not callable, (e.g. a DataArray , scalar, or array), they are simply assigned. A new coordinate can also be...
Read more >xarray assign_coords does not work to set new coordinates
I have a NetCDF file and trying to use xarray to read a variable (with no attributes and coordinates) then ...
Read more >xarray.DataArray.assign_coords — xarray 0.14.1 documentation
xarray.DataArray.assign_coords¶ ... Assign new coordinates to this object. Returns a new object with all the original data in addition to the new coordinates....
Read more >Scalar non-dimension coords forget their heritage · Issue #4501
When a dimension is squeezed or selected with a scalar, the associated non-dimension coordinates are unassociated from the dimension. When the ...
Read more >Python Examples of xarray.Dataset - ProgramCreek.com
Returns ------- Root Mean Squared Error Single value or tuple of Dataset, ... DataArray, GroupBy, Variable, numpy/dask arrays or scalars Mix of labeled ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I see. This is because coordinates are just
DataArray
objects. Sois equivalent to
And the reason this is a
ValueError
is thatnew_coords
has a default dimensiondim_0
that is not onarr
. However, thisis equivalent to…
And
new_coords
has no dimensions that are not onarr
.This was intentional.
array.assign_coords(name=value)
should be equivalent toarray = array.copy(deep=False); array.coords[name] = value
.You are allowed to add new coordinates to a DataArray if they share existing dimensions. You are not allowed to add coordinates with new dimensions, because it is enforced as an invariant of the DataArray data model that all coordinate dimensions are found on the DataArray variable as well.