Skip to content
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

renaming index variables with rename_vars seems buggy #8659

Closed
dcherian opened this issue Jan 24, 2024 · 1 comment
Closed

renaming index variables with rename_vars seems buggy #8659

dcherian opened this issue Jan 24, 2024 · 1 comment
Labels

Comments

@dcherian
Copy link
Contributor

dcherian commented Jan 24, 2024

What happened?

(xref #8658)

I'm not sure what the expected behaviour is here:

import xarray as xr
import numpy as np
from xarray.testing import _assert_internal_invariants

ds = xr.Dataset()
ds.coords["1"] = ("1", np.array([1], dtype=np.uint32))
ds["1_"] = ("1", np.array([1], dtype=np.uint32))
ds = ds.rename_vars({"1": "0"})
ds

It looks like this sequence of operations creates a default index
image

But then

from xarray.testing import _assert_internal_invariants

_assert_internal_invariants(ds, check_default_indexes=True)

fails with

...
File ~/repos/xarray/xarray/testing/assertions.py:301, in _assert_indexes_invariants_checks(indexes, possible_coord_variables, dims, check_default)
    299 if check_default:
    300     defaults = default_indexes(possible_coord_variables, dims)
--> 301     assert indexes.keys() == defaults.keys(), (set(indexes), set(defaults))
    302     assert all(v.equals(defaults[k]) for k, v in indexes.items()), (
    303         indexes,
    304         defaults,
    305     )

AssertionError: ({'0'}, set())
@benbovy
Copy link
Member

benbovy commented Jan 25, 2024

That looks right to me.

  • ds.coords["1"] = ("1", np.array([1], dtype=np.uint32)) creates an index by default (setting a dimension coordinate)
  • since the index refactor ds = ds.rename_vars({"1": "0"}) preserves the coordinate index
  • _assert_internal_invariants(ds, check_default_indexes=True) fails as expected (presence of a non-dimension index after renaming the coordinate "1" -> "0".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants