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

Function test_differentiate_cftime

xarray/tests/test_dataset.py:8011–8036  ·  view source on GitHub ↗
(dask)

Source from the content-addressed store, hash-verified

8009@requires_cftime
8010@pytest.mark.parametrize("dask", [True, False])
8011def test_differentiate_cftime(dask) -> None:
8012 rs = np.random.default_rng(42)
8013 coord = xr.date_range("2000", periods=8, freq="2ME", use_cftime=True)
8014
8015 da = xr.DataArray(
8016 rs.random((8, 6)),
8017 coords={"time": coord, "z": 3, "t2d": (("time", "y"), rs.random((8, 6)))},
8018 dims=["time", "y"],
8019 )
8020
8021 if dask and has_dask:
8022 da = da.chunk({"time": 4})
8023
8024 actual = da.differentiate("time", edge_order=1, datetime_unit="D")
8025 expected_data = np.gradient(
8026 da, da["time"].variable._to_numeric(datetime_unit="D"), axis=0, edge_order=1
8027 )
8028 expected = xr.DataArray(expected_data, coords=da.coords, dims=da.dims)
8029 assert_equal(expected, actual)
8030
8031 actual2 = da.differentiate("time", edge_order=1, datetime_unit="h")
8032 assert_allclose(actual, actual2 * 24)
8033
8034 # Test the differentiation of datetimes themselves
8035 actual = da["time"].differentiate("time", edge_order=1, datetime_unit="D")
8036 assert_allclose(actual, xr.ones_like(da["time"]).astype(float))
8037
8038
8039@pytest.mark.parametrize("dask", [True, False])

Callers

nothing calls this directly

Calls 7

chunkMethod · 0.95
differentiateMethod · 0.95
assert_equalFunction · 0.90
assert_allcloseFunction · 0.90
_to_numericMethod · 0.80
differentiateMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…