From 352445c34cb4772f6b329c9d7cd2d6afb9900fa5 Mon Sep 17 00:00:00 2001 From: Paul Siegel Date: Tue, 12 Nov 2019 01:47:31 -0500 Subject: [PATCH 1/5] BUG: `RollingGroupBy.quantile` ignores `interpolation` keyword argument (#28779) --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/core/window/rolling.py | 4 +++- pandas/tests/window/test_grouper.py | 15 ++++++++------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index cd012fe755337..3436270ac1bc5 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -427,6 +427,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`) - Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`) - Bug in :meth:`DataFrame.groupby` losing column name information when grouping by a categorical column (:issue:`28787`) +- Bug in :meth:`RollingGroupby.quantile` ignoring ``interpolation`` keyword argument (:issue:`28779`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index caf2f9e1c9dd3..de550132a9f6d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1499,7 +1499,9 @@ def f(arg, *args, **kwargs): interpolation, ) - return self._apply(f, "quantile", quantile=quantile, **kwargs) + return self._apply( + f, "quantile", quantile=quantile, interpolation=interpolation, **kwargs + ) _shared_docs[ "cov" diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index b726bd3e3c8a7..9c9dbf11bcef8 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -59,7 +59,6 @@ def test_rolling(self): r = g.rolling(window=4) for f in ["sum", "mean", "min", "max", "count", "kurt", "skew"]: - result = getattr(r, f)() expected = g.apply(lambda x: getattr(x.rolling(4), f)()) tm.assert_frame_equal(result, expected) @@ -69,9 +68,10 @@ def test_rolling(self): expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1)) tm.assert_frame_equal(result, expected) - result = r.quantile(0.5) - expected = g.apply(lambda x: x.rolling(4).quantile(0.5)) - tm.assert_frame_equal(result, expected) + for f in ["linear", "lower", "higher", "midpoint", "nearest"]: + result = r.quantile(0.4, interpolation=f) + expected = g.apply(lambda x: x.rolling(4).quantile(0.4, interpolation=f)) + tm.assert_frame_equal(result, expected) def test_rolling_corr_cov(self): g = self.frame.groupby("A") @@ -141,9 +141,10 @@ def test_expanding(self): expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0)) tm.assert_frame_equal(result, expected) - result = r.quantile(0.5) - expected = g.apply(lambda x: x.expanding().quantile(0.5)) - tm.assert_frame_equal(result, expected) + for f in ["linear", "lower", "higher", "midpoint", "nearest"]: + result = r.quantile(0.4, interpolation=f) + expected = g.apply(lambda x: x.expanding().quantile(0.4, interpolation=f)) + tm.assert_frame_equal(result, expected) def test_expanding_corr_cov(self): g = self.frame.groupby("A") From ed7aa3faec30a9fce3515472e3c8278bd5489ef9 Mon Sep 17 00:00:00 2001 From: Paul Siegel Date: Sun, 17 Nov 2019 10:43:36 -0500 Subject: [PATCH 2/5] Create new test `test_rolling_quantile` --- pandas/tests/window/test_grouper.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index 269349355cc58..50f319976389d 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -74,6 +74,15 @@ def test_rolling(self): expected = g.apply(lambda x: x.rolling(4).quantile(0.4, interpolation=f)) tm.assert_frame_equal(result, expected) + def test_rolling_quantile(self, interpolation): + g = self.frame.groupby("A") + r = g.rolling(window=4) + result = r.quantile(0.4, interpolation=interpolation) + expected = g.apply( + lambda x: x.rolling(4).quantile(0.4, interpolation=interpolation) + ) + tm.assert_frame_equal(result, expected) + def test_rolling_corr_cov(self): g = self.frame.groupby("A") r = g.rolling(window=4) From 48cb4e540dbaa581e1679a58c67f197d1050f3d6 Mon Sep 17 00:00:00 2001 From: Paul Siegel Date: Sun, 17 Nov 2019 10:54:16 -0500 Subject: [PATCH 3/5] Parametrize test_rolling_quantile with interpolation parameters previously in test_rolling --- pandas/tests/window/test_grouper.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index 50f319976389d..e412c1f970004 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -69,11 +69,9 @@ def test_rolling(self): expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1)) tm.assert_frame_equal(result, expected) - for f in ["linear", "lower", "higher", "midpoint", "nearest"]: - result = r.quantile(0.4, interpolation=f) - expected = g.apply(lambda x: x.rolling(4).quantile(0.4, interpolation=f)) - tm.assert_frame_equal(result, expected) - + @pytest.mark.parametrize( + "interpolation", ["linear", "lower", "higher", "midpoint", "nearest"] + ) def test_rolling_quantile(self, interpolation): g = self.frame.groupby("A") r = g.rolling(window=4) From fde54ce92c43327912cdd2e4d8e8a81ec9413e26 Mon Sep 17 00:00:00 2001 From: Paul Siegel Date: Sun, 17 Nov 2019 12:04:19 -0500 Subject: [PATCH 4/5] add parametrized test `test_expanding_quantile` and removed quantile test from `test_expanding` --- pandas/tests/window/test_grouper.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pandas/tests/window/test_grouper.py b/pandas/tests/window/test_grouper.py index e412c1f970004..189942bc07d2a 100644 --- a/pandas/tests/window/test_grouper.py +++ b/pandas/tests/window/test_grouper.py @@ -149,10 +149,17 @@ def test_expanding(self): expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0)) tm.assert_frame_equal(result, expected) - for f in ["linear", "lower", "higher", "midpoint", "nearest"]: - result = r.quantile(0.4, interpolation=f) - expected = g.apply(lambda x: x.expanding().quantile(0.4, interpolation=f)) - tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize( + "interpolation", ["linear", "lower", "higher", "midpoint", "nearest"] + ) + def test_expanding_quantile(self, interpolation): + g = self.frame.groupby("A") + r = g.expanding() + result = r.quantile(0.4, interpolation=interpolation) + expected = g.apply( + lambda x: x.expanding().quantile(0.4, interpolation=interpolation) + ) + tm.assert_frame_equal(result, expected) def test_expanding_corr_cov(self): g = self.frame.groupby("A") From 61c5142df5edb8cd8332a68b87e064c3761a127f Mon Sep 17 00:00:00 2001 From: Paul Siegel Date: Sun, 17 Nov 2019 13:05:20 -0500 Subject: [PATCH 5/5] Change format of method reference in bug description in whatsnew to be consistent with similar bugs --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index f423db5dda182..8e671f524dcd6 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -436,7 +436,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`) - Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`) - Bug in :meth:`DataFrame.groupby` losing column name information when grouping by a categorical column (:issue:`28787`) -- Bug in :meth:`RollingGroupby.quantile` ignoring ``interpolation`` keyword argument (:issue:`28779`) +- Bug in :meth:`DataFrameGroupBy.rolling().quantile()` ignoring ``interpolation`` keyword argument (:issue:`28779`) Reshaping ^^^^^^^^^