Skip to content

BUG: Incorrect handling of rolling.cov with offset window #16244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -80,6 +80,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()
Copy link
Contributor

@jreback jreback May 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also test again using ss.rolling(2).cov() (will be the same for this case).

I don't know if we have any tests for a non-regular date range, where freq=2 and freq='2h' are actually different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test added

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)