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

Method test_rolling_construct

xarray/tests/test_rolling.py:713–737  ·  view source on GitHub ↗
(self, center: bool, window: int)

Source from the content-addressed store, hash-verified

711 @pytest.mark.parametrize("center", (True, False))
712 @pytest.mark.parametrize("window", (1, 2, 3, 4))
713 def test_rolling_construct(self, center: bool, window: int) -> None:
714 df = pd.DataFrame(
715 {
716 "x": np.random.randn(20),
717 "y": np.random.randn(20),
718 "time": np.linspace(0, 1, 20),
719 }
720 )
721
722 ds = Dataset.from_dataframe(df)
723 df_rolling = df.rolling(window, center=center, min_periods=1).mean()
724 ds_rolling = ds.rolling(index=window, center=center)
725
726 ds_rolling_mean = ds_rolling.construct("window").mean("window")
727 np.testing.assert_allclose(
728 np.asarray(df_rolling["x"].values), ds_rolling_mean["x"].values
729 )
730 np.testing.assert_allclose(df_rolling.index, ds_rolling_mean["index"])
731
732 # with fill_value
733 ds_rolling_mean = ds_rolling.construct("window", stride=2, fill_value=0.0).mean(
734 "window"
735 )
736 assert (ds_rolling_mean.isnull().sum() == 0).to_dataarray(dim="vars").all()
737 assert (ds_rolling_mean["x"] == 0.0).sum() >= 0
738
739 @pytest.mark.parametrize("center", (True, False))
740 @pytest.mark.parametrize("window", (1, 2, 3, 4))

Callers

nothing calls this directly

Calls 9

linspaceMethod · 0.80
from_dataframeMethod · 0.80
meanMethod · 0.45
rollingMethod · 0.45
constructMethod · 0.45
allMethod · 0.45
to_dataarrayMethod · 0.45
sumMethod · 0.45
isnullMethod · 0.45

Tested by

no test coverage detected