Skip to content

Commit 348dc2d

Browse files
authored
[TST]: Wrong Corr with Timedelta index (#36454)
1 parent 57c06d9 commit 348dc2d

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ Groupby/resample/rolling
499499
- Bug in :meth:`DataFrame.groupby.rolling` returning wrong values with partial centered window (:issue:`36040`).
500500
- Bug in :meth:`DataFrameGroupBy.rolling` returned wrong values with timeaware window containing ``NaN``. Raises ``ValueError`` because windows are not monotonic now (:issue:`34617`)
501501
- Bug in :meth:`Rolling.__iter__` where a ``ValueError`` was not raised when ``min_periods`` was larger than ``window`` (:issue:`37156`)
502+
- Using :meth:`Rolling.var()` instead of :meth:`Rolling.std()` avoids numerical issues for :meth:`Rolling.corr()` when :meth:`Rolling.var()` is still within floating point precision while :meth:`Rolling.std()` is not (:issue:`31286`)
502503

503504
Reshaping
504505
^^^^^^^^^

pandas/core/window/rolling.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1906,8 +1906,10 @@ def _get_corr(a, b):
19061906
b = b.rolling(
19071907
window=window, min_periods=self.min_periods, center=self.center
19081908
)
1909-
1910-
return a.cov(b, **kwargs) / (a.std(**kwargs) * b.std(**kwargs))
1909+
# GH 31286: Through using var instead of std we can avoid numerical
1910+
# issues when the result of var is withing floating proint precision
1911+
# while std is not.
1912+
return a.cov(b, **kwargs) / (a.var(**kwargs) * b.var(**kwargs)) ** 0.5
19111913

19121914
return flex_binary_moment(
19131915
self._selected_obj, other._selected_obj, _get_corr, pairwise=bool(pairwise)

pandas/tests/window/test_rolling.py

+14
Original file line numberDiff line numberDiff line change
@@ -1073,3 +1073,17 @@ def get_window_bounds(self, num_values, min_periods, center, closed):
10731073
result = getattr(df.rolling(indexer), method)()
10741074
expected = DataFrame({"values": expected})
10751075
tm.assert_frame_equal(result, expected)
1076+
1077+
1078+
@pytest.mark.parametrize(
1079+
("index", "window"),
1080+
[([0, 1, 2, 3, 4], 2), (pd.date_range("2001-01-01", freq="D", periods=5), "2D")],
1081+
)
1082+
def test_rolling_corr_timedelta_index(index, window):
1083+
# GH: 31286
1084+
x = Series([1, 2, 3, 4, 5], index=index)
1085+
y = x.copy()
1086+
x[0:2] = 0.0
1087+
result = x.rolling(window).corr(y)
1088+
expected = Series([np.nan, np.nan, 1, 1, 1], index=index)
1089+
tm.assert_almost_equal(result, expected)

0 commit comments

Comments
 (0)