(dask, which_datetime)
| 8154 | @pytest.mark.parametrize("dask", [True, False]) |
| 8155 | @pytest.mark.parametrize("which_datetime", ["np", "cftime"]) |
| 8156 | def test_trapezoid_datetime(dask, which_datetime) -> None: |
| 8157 | rs = np.random.default_rng(42) |
| 8158 | coord: ArrayLike |
| 8159 | if which_datetime == "np": |
| 8160 | coord = np.array( |
| 8161 | [ |
| 8162 | "2004-07-13", |
| 8163 | "2006-01-13", |
| 8164 | "2010-08-13", |
| 8165 | "2010-09-13", |
| 8166 | "2010-10-11", |
| 8167 | "2010-12-13", |
| 8168 | "2011-02-13", |
| 8169 | "2012-08-13", |
| 8170 | ], |
| 8171 | dtype="datetime64", |
| 8172 | ) |
| 8173 | else: |
| 8174 | if not has_cftime: |
| 8175 | pytest.skip("Test requires cftime.") |
| 8176 | coord = xr.date_range("2000", periods=8, freq="2D", use_cftime=True) |
| 8177 | |
| 8178 | da = xr.DataArray( |
| 8179 | rs.random((8, 6)), |
| 8180 | coords={"time": coord, "z": 3, "t2d": (("time", "y"), rs.random((8, 6)))}, |
| 8181 | dims=["time", "y"], |
| 8182 | ) |
| 8183 | |
| 8184 | if dask and has_dask: |
| 8185 | da = da.chunk({"time": 4}) |
| 8186 | |
| 8187 | actual = da.integrate("time", datetime_unit="D") |
| 8188 | expected_data = trapezoid( |
| 8189 | da.compute().data, |
| 8190 | duck_array_ops.datetime_to_numeric(da["time"].data, datetime_unit="D"), |
| 8191 | axis=0, |
| 8192 | ) |
| 8193 | expected = xr.DataArray( |
| 8194 | expected_data, |
| 8195 | dims=["y"], |
| 8196 | coords={k: v for k, v in da.coords.items() if "time" not in v.dims}, |
| 8197 | ) |
| 8198 | assert_allclose(expected, actual.compute()) |
| 8199 | |
| 8200 | # make sure result is also a dask array (if the source is dask array) |
| 8201 | assert isinstance(actual.data, type(da.data)) |
| 8202 | |
| 8203 | actual2 = da.integrate("time", datetime_unit="h") |
| 8204 | assert_allclose(actual, actual2 / 24.0) |
| 8205 | |
| 8206 | |
| 8207 | def test_no_dict() -> None: |
nothing calls this directly
no test coverage detected
searching dependent graphs…