|
51 | 51 | requires_bottleneck,
|
52 | 52 | requires_cupy,
|
53 | 53 | requires_dask,
|
| 54 | + requires_dask_expr, |
54 | 55 | requires_iris,
|
55 | 56 | requires_numexpr,
|
56 | 57 | requires_pint,
|
@@ -3203,6 +3204,42 @@ def test_align_str_dtype(self) -> None:
|
3203 | 3204 | assert_identical(expected_b, actual_b)
|
3204 | 3205 | assert expected_b.x.dtype == actual_b.x.dtype
|
3205 | 3206 |
|
| 3207 | + def test_broadcast_on_vs_off_global_option_different_dims(self) -> None: |
| 3208 | + xda_1 = xr.DataArray([1], dims="x1") |
| 3209 | + xda_2 = xr.DataArray([1], dims="x2") |
| 3210 | + |
| 3211 | + with xr.set_options(arithmetic_broadcast=True): |
| 3212 | + expected_xda = xr.DataArray([[1.0]], dims=("x1", "x2")) |
| 3213 | + actual_xda = xda_1 / xda_2 |
| 3214 | + assert_identical(actual_xda, expected_xda) |
| 3215 | + |
| 3216 | + with xr.set_options(arithmetic_broadcast=False): |
| 3217 | + with pytest.raises( |
| 3218 | + ValueError, |
| 3219 | + match=re.escape( |
| 3220 | + "Broadcasting is necessary but automatic broadcasting is disabled via " |
| 3221 | + "global option `'arithmetic_broadcast'`. " |
| 3222 | + "Use `xr.set_options(arithmetic_broadcast=True)` to enable automatic broadcasting." |
| 3223 | + ), |
| 3224 | + ): |
| 3225 | + xda_1 / xda_2 |
| 3226 | + |
| 3227 | + @pytest.mark.parametrize("arithmetic_broadcast", [True, False]) |
| 3228 | + def test_broadcast_on_vs_off_global_option_same_dims( |
| 3229 | + self, arithmetic_broadcast: bool |
| 3230 | + ) -> None: |
| 3231 | + # Ensure that no error is raised when arithmetic broadcasting is disabled, |
| 3232 | + # when broadcasting is not needed. The two DataArrays have the same |
| 3233 | + # dimensions of the same size. |
| 3234 | + xda_1 = xr.DataArray([1], dims="x") |
| 3235 | + xda_2 = xr.DataArray([1], dims="x") |
| 3236 | + expected_xda = xr.DataArray([2.0], dims=("x",)) |
| 3237 | + |
| 3238 | + with xr.set_options(arithmetic_broadcast=arithmetic_broadcast): |
| 3239 | + assert_identical(xda_1 + xda_2, expected_xda) |
| 3240 | + assert_identical(xda_1 + np.array([1.0]), expected_xda) |
| 3241 | + assert_identical(np.array([1.0]) + xda_1, expected_xda) |
| 3242 | + |
3206 | 3243 | def test_broadcast_arrays(self) -> None:
|
3207 | 3244 | x = DataArray([1, 2], coords=[("a", [-1, -2])], name="x")
|
3208 | 3245 | y = DataArray([1, 2], coords=[("b", [3, 4])], name="y")
|
@@ -3381,6 +3418,7 @@ def test_to_dataframe_0length(self) -> None:
|
3381 | 3418 | assert len(actual) == 0
|
3382 | 3419 | assert_array_equal(actual.index.names, list("ABC"))
|
3383 | 3420 |
|
| 3421 | + @requires_dask_expr |
3384 | 3422 | @requires_dask
|
3385 | 3423 | def test_to_dask_dataframe(self) -> None:
|
3386 | 3424 | arr_np = np.arange(3 * 4).reshape(3, 4)
|
|
0 commit comments