Reputation: 26465
I have a DataArray for which da.dims==()
. I can assign a coordinate da.assign_coords(foo=42)
. I would like to add a corresponding dimension with length one, such that da.dims==("foo",)
and the corresponding coordinate would be foo=[42]
. I cannot use assign_coords(foo=[42])
, as this results in the error message cannot add coordinates with new dimensions to a DataArray
.
How do I assign a new dimension of length one to a DataArray? I could do something like DataArray(da.values.reshape([1]), dims="foo", coords={"foo": [42]})
but I wonder if there is a method that does not require copying the entire object.
Upvotes: 1
Views: 349
Reputation: 26465
You can use xarray.concat
to achieve this:
da = xarray.DataArray(0, coords={"x": 42})
xarray.concat((da,), dim="x")
Upvotes: 1