MCPcopy Index your code
hub / github.com/pydata/xarray / test_sortby

Method test_sortby

xarray/tests/test_dataset.py:7223–7324  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

7221 assert_equal(actual, expected)
7222
7223 def test_sortby(self) -> None:
7224 ds = Dataset(
7225 {
7226 "A": DataArray(
7227 [[1, 2], [3, 4], [5, 6]], [("x", ["c", "b", "a"]), ("y", [1, 0])]
7228 ),
7229 "B": DataArray([[5, 6], [7, 8], [9, 10]], dims=["x", "y"]),
7230 }
7231 )
7232
7233 sorted1d = Dataset(
7234 {
7235 "A": DataArray(
7236 [[5, 6], [3, 4], [1, 2]], [("x", ["a", "b", "c"]), ("y", [1, 0])]
7237 ),
7238 "B": DataArray([[9, 10], [7, 8], [5, 6]], dims=["x", "y"]),
7239 }
7240 )
7241
7242 sorted2d = Dataset(
7243 {
7244 "A": DataArray(
7245 [[6, 5], [4, 3], [2, 1]], [("x", ["a", "b", "c"]), ("y", [0, 1])]
7246 ),
7247 "B": DataArray([[10, 9], [8, 7], [6, 5]], dims=["x", "y"]),
7248 }
7249 )
7250
7251 expected = sorted1d
7252 dax = DataArray([100, 99, 98], [("x", ["c", "b", "a"])])
7253 actual = ds.sortby(dax)
7254 assert_equal(actual, expected)
7255
7256 # test descending order sort
7257 actual = ds.sortby(dax, ascending=False)
7258 assert_equal(actual, ds)
7259
7260 # test alignment (fills in nan for 'c')
7261 dax_short = DataArray([98, 97], [("x", ["b", "a"])])
7262 actual = ds.sortby(dax_short)
7263 assert_equal(actual, expected)
7264
7265 # test 1-D lexsort
7266 # dax0 is sorted first to give indices of [1, 2, 0]
7267 # and then dax1 would be used to move index 2 ahead of 1
7268 dax0 = DataArray([100, 95, 95], [("x", ["c", "b", "a"])])
7269 dax1 = DataArray([0, 1, 0], [("x", ["c", "b", "a"])])
7270 actual = ds.sortby([dax0, dax1]) # lexsort underneath gives [2, 1, 0]
7271 assert_equal(actual, expected)
7272
7273 expected = sorted2d
7274 # test multi-dim sort by 1D dataarray values
7275 day = DataArray([90, 80], [("y", [1, 0])])
7276 actual = ds.sortby([day, dax])
7277 assert_equal(actual, expected)
7278
7279 # test exception-raising
7280 with pytest.raises(KeyError):

Callers

nothing calls this directly

Calls 4

sortbyMethod · 0.95
DatasetClass · 0.90
DataArrayClass · 0.90
assert_equalFunction · 0.90

Tested by

no test coverage detected