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

Convert Variable to IndexVariable or vice versa when renamed #4108

Closed
wants to merge 20 commits into from
Closed
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Bug fixes
By `Victor Negîrneac <https://github.com/caenrigen>`_.
- Don't allow passing ``axis`` to :py:meth:`Dataset.reduce` methods (:issue:`3510`, :pull:`4940`).
By `Justus Magin <https://github.com/keewis>`_.
- Convert to IndexVariable or Variable during renaming as appropriate. (:issue:`4107`, :issue:`4417`, :pull:`4108`)
By `Deepak Cherian <https://github.com/dcherian>`_.
- Decode values as signed if attribute `_Unsigned = "false"` (:issue:`4954`)
By `Tobias Kölling <https://github.com/d70-t>`_.

Expand Down
20 changes: 19 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3006,7 +3006,7 @@ def _rename_dims(self, name_dict):

def _rename_indexes(self, name_dict, dims_set):
if self._indexes is None:
return None
return {}
dcherian marked this conversation as resolved.
Show resolved Hide resolved
indexes = {}
for k, v in self.indexes.items():
new_name = name_dict.get(k, k)
Expand All @@ -3024,6 +3024,24 @@ def _rename_all(self, name_dict, dims_dict):
variables, coord_names = self._rename_vars(name_dict, dims_dict)
dims = self._rename_dims(dims_dict)
indexes = self._rename_indexes(name_dict, dims.keys())

# Variable could be renamed to an existing dimension name
# in this case, convert to IndexVariable and set indexes
# GH4107
for name in set(self.dims) & set(variables) - set(indexes):
variables[name] = variables[name].to_index_variable()
indexes[name] = variables[name].to_index()
coord_names.add(name)

# rename_dims was called to rename an indexed dimension
# the new renamed dimension is unindexed
# remove the old variable from indexes and convert to a base variable
if indexes:
for name, newname in dims_dict.items():
if name != newname and name in indexes:
variables[name] = variables[name].to_base_variable()
del indexes[name]

return variables, coord_names, dims, indexes

def rename(
Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,22 @@ def test_rename_vars(self):
with pytest.raises(ValueError):
original.rename_vars(names_dict_bad)

def test_rename_to_dim_name(self):
# regression test for GH4107
coord_1 = xr.DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True})
ds = xr.Dataset()
ds["a"] = xr.DataArray([1, 0], [coord_1])
obj = ds.reset_index("coord_1").rename({"coord_1_": "coord_1"})
assert_equal(ds, obj)

newds = (
ds.reset_index("coord_1")
.reset_coords("coord_1_")
.rename({"coord_1_": "coord_1"})
)
assert "coord_1" not in newds.data_vars
dcherian marked this conversation as resolved.
Show resolved Hide resolved
assert_equal(newds, obj)

def test_rename_multiindex(self):
mindex = pd.MultiIndex.from_tuples(
[([1, 2]), ([3, 4])], names=["level0", "level1"]
Expand Down