Skip to content

Refactor swap dims #8911

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
39 changes: 11 additions & 28 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4334,9 +4334,9 @@ def rename_dims(
f"cannot rename {k!r} because it is not found "
f"in the dimensions of this dataset {tuple(self.dims)}"
)
if v in self.dims or v in self:
if v in self.dims:
raise ValueError(
f"Cannot rename {k} to {v} because {v} already exists. "
f"Cannot rename dimension {k} to {v} because dimension {v} already exists. "
"Try using swap_dims instead."
)

Expand Down Expand Up @@ -4464,33 +4464,16 @@ def swap_dims(
f"variable along the old dimension {current_name!r}"
)

result_dims = {dims_dict.get(dim, dim) for dim in self.dims}
result = self.rename_dims(dims_dict)
result = result.drop_indexes(dims_dict.keys(), errors="ignore")
for dim in dims_dict.values():
if dim in result._variables:
if dim not in result._coord_names:
result = result.set_coords(dim)
if dim not in result._indexes:
result = result.set_xindex(dim)

coord_names = self._coord_names.copy()
coord_names.update({dim for dim in dims_dict.values() if dim in self.variables})

variables: dict[Hashable, Variable] = {}
indexes: dict[Hashable, Index] = {}
for current_name, current_variable in self.variables.items():
dims = tuple(dims_dict.get(dim, dim) for dim in current_variable.dims)
var: Variable
if current_name in result_dims:
var = current_variable.to_index_variable()
var.dims = dims
if current_name in self._indexes:
indexes[current_name] = self._indexes[current_name]
variables[current_name] = var
else:
index, index_vars = create_default_index_implicit(var)
indexes.update({name: index for name in index_vars})
variables.update(index_vars)
coord_names.update(index_vars)
else:
var = current_variable.to_base_variable()
var.dims = dims
variables[current_name] = var

return self._replace_with_new_dims(variables, coord_names, indexes=indexes)
return result

def expand_dims(
self,
Expand Down
2 changes: 1 addition & 1 deletion xarray/testing/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _assert_indexes_invariants_checks(
if isinstance(index, PandasIndex):
pd_index = index.index
var = possible_coord_variables[k]
assert (index.dim,) == var.dims, (pd_index, var)
assert (index.dim,) == var.dims, (index, index.dim, var)
if k == index.dim:
# skip multi-index levels here (checked below)
assert index.coord_dtype == var.dtype, (index.coord_dtype, var.dtype)
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3251,6 +3251,13 @@ def test_swap_dims(self) -> None:
assert isinstance(actual.variables["x"], Variable)
assert actual.xindexes["y"].equals(expected.xindexes["y"])

def test_swap_dims_after_rename_vars(self) -> None:
# https://github.com/pydata/xarray/issues/8646
ds = Dataset(coords={"y": [1, 2]})
actual = ds.rename_vars(y="z").swap_dims(y="z")
expected = Dataset(coords={"z": [1, 2]})
assert_identical(actual, expected)

def test_expand_dims_error(self) -> None:
original = Dataset(
{
Expand Down