MCPcopy
hub / github.com/pydata/xarray / test_rename_multiindex

Method test_rename_multiindex

xarray/tests/test_dataset.py:3509–3531  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3507 ds.rename(x="x")
3508
3509 def test_rename_multiindex(self) -> None:
3510 midx = pd.MultiIndex.from_tuples([(1, 2), (3, 4)], names=["a", "b"])
3511 midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
3512 original = Dataset({}, midx_coords)
3513
3514 # pandas-stubs expects Hashable for rename, but list of names works for MultiIndex
3515 midx_renamed = midx.rename(["a", "c"]) # type: ignore[call-overload]
3516 midx_coords_renamed = Coordinates.from_pandas_multiindex(midx_renamed, "x")
3517 expected = Dataset({}, midx_coords_renamed)
3518
3519 actual = original.rename({"b": "c"})
3520 assert_identical(expected, actual)
3521
3522 with pytest.raises(ValueError, match=r"'a' conflicts"):
3523 with pytest.warns(UserWarning, match="does not create an index anymore"):
3524 original.rename({"x": "a"})
3525
3526 with pytest.raises(ValueError, match=r"'x' conflicts"):
3527 with pytest.warns(UserWarning, match="does not create an index anymore"):
3528 original.rename({"a": "x"})
3529
3530 with pytest.raises(ValueError, match=r"'b' conflicts"):
3531 original.rename({"a": "b"})
3532
3533 def test_rename_preserve_attrs_encoding(self) -> None:
3534 # test propagate attrs/encoding to new variable(s) created from Index object

Callers

nothing calls this directly

Calls 5

renameMethod · 0.95
DatasetClass · 0.90
assert_identicalFunction · 0.90
renameMethod · 0.45

Tested by

no test coverage detected