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

Method test_set_index

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

Source from the content-addressed store, hash-verified

3878 assert ds.time.dtype == np.dtype("datetime64[s]")
3879
3880 def test_set_index(self) -> None:
3881 expected = create_test_multiindex()
3882 mindex = expected["x"].to_index()
3883 indexes = [mindex.get_level_values(str(n)) for n in mindex.names]
3884 coords = {idx.name: ("x", idx) for idx in indexes}
3885 ds = Dataset({}, coords=coords)
3886
3887 obj = ds.set_index(x=mindex.names)
3888 assert_identical(obj, expected)
3889
3890 # ensure pre-existing indexes involved are removed
3891 # (level_2 should be a coordinate with no index)
3892 ds = create_test_multiindex()
3893 coords = {"x": coords["level_1"], "level_2": coords["level_2"]}
3894 expected = Dataset({}, coords=coords)
3895
3896 obj = ds.set_index(x="level_1")
3897 assert_identical(obj, expected)
3898
3899 # ensure set_index with no existing index and a single data var given
3900 # doesn't return multi-index
3901 ds = Dataset(data_vars={"x_var": ("x", [0, 1, 2])})
3902 expected = Dataset(coords={"x": [0, 1, 2]})
3903 assert_identical(ds.set_index(x="x_var"), expected)
3904
3905 with pytest.raises(ValueError, match=r"bar variable\(s\) do not exist"):
3906 ds.set_index(foo="bar")
3907
3908 with pytest.raises(ValueError, match=r"dimension mismatch.*"):
3909 ds.set_index(y="x_var")
3910
3911 ds = Dataset(coords={"x": 1})
3912 with pytest.raises(
3913 ValueError, match=r".*cannot set a PandasIndex.*scalar variable.*"
3914 ):
3915 ds.set_index(x="x")
3916
3917 def test_set_index_deindexed_coords(self) -> None:
3918 # test de-indexed coordinates are converted to base variable

Callers

nothing calls this directly

Calls 5

set_indexMethod · 0.95
DatasetClass · 0.90
assert_identicalFunction · 0.90
create_test_multiindexFunction · 0.85
to_indexMethod · 0.45

Tested by

no test coverage detected