MCPcopy
hub / github.com/pydata/xarray / test_datetime_to_numeric_cftime

Function test_datetime_to_numeric_cftime

xarray/tests/test_duck_array_ops.py:968–1009  ·  view source on GitHub ↗
(dask)

Source from the content-addressed store, hash-verified

966@requires_cftime
967@pytest.mark.parametrize("dask", [True, False])
968def test_datetime_to_numeric_cftime(dask):
969 if dask and not has_dask:
970 pytest.skip("requires dask")
971
972 times = date_range(
973 "2000", periods=5, freq="7D", calendar="standard", use_cftime=True
974 ).values
975 if dask:
976 import dask.array
977
978 times = dask.array.from_array(times, chunks=-1)
979 with raise_if_dask_computes():
980 result = duck_array_ops.datetime_to_numeric(times, datetime_unit="h", dtype=int)
981 expected = 24 * np.arange(0, 35, 7)
982 np.testing.assert_array_equal(result, expected)
983
984 offset = times[1]
985 with raise_if_dask_computes():
986 result = duck_array_ops.datetime_to_numeric(
987 times, offset=offset, datetime_unit="h", dtype=int
988 )
989 expected = 24 * np.arange(-7, 28, 7)
990 np.testing.assert_array_equal(result, expected)
991
992 dtype = np.float32
993 with raise_if_dask_computes():
994 result = duck_array_ops.datetime_to_numeric(
995 times, datetime_unit="h", dtype=dtype
996 )
997 expected2: Any = 24 * np.arange(0, 35, 7).astype(dtype)
998 np.testing.assert_array_equal(result, expected2)
999
1000 with raise_if_dask_computes():
1001 if dask:
1002 time = dask.array.asarray(times[1])
1003 else:
1004 time = np.asarray(times[1])
1005 result = duck_array_ops.datetime_to_numeric(
1006 time, offset=times[0], datetime_unit="h", dtype=int
1007 )
1008 expected3 = np.array(24 * 7).astype(int)
1009 np.testing.assert_array_equal(result, expected3)
1010
1011
1012@requires_cftime

Callers

nothing calls this directly

Calls 5

date_rangeFunction · 0.90
raise_if_dask_computesFunction · 0.90
arangeMethod · 0.80
from_arrayMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…