Skip to content

Commit 64eba1d

Browse files
committed
Fix test_dataarray.TestDataArray.test_expand_dims_with_greater_dim_size tests to pass in python 3.5 using ordered dicts instead of regular dicts. This was needed because python 3.5 and earlier did not maintain insertion order for dicts
1 parent 25067cf commit 64eba1d

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

xarray/tests/test_dataarray.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,25 +1403,43 @@ def test_expand_dims_with_scalar_coordinate(self):
14031403
assert_identical(array, roundtripped)
14041404

14051405
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+
"""
14061410
array = DataArray(np.random.randn(3, 4), dims=['x', 'dim_0'],
14071411
coords={'x': np.linspace(0.0, 1.0, 3), 'z': 1.0},
14081412
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))))
14111421
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()),
14181424
attrs={'key': 'entry'}
14191425
).drop(['y', 'dim_0'])
14201426
assert_identical(expected, actual)
14211427

14221428
# Test with kwargs instead of passing dict to dim arg.
14231429
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)
14251443

14261444
def test_set_index(self):
14271445
indexes = [self.mindex.get_level_values(n) for n in self.mindex.names]

0 commit comments

Comments
 (0)