|
22 | 22 | PandasIndexAdapter, |
23 | 23 | VectorizedIndexer, |
24 | 24 | ) |
| 25 | +from xarray.core.pycompat import dask_array_type |
25 | 26 | from xarray.core.utils import NDArrayMixin |
26 | 27 | from xarray.core.variable import as_compatible_data, as_variable |
27 | 28 | from xarray.tests import requires_bottleneck |
@@ -1492,23 +1493,31 @@ def test_reduce(self): |
1492 | 1493 | with pytest.warns(DeprecationWarning, match="allow_lazy is deprecated"): |
1493 | 1494 | v.mean(dim="x", allow_lazy=False) |
1494 | 1495 |
|
1495 | | - def test_quantile(self): |
| 1496 | + @pytest.mark.parametrize("q", [0.25, [0.50], [0.25, 0.75]]) |
| 1497 | + @pytest.mark.parametrize( |
| 1498 | + "axis, dim", zip([None, 0, [0], [0, 1]], [None, "x", ["x"], ["x", "y"]]) |
| 1499 | + ) |
| 1500 | + def test_quantile(self, q, axis, dim): |
1496 | 1501 | v = Variable(["x", "y"], self.d) |
1497 | | - for q in [0.25, [0.50], [0.25, 0.75]]: |
1498 | | - for axis, dim in zip( |
1499 | | - [None, 0, [0], [0, 1]], [None, "x", ["x"], ["x", "y"]] |
1500 | | - ): |
1501 | | - actual = v.quantile(q, dim=dim) |
| 1502 | + actual = v.quantile(q, dim=dim) |
| 1503 | + expected = np.nanpercentile(self.d, np.array(q) * 100, axis=axis) |
| 1504 | + np.testing.assert_allclose(actual.values, expected) |
1502 | 1505 |
|
1503 | | - expected = np.nanpercentile(self.d, np.array(q) * 100, axis=axis) |
1504 | | - np.testing.assert_allclose(actual.values, expected) |
| 1506 | + @requires_dask |
| 1507 | + @pytest.mark.parametrize("q", [0.25, [0.50], [0.25, 0.75]]) |
| 1508 | + @pytest.mark.parametrize("axis, dim", [[1, "y"], [[1], ["y"]]]) |
| 1509 | + def test_quantile_dask(self, q, axis, dim): |
| 1510 | + v = Variable(["x", "y"], self.d).chunk({"x": 2}) |
| 1511 | + actual = v.quantile(q, dim=dim) |
| 1512 | + assert isinstance(actual.data, dask_array_type) |
| 1513 | + expected = np.nanpercentile(self.d, np.array(q) * 100, axis=axis) |
| 1514 | + np.testing.assert_allclose(actual.values, expected) |
1505 | 1515 |
|
1506 | 1516 | @requires_dask |
1507 | | - def test_quantile_dask_raises(self): |
1508 | | - # regression for GH1524 |
1509 | | - v = Variable(["x", "y"], self.d).chunk(2) |
| 1517 | + def test_quantile_chunked_dim_error(self): |
| 1518 | + v = Variable(["x", "y"], self.d).chunk({"x": 2}) |
1510 | 1519 |
|
1511 | | - with raises_regex(TypeError, "arrays stored as dask"): |
| 1520 | + with raises_regex(ValueError, "dimension 'x'"): |
1512 | 1521 | v.quantile(0.5, dim="x") |
1513 | 1522 |
|
1514 | 1523 | @requires_dask |
|
0 commit comments