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

Function test_differentiate_datetime

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

Source from the content-addressed store, hash-verified

7959
7960@pytest.mark.parametrize("dask", [True, False])
7961def test_differentiate_datetime(dask) -> None:
7962 rs = np.random.default_rng(42)
7963 coord = np.array(
7964 [
7965 "2004-07-13",
7966 "2006-01-13",
7967 "2010-08-13",
7968 "2010-09-13",
7969 "2010-10-11",
7970 "2010-12-13",
7971 "2011-02-13",
7972 "2012-08-13",
7973 ],
7974 dtype="datetime64",
7975 )
7976
7977 da = xr.DataArray(
7978 rs.random((8, 6)),
7979 dims=["x", "y"],
7980 coords={"x": coord, "z": 3, "x2d": (("x", "y"), rs.random((8, 6)))},
7981 )
7982 if dask and has_dask:
7983 da = da.chunk({"x": 4})
7984
7985 # along x
7986 actual = da.differentiate("x", edge_order=1, datetime_unit="D")
7987 expected_x = xr.DataArray(
7988 np.gradient(
7989 da, da["x"].variable._to_numeric(datetime_unit="D"), axis=0, edge_order=1
7990 ),
7991 dims=da.dims,
7992 coords=da.coords,
7993 )
7994 assert_equal(expected_x, actual)
7995
7996 actual2 = da.differentiate("x", edge_order=1, datetime_unit="h")
7997 assert np.allclose(actual, actual2 * 24)
7998
7999 # for datetime variable
8000 actual = da["x"].differentiate("x", edge_order=1, datetime_unit="D")
8001 assert np.allclose(actual, 1.0)
8002
8003 # with different date unit
8004 da = xr.DataArray(coord.astype("datetime64[ms]"), dims=["x"], coords={"x": coord})
8005 actual = da.differentiate("x", edge_order=1)
8006 assert np.allclose(actual, 1.0)
8007
8008
8009@requires_cftime

Callers

nothing calls this directly

Calls 6

chunkMethod · 0.95
differentiateMethod · 0.95
assert_equalFunction · 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…