(
self, use_cftime: bool, shuffle: bool, resample_freq: ResampleCompatible
)
| 1895 | ], |
| 1896 | ) |
| 1897 | def test_resample( |
| 1898 | self, use_cftime: bool, shuffle: bool, resample_freq: ResampleCompatible |
| 1899 | ) -> None: |
| 1900 | if use_cftime and not has_cftime: |
| 1901 | pytest.skip() |
| 1902 | times = xr.date_range( |
| 1903 | "2000-01-01", freq="6h", periods=10, use_cftime=use_cftime |
| 1904 | ) |
| 1905 | |
| 1906 | def resample_as_pandas(array, *args, **kwargs): |
| 1907 | array_ = array.copy(deep=True) |
| 1908 | if use_cftime: |
| 1909 | array_["time"] = times.to_datetimeindex(time_unit="ns") |
| 1910 | result = DataArray.from_series( |
| 1911 | array_.to_series().resample(*args, **kwargs).mean() |
| 1912 | ) |
| 1913 | if use_cftime: |
| 1914 | result = result.convert_calendar( |
| 1915 | calendar="standard", use_cftime=use_cftime |
| 1916 | ) |
| 1917 | return result |
| 1918 | |
| 1919 | array = DataArray(np.arange(10), [("time", times)]) |
| 1920 | |
| 1921 | rs = array.resample(time=resample_freq) |
| 1922 | shuffled = rs.shuffle_to_chunks().resample(time=resample_freq) |
| 1923 | actual = rs.mean() |
| 1924 | expected = resample_as_pandas(array, resample_freq) |
| 1925 | assert_identical(expected, actual) |
| 1926 | assert_identical(expected, shuffled.mean()) |
| 1927 | |
| 1928 | assert_identical(expected, rs.reduce(np.mean)) |
| 1929 | assert_identical(expected, shuffled.reduce(np.mean)) |
| 1930 | |
| 1931 | rs = array.resample(time="24h", closed="right") |
| 1932 | actual = rs.mean() |
| 1933 | shuffled = rs.shuffle_to_chunks().resample(time="24h", closed="right") |
| 1934 | expected = resample_as_pandas(array, "24h", closed="right") |
| 1935 | assert_identical(expected, actual) |
| 1936 | assert_identical(expected, shuffled.mean()) |
| 1937 | |
| 1938 | with pytest.raises(ValueError, match=r"Index must be monotonic"): |
| 1939 | array[[2, 0, 1]].resample(time=resample_freq) |
| 1940 | |
| 1941 | reverse = array.isel(time=slice(-1, None, -1)) |
| 1942 | with pytest.raises(ValueError): |
| 1943 | reverse.resample(time=resample_freq).mean() |
| 1944 | |
| 1945 | def test_resample_doctest(self, use_cftime: bool) -> None: |
| 1946 | # run the doctest example here so we are not surprised |
nothing calls this directly
no test coverage detected