Skip to content

Commit

Permalink
BUG: Incorrect handling of rolling.cov with offset window (pandas-dev…
Browse files Browse the repository at this point in the history
  • Loading branch information
keitakurita authored and jreback committed May 30, 2017
1 parent 7efc4e8 commit 4ca29f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^

- Bug creating datetime rolling window on an empty DataFrame (:issue:`15819`)
- Bug in ``rolling.cov()`` with offset window (:issue:`16058`)


Sparse
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __init__(self, obj, window=None, min_periods=None, freq=None,
self.freq = freq
self.center = center
self.win_type = win_type
self.win_freq = None
self.axis = obj._get_axis_number(axis) if axis is not None else None
self.validate()

Expand Down Expand Up @@ -996,7 +997,12 @@ def cov(self, other=None, pairwise=None, ddof=1, **kwargs):
# only default unset
pairwise = True if pairwise is None else pairwise
other = self._shallow_copy(other)
window = self._get_window(other)

# GH 16058: offset window
if self.is_freq_type:
window = self.win_freq
else:
window = self._get_window(other)

def _get_cov(X, Y):
# GH #12373 : rolling functions error on float32 data
Expand Down Expand Up @@ -1088,6 +1094,7 @@ def validate(self):
"based windows")

# this will raise ValueError on non-fixed freqs
self.win_freq = self.window
self.window = freq.nanos
self.win_type = 'freq'

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3833,3 +3833,26 @@ def test_non_monotonic(self):
df2 = df.sort_values('B')
result = df2.groupby('A').rolling('4s', on='B').C.mean()
tm.assert_series_equal(result, expected)

def test_rolling_cov_offset(self):
# GH16058

idx = pd.date_range('2017-01-01', periods=24, freq='1h')
ss = pd.Series(np.arange(len(idx)), index=idx)

result = ss.rolling('2h').cov()
expected = pd.Series([np.nan] + [0.5 for _ in range(len(idx) - 1)],
index=idx)
tm.assert_series_equal(result, expected)

expected2 = ss.rolling(2, min_periods=1).cov()
tm.assert_series_equal(result, expected2)

result = ss.rolling('3h').cov()
expected = pd.Series([np.nan, 0.5] +
[1.0 for _ in range(len(idx) - 2)],
index=idx)
tm.assert_series_equal(result, expected)

expected2 = ss.rolling(3, min_periods=1).cov()
tm.assert_series_equal(result, expected2)

0 comments on commit 4ca29f4

Please sign in to comment.