From 14e60895629388ff4e038a6144e2cf293a23afd2 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Fri, 23 Mar 2018 14:35:35 +0100 Subject: [PATCH] rolling: periodic --- xarray/core/nputils.py | 7 ++++++- xarray/tests/test_nputils.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/xarray/core/nputils.py b/xarray/core/nputils.py index 4ca1f9390eb..24dc482ce27 100644 --- a/xarray/core/nputils.py +++ b/xarray/core/nputils.py @@ -146,7 +146,12 @@ def rolling_window(a, axis, window, center, fill_value): pads[axis] = (start, end) else: pads[axis] = (window - 1, 0) - a = np.pad(a, pads, mode='constant', constant_values=fill_value) + + if fill_value == 'periodic': + a = np.pad(a, pads, mode='wrap') + else: + a = np.pad(a, pads, mode='constant', constant_values=fill_value) + return _rolling_window(a, window, axis) diff --git a/xarray/tests/test_nputils.py b/xarray/tests/test_nputils.py index d3ad87d0d28..9a03c610514 100644 --- a/xarray/tests/test_nputils.py +++ b/xarray/tests/test_nputils.py @@ -53,3 +53,20 @@ def test_rolling(): actual = rolling_window(x, axis=-1, window=3, center=False, fill_value=0.0) expected = np.stack([expected, expected * 1.1], axis=0) assert_array_equal(actual, expected) + + x = np.array([1, 2, 3, 4], dtype=float) + actual = rolling_window(x, axis=-1, window=3, center=True, + fill_value='periodic') + expected = np.array([[4, 1, 2], + [1, 2, 3], + [2, 3, 4], + [3, 4, 1]], dtype=float) + assert_array_equal(actual, expected) + + actual = rolling_window(x, axis=-1, window=3, center=False, + fill_value='periodic') + expected = np.array([[3, 4, 1], + [4, 1, 2], + [1, 2, 3], + [2, 3, 4]], dtype=float) + assert_array_equal(actual, expected) \ No newline at end of file