@@ -3431,21 +3431,34 @@ def test_expand_dims_kwargs_python36plus(self) -> None:
3431
3431
)
3432
3432
assert_identical (other_way_expected , other_way )
3433
3433
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 ):
3435
3436
# 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 == {}
3442
3448
3449
+ def test_expand_dims_create_index_coordinate_variable (self ):
3443
3450
# coordinate variables should gain an index only if create_1d_index is True (the default)
3444
3451
ds = Dataset (coords = {"x" : 0 })
3445
3452
expanded = ds .expand_dims ("x" )
3446
3453
expected = Dataset ({"x" : ("x" , [0 ])})
3447
3454
assert_identical (expanded , expected )
3455
+
3448
3456
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" ])}
3449
3462
assert expanded_no_index .indexes == {}
3450
3463
3451
3464
@requires_pandas_version_two
0 commit comments