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

Don't raise rename warning if it is a no operation #8266

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4165,6 +4165,9 @@ def _rename(
create_dim_coord = False
new_k = name_dict[k]

if k == new_k:
continue # Same name, nothing to do

if k in self.dims and new_k in self._coord_names:
coord_dims = self._variables[name_dict[k]].dims
if coord_dims == (k,):
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,16 @@ def test_rename_dimension_coord_warnings(self) -> None:
):
da.rename(x="y")

# No operation should not raise a warning
da = xr.DataArray(
data=np.ones((2, 3)),
dims=["x", "y"],
coords={"x": range(2), "y": range(3), "a": ("x", [3, 4])},
)
with warnings.catch_warnings():
warnings.simplefilter("error")
da.rename(x="x")

def test_init_value(self) -> None:
expected = DataArray(
np.full((3, 4), 3), dims=["x", "y"], coords=[range(3), range(4)]
Expand Down
12 changes: 10 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3032,8 +3032,7 @@ def test_rename_old_name(self) -> None:
def test_rename_same_name(self) -> None:
data = create_test_data()
newnames = {"var1": "var1", "dim2": "dim2"}
with pytest.warns(UserWarning, match="does not create an index anymore"):
renamed = data.rename(newnames)
renamed = data.rename(newnames)
assert_identical(renamed, data)

def test_rename_dims(self) -> None:
Expand Down Expand Up @@ -3103,6 +3102,15 @@ def test_rename_dimension_coord_warnings(self) -> None:
):
ds.rename(x="y")

# No operation should not raise a warning
ds = Dataset(
data_vars={"data": (("x", "y"), np.ones((2, 3)))},
coords={"x": range(2), "y": range(3), "a": ("x", [3, 4])},
)
with warnings.catch_warnings():
warnings.simplefilter("error")
ds.rename(x="x")

def test_rename_multiindex(self) -> None:
midx = pd.MultiIndex.from_tuples([([1, 2]), ([3, 4])], names=["a", "b"])
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
Expand Down