@@ -1403,25 +1403,43 @@ def test_expand_dims_with_scalar_coordinate(self):
1403
1403
assert_identical (array , roundtripped )
1404
1404
1405
1405
def test_expand_dims_with_greater_dim_size (self ):
1406
+ """Python 3.6+ dicts keep insertion order, unlike Python 3.5 and
1407
+ earlier. Therefore the following tests have to use ordered dicts to
1408
+ pass for python 3.5 and earlier.
1409
+ """
1406
1410
array = DataArray (np .random .randn (3 , 4 ), dims = ['x' , 'dim_0' ],
1407
1411
coords = {'x' : np .linspace (0.0 , 1.0 , 3 ), 'z' : 1.0 },
1408
1412
attrs = {'key' : 'entry' })
1409
- actual = array .expand_dims ({'y' : 2 , 'z' : 1 , 'dim_1' : ['a' , 'b' , 'c' ]})
1410
-
1413
+ # For python 3.5 and earlier this has to be an ordered dict, to
1414
+ # maintain insertion order.
1415
+ actual = array .expand_dims (
1416
+ OrderedDict ((('y' , 2 ), ('z' , 1 ), ('dim_1' , ['a' , 'b' , 'c' ]))))
1417
+
1418
+ expected_coords = OrderedDict ((
1419
+ ('y' , [0 , 1 ]), ('z' , [1.0 ]), ('dim_1' , ['a' , 'b' , 'c' ]),
1420
+ ('x' , np .linspace (0 , 1 , 3 )), ('dim_0' , range (4 ))))
1411
1421
expected = DataArray (array .values * np .ones ([2 , 1 , 3 , 3 , 4 ]),
1412
- coords = dict (y = [0 , 1 ],
1413
- z = [1.0 ],
1414
- dim_1 = ['a' , 'b' , 'c' ],
1415
- x = np .linspace (0 , 1 , 3 ),
1416
- dim_0 = range (4 )),
1417
- dims = ['y' , 'z' , 'dim_1' , 'x' , 'dim_0' ],
1422
+ coords = expected_coords ,
1423
+ dims = list (expected_coords .keys ()),
1418
1424
attrs = {'key' : 'entry' }
1419
1425
).drop (['y' , 'dim_0' ])
1420
1426
assert_identical (expected , actual )
1421
1427
1422
1428
# Test with kwargs instead of passing dict to dim arg.
1423
1429
other_way = array .expand_dims (y = 2 , z = 1 , dim_1 = ['a' , 'b' , 'c' ])
1424
- assert_identical (expected , other_way )
1430
+ # Unfortunately, there is no way to maintain insertion order with
1431
+ # kwargs in python 3.5 and earlier, so for now we have to ensure the
1432
+ # dimensions of the expected result are in the same order as the actual
1433
+ # result to allow the test to pass.
1434
+ other_way_expected_coords = OrderedDict ()
1435
+ for dim in other_way .dims :
1436
+ other_way_expected_coords [dim ] = expected_coords [dim ]
1437
+ other_way_expected = DataArray (
1438
+ array .values * np .ones (list (other_way .shape )),
1439
+ coords = other_way_expected_coords ,
1440
+ dims = list (other_way_expected_coords .keys ()),
1441
+ attrs = {'key' : 'entry' }).drop (['y' , 'dim_0' ])
1442
+ assert_identical (other_way_expected , other_way )
1425
1443
1426
1444
def test_set_index (self ):
1427
1445
indexes = [self .mindex .get_level_values (n ) for n in self .mindex .names ]
0 commit comments