home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 1240532341

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/7005#issuecomment-1240532341 https://api.github.com/repos/pydata/xarray/issues/7005 1240532341 IC_kwDOAMm_X85J8QV1 4160723 2022-09-08T10:32:31Z 2022-09-08T10:32:31Z MEMBER

Hi @jamesstidard,

causes a ValueError I am struggling to interpret.

I agree that the error message is not very informative. This could be improved.

The problem in your example is located here:

python def map_coords(ds, *, name, mapping): ... ds.coords[name] = xr.DataArray(new_values, coords=coord.coords) ...

Since v2022.6.0, multi-index level coordinates are real coordinates. What happens is that when you update "x_one" it invalidates the multi-index "x" - "x_one" - "x_two" in the DataArray:

```python ... map_coords(mda, name="z", mapping={0: "zero", 1: "one", 2: "two"}). # success map_coords(mda, name="x_one", mapping={"a": "aa", "b": "bb", "c": "cc"}) # success

the 'x', 'x_one' and 'x_two' indexes are all equal

mda.xindexes["x_one"].equals(mda.xindexes["x"]) and mda.xindexes["x_one"].equals(mda.xindexes["x_two"])

True

the 'x' and 'x_two' indexes are identical, but not the 'x_one' index!

mda.xindexes["x"] is mda.xindexes["x_two"]

True

mda.xindexes["x"] is mda.xindexes["x_one"]

False

```

Which explains the alignment error when you further try to update the DataArray.

I'm actually surprised that it doesn't raise any error at that point, like this:

```python mda = xr.DataArray(np.random.rand(6, 6, 3), [("x", midx), ("y", midy), ("z", range(3))]) mda.coords["x_two"] = ("x", range(6))

ValueError: cannot set or update variable(s) 'x_two', which would corrupt the following

index built from coordinates 'x', 'x_one', 'x_two':

<xarray.core.indexes.PandasMultiIndex object at 0x1689cc190>

```

It seems like a bug.

Either way, you shouldn't try updating a single coordinate that is part of a multi-coordinate index, which is assumed immutable. Instead, you could use reset_index/set_index before/after updating the labels of one level coordinate. You could also replace the whole index and its coordinates at once (example below). I wouldn't rely too much on the latter option, though, as such special case for pd.MultiIndex might eventually be deprecated in Xarray.

python new_midx = midx.set_levels(["aa", "bb", "cc"], level="x_one") mda.coords["x"] = new_midx

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  1364911775
Powered by Datasette · Queries took 1.285ms · About: xarray-datasette