Skip to content

Commit d782f55

Browse files
BUG: Incorrect handling of rolling.cov with offset window
1 parent 1a0c878 commit d782f55

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pandas/core/window.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def __init__(self, obj, window=None, min_periods=None, freq=None,
8181
self.freq = freq
8282
self.center = center
8383
self.win_type = win_type
84+
self.win_freq = None
8485
self.axis = obj._get_axis_number(axis) if axis is not None else None
8586
self.validate()
8687

@@ -996,7 +997,10 @@ def cov(self, other=None, pairwise=None, ddof=1, **kwargs):
996997
# only default unset
997998
pairwise = True if pairwise is None else pairwise
998999
other = self._shallow_copy(other)
999-
window = self._get_window(other)
1000+
if self.is_freq_type:
1001+
window = self.win_freq
1002+
else:
1003+
window = self._get_window(other)
10001004

10011005
def _get_cov(X, Y):
10021006
# GH #12373 : rolling functions error on float32 data
@@ -1088,6 +1092,7 @@ def validate(self):
10881092
"based windows")
10891093

10901094
# this will raise ValueError on non-fixed freqs
1095+
self.win_freq = self.window
10911096
self.window = freq.nanos
10921097
self.win_type = 'freq'
10931098

pandas/tests/test_window.py

+12
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,18 @@ def test_min_periods(self):
33963396
result = df.rolling('2s', min_periods=1).sum()
33973397
tm.assert_frame_equal(result, expected)
33983398

3399+
def test_cov(self):
3400+
3401+
df = self.regular
3402+
3403+
result = df.rolling('2s', min_periods=1).cov()
3404+
expected = df.rolling(2, min_periods=1).cov()
3405+
tm.assert_frame_equal(result, expected)
3406+
3407+
result = df.rolling('3s', min_periods=1).cov()
3408+
expected = df.rolling(3, min_periods=1).cov()
3409+
tm.assert_frame_equal(result, expected)
3410+
33993411
def test_closed(self):
34003412

34013413
# xref GH13965

0 commit comments

Comments
 (0)