From ba5627eebf7b580d0a0b9a171f1f94d7412662e3 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Fri, 19 Apr 2024 22:47:07 -0400 Subject: [PATCH] refactor tests to consider data variables and coordinate variables separately --- xarray/tests/test_dataset.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 020af51a5f8..8c76e1ffd24 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -3431,21 +3431,34 @@ def test_expand_dims_kwargs_python36plus(self) -> None: ) assert_identical(other_way_expected, other_way) - def test_expand_dims_creates_indexvariable(self): + @pytest.mark.parametrize("create_1d_index_flag", [True, False]) + def test_expand_dims_create_index_data_variable(self, create_1d_index_flag): # data variables should not gain an index ever - ds = Dataset({"a": 0}) - for flag in [True, False]: - expanded = ds.expand_dims("x", create_1d_index=flag) - expected = Dataset({"a": ("x", [0])}) - assert_identical(expanded, expected) - assert expanded.indexes == {} + ds = Dataset({"x": 0}) + expanded = ds.expand_dims("x", create_1d_index=create_1d_index_flag) + + # TODO I can't just create the expected dataset directly using constructor because of GH issue 8959 + # expected = Dataset(data_vars={"x": ("x", [0])}) + expected = Dataset({"x": ("x", [0])}).drop_indexes("x").reset_coords("x") + + # TODO also can't just assert equivalence because it will fail internal invariants default indexes checks + # assert_identical(expanded, expected) + assert expected.data_vars == {"x": Variable(data=[0], dims=["x"])} + assert expanded.indexes == {} + def test_expand_dims_create_index_coordinate_variable(self): # coordinate variables should gain an index only if create_1d_index is True (the default) ds = Dataset(coords={"x": 0}) expanded = ds.expand_dims("x") expected = Dataset({"x": ("x", [0])}) assert_identical(expanded, expected) + expanded_no_index = ds.expand_dims("x", create_1d_index=False) + expected = Dataset(coords={"x": ("x", [0])}).drop_indexes("x") + + # TODO also can't just assert equivalence because it will fail internal invariants default indexes checks + # assert_identical(expanded, expected) + assert expanded_no_index.coords == {"x": Variable(data=[0], dims=["x"])} assert expanded_no_index.indexes == {} @requires_pandas_version_two