question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

assign_coords adds coordinates without a new dimension if the value is scalar

See original GitHub issue

Problem

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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gwgundersencommented, Aug 25, 2019

I see. This is because coordinates are just DataArray objects. So

>>> arr.coords['y'] = range(3)

is equivalent to

>>> new_coords = xr.DataArray(data=range(3))
>>> arr.coords['y'] = new_coords

And the reason this is a ValueError is that new_coords has a default dimension dim_0 that is not on arr. However, this

>>> arr.coords['y'] = 1

is equivalent to…

>>> new_coords = xr.DataArray(data=1, dims=[])
>>> arr.coords['y'] = new_coords

And new_coords has no dimensions that are not on arr.

1reaction
shoyercommented, Aug 23, 2019

This was intentional. array.assign_coords(name=value) should be equivalent to array = 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found