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

Function test_mean_datetime_edge_cases

xarray/tests/test_groupby.py:3834–3868  ·  view source on GitHub ↗

Test mean with datetime edge cases like NaT

()

Source from the content-addressed store, hash-verified

3832
3833
3834def test_mean_datetime_edge_cases():
3835 """Test mean with datetime edge cases like NaT"""
3836 # Test with NaT values
3837 dates_with_nat = pd.date_range("2021-01-01", periods=4, freq="D")
3838 dates_with_nat_array = dates_with_nat.values.copy()
3839 dates_with_nat_array[1] = np.datetime64("NaT", "us")
3840
3841 ds = xr.Dataset(
3842 {
3843 "dates": (("x",), dates_with_nat_array),
3844 "values": (("x",), [1.0, 2.0, 3.0, 4.0]),
3845 }
3846 )
3847
3848 # Mean should handle NaT properly (skipna behavior)
3849 result = ds.mean()
3850 assert "dates" in result.data_vars
3851 assert "values" in result.data_vars
3852 # The mean should skip NaT and compute mean of the other 3 dates
3853 assert not result.dates.isnull().item()
3854
3855 # Test with timedelta
3856 timedeltas = pd.timedelta_range("1 day", periods=4, freq="D")
3857 ds_td = xr.Dataset(
3858 {
3859 "timedeltas": (("x",), timedeltas),
3860 "values": (("x",), [1.0, 2.0, 3.0, 4.0]),
3861 }
3862 )
3863
3864 result_td = ds_td.mean()
3865 assert "timedeltas" in result_td.data_vars
3866 assert result_td["timedeltas"].values == np.timedelta64(
3867 216000000000000, "ns"
3868 ) # 2.5 days
3869
3870
3871@requires_cftime

Callers

nothing calls this directly

Calls 3

copyMethod · 0.45
meanMethod · 0.45
isnullMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…