Skip to content

Commit ba5627e

Browse files
committed
refactor tests to consider data variables and coordinate variables separately
1 parent 95d453c commit ba5627e

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

xarray/tests/test_dataset.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,21 +3431,34 @@ def test_expand_dims_kwargs_python36plus(self) -> None:
34313431
)
34323432
assert_identical(other_way_expected, other_way)
34333433

3434-
def test_expand_dims_creates_indexvariable(self):
3434+
@pytest.mark.parametrize("create_1d_index_flag", [True, False])
3435+
def test_expand_dims_create_index_data_variable(self, create_1d_index_flag):
34353436
# data variables should not gain an index ever
3436-
ds = Dataset({"a": 0})
3437-
for flag in [True, False]:
3438-
expanded = ds.expand_dims("x", create_1d_index=flag)
3439-
expected = Dataset({"a": ("x", [0])})
3440-
assert_identical(expanded, expected)
3441-
assert expanded.indexes == {}
3437+
ds = Dataset({"x": 0})
3438+
expanded = ds.expand_dims("x", create_1d_index=create_1d_index_flag)
3439+
3440+
# TODO I can't just create the expected dataset directly using constructor because of GH issue 8959
3441+
# expected = Dataset(data_vars={"x": ("x", [0])})
3442+
expected = Dataset({"x": ("x", [0])}).drop_indexes("x").reset_coords("x")
3443+
3444+
# TODO also can't just assert equivalence because it will fail internal invariants default indexes checks
3445+
# assert_identical(expanded, expected)
3446+
assert expected.data_vars == {"x": Variable(data=[0], dims=["x"])}
3447+
assert expanded.indexes == {}
34423448

3449+
def test_expand_dims_create_index_coordinate_variable(self):
34433450
# coordinate variables should gain an index only if create_1d_index is True (the default)
34443451
ds = Dataset(coords={"x": 0})
34453452
expanded = ds.expand_dims("x")
34463453
expected = Dataset({"x": ("x", [0])})
34473454
assert_identical(expanded, expected)
3455+
34483456
expanded_no_index = ds.expand_dims("x", create_1d_index=False)
3457+
expected = Dataset(coords={"x": ("x", [0])}).drop_indexes("x")
3458+
3459+
# TODO also can't just assert equivalence because it will fail internal invariants default indexes checks
3460+
# assert_identical(expanded, expected)
3461+
assert expanded_no_index.coords == {"x": Variable(data=[0], dims=["x"])}
34493462
assert expanded_no_index.indexes == {}
34503463

34513464
@requires_pandas_version_two

0 commit comments

Comments
 (0)