Skip to content

Raise if Index.create_variables returns more variables than passed in through set_xindex #10503

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

Merged
merged 5 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Deprecations
Bug fixes
~~~~~~~~~

- :py:meth:`Dataset.set_xindex` now raises a helpful error when a custom index
creates extra variables that don't match the provided coordinate names, instead
of silently ignoring them. The error message suggests using the factory method
pattern with :py:meth:`xarray.Coordinates.from_xindex` and
:py:meth:`Dataset.assign_coords` for advanced use cases (:issue:`10499`).
By `Dhruva Kumar Kaushal <https://github.com/dhruvak001>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
14 changes: 14 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4940,6 +4940,20 @@ def set_xindex(
if isinstance(index, PandasMultiIndex):
coord_names = [index.dim] + list(coord_names)

# Check for extra variables that don't match the coordinate names
extra_vars = set(new_coord_vars) - set(coord_names)
if extra_vars:
extra_vars_str = ", ".join(f"'{name}'" for name in extra_vars)
coord_names_str = ", ".join(f"'{name}'" for name in coord_names)
raise ValueError(
f"The index created extra variables {extra_vars_str} that are not "
f"in the list of coordinates {coord_names_str}. "
f"Use a factory method pattern instead:\n"
f" index = {index_cls.__name__}.from_variables(ds, {list(coord_names)!r})\n"
f" coords = xr.Coordinates.from_xindex(index)\n"
f" ds = ds.assign_coords(coords)"
)

variables: dict[Hashable, Variable]
indexes: dict[Hashable, Index]

Expand Down
51 changes: 51 additions & 0 deletions xarray/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,54 @@ def test_restore_dtype_on_multiindexes(dtype: str) -> None:
foo = xr.Dataset(coords={"bar": ("bar", np.array([0, 1], dtype=dtype))})
foo = foo.stack(baz=("bar",))
assert str(foo["bar"].values.dtype) == dtype


class IndexWithExtraVariables(Index):
@classmethod
def from_variables(cls, variables, *, options=None):
return cls()

def create_variables(self, variables=None):
if variables is None:
# For Coordinates.from_xindex(), return all variables the index can create
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) clever

return {
"time": Variable(dims=("time",), data=[1, 2, 3]),
"valid_time": Variable(
dims=("time",),
data=[2, 3, 4], # time + 1
attrs={"description": "time + 1"},
),
}

result = dict(variables)
if "time" in variables:
result["valid_time"] = Variable(
dims=("time",),
data=variables["time"].data + 1,
attrs={"description": "time + 1"},
)
return result


def test_set_xindex_with_extra_variables() -> None:
"""Test that set_xindex raises an error when custom index creates extra variables."""

ds = xr.Dataset(coords={"time": [1, 2, 3]}).reset_index("time")

# Test that set_xindex raises error for extra variables
with pytest.raises(ValueError, match="extra variables 'valid_time'"):
ds.set_xindex("time", IndexWithExtraVariables)


def test_set_xindex_factory_method_pattern() -> None:
ds = xr.Dataset(coords={"time": [1, 2, 3]}).reset_index("time")

# Test the recommended factory method pattern
coord_vars = {"time": ds._variables["time"]}
index = IndexWithExtraVariables.from_variables(coord_vars)
coords = xr.Coordinates.from_xindex(index)
result = ds.assign_coords(coords)

assert "time" in result.variables
assert "valid_time" in result.variables
assert_array_equal(result.valid_time.data, result.time.data + 1)
Loading