From c1b4cd53ba41f35774d9fdd3a0bf14e6840de629 Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Sat, 13 May 2023 15:11:23 +0000 Subject: [PATCH 1/7] deprecated 'quantile' arg in Rolling.quantile --- pandas/core/window/rolling.py | 17 +++++++++++++++-- pandas/tests/window/test_rolling_quantile.py | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 57fda0a84c751..bc97ec63058a5 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -18,6 +18,7 @@ Sized, cast, ) +import warnings import numpy as np @@ -29,6 +30,7 @@ from pandas.compat._optional import import_optional_dependency from pandas.errors import DataError from pandas.util._decorators import doc +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( ensure_float64, @@ -2426,12 +2428,23 @@ def kurt(self, numeric_only: bool = False): ) def quantile( self, - quantile: float, + q: float = None, interpolation: QuantileInterpolation = "linear", numeric_only: bool = False, + *, + quantile: float = None, ): + if quantile is not None: + warnings.warn( + "The 'quantile' argument in Rolling.quantile " + "has been deprecated. Use 'q' instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + q = quantile if q is None else q + return super().quantile( - quantile=quantile, + quantile=q, interpolation=interpolation, numeric_only=numeric_only, ) diff --git a/pandas/tests/window/test_rolling_quantile.py b/pandas/tests/window/test_rolling_quantile.py index e78e997f220b5..93dfdd9f0abc5 100644 --- a/pandas/tests/window/test_rolling_quantile.py +++ b/pandas/tests/window/test_rolling_quantile.py @@ -173,3 +173,14 @@ def test_center_reindex_frame(frame, q): ) frame_rs = frame.rolling(window=25, center=True).quantile(q) tm.assert_frame_equal(frame_xp, frame_rs) + + +def test_arg_quantile_deprecated(): + # GH #52550 + s = Series([1, 2, 3, 4]) + msg = ( + "The 'quantile' argument in Rolling.quantile " + "has been deprecated. Use 'q' instead." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + s.rolling(2).quantile(quantile=0.4, interpolation="lower") From ea70562a87c4bea8e97569755c8b122277b9951a Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Sat, 13 May 2023 19:54:51 +0000 Subject: [PATCH 2/7] renamed keyword 'quantile' to 'q' in Rolling.quantile --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/window/rolling.py | 30 +++++++++----------- pandas/tests/window/test_rolling_quantile.py | 11 +++---- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 52fc8512c9db3..15a20e8779f8b 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -219,6 +219,7 @@ Deprecations ~~~~~~~~~~~~ - Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`) - Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`) +- Deprecated 'quantile' keyword in :meth:`Rolling.quantile`, renamed as 'q' instead (:issue:`52550`) - Deprecated :meth:`.DataFrameGroupBy.apply` and methods on the objects returned by :meth:`.DataFrameGroupBy.resample` operating on the grouping column(s); select the columns to operate on after groupby to either explicitly include or exclude the groupings and avoid the ``FutureWarning`` (:issue:`7155`) - Deprecated :meth:`.Groupby.all` and :meth:`.GroupBy.any` with datetime64 or :class:`PeriodDtype` values, matching the :class:`Series` and :class:`DataFrame` deprecations (:issue:`34479`) - Deprecated :meth:`Categorical.to_list`, use ``obj.tolist()`` instead (:issue:`51254`) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index bc97ec63058a5..741cdf59daf8c 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -18,7 +18,6 @@ Sized, cast, ) -import warnings import numpy as np @@ -29,8 +28,10 @@ import pandas._libs.window.aggregations as window_aggregations from pandas.compat._optional import import_optional_dependency from pandas.errors import DataError -from pandas.util._decorators import doc -from pandas.util._exceptions import find_stack_level +from pandas.util._decorators import ( + deprecate_kwarg, + doc, +) from pandas.core.dtypes.common import ( ensure_float64, @@ -2384,8 +2385,8 @@ def kurt(self, numeric_only: bool = False): create_section_header("Parameters"), dedent( """ - quantile : float - Quantile to compute. 0 <= quantile <= 1. + q : float + Quantile to compute. 0 <= q <= 1. interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}} This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points `i` and `j`: @@ -2396,6 +2397,11 @@ def kurt(self, numeric_only: bool = False): * higher: `j`. * nearest: `i` or `j` whichever is nearest. * midpoint: (`i` + `j`) / 2. + cols : float + Alias for keyword 'q', kwargs only. + + .. deprecated:: 2.1.0 + Use keyword 'q' instead. """ ).replace("\n", "", 1), kwargs_numeric_only, @@ -2426,23 +2432,13 @@ def kurt(self, numeric_only: bool = False): aggregation_description="quantile", agg_method="quantile", ) + @deprecate_kwarg(old_arg_name="quantile", new_arg_name="q") def quantile( self, - q: float = None, + q: float, interpolation: QuantileInterpolation = "linear", numeric_only: bool = False, - *, - quantile: float = None, ): - if quantile is not None: - warnings.warn( - "The 'quantile' argument in Rolling.quantile " - "has been deprecated. Use 'q' instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - q = quantile if q is None else q - return super().quantile( quantile=q, interpolation=interpolation, diff --git a/pandas/tests/window/test_rolling_quantile.py b/pandas/tests/window/test_rolling_quantile.py index 93dfdd9f0abc5..3ea3c8060be56 100644 --- a/pandas/tests/window/test_rolling_quantile.py +++ b/pandas/tests/window/test_rolling_quantile.py @@ -175,12 +175,9 @@ def test_center_reindex_frame(frame, q): tm.assert_frame_equal(frame_xp, frame_rs) -def test_arg_quantile_deprecated(): +def test_keyword_quantile_deprecated(): # GH #52550 + # deprecate keyword 'quantile' of Rolling.quantile, rename as 'q' s = Series([1, 2, 3, 4]) - msg = ( - "The 'quantile' argument in Rolling.quantile " - "has been deprecated. Use 'q' instead." - ) - with tm.assert_produces_warning(FutureWarning, match=msg): - s.rolling(2).quantile(quantile=0.4, interpolation="lower") + with tm.assert_produces_warning(FutureWarning): + s.rolling(2).quantile(quantile=0.4) From 4abfb99e53317a4df8f06fe3f734c42f5b42eef7 Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Sat, 13 May 2023 20:47:31 +0000 Subject: [PATCH 3/7] fixed test --- pandas/tests/window/test_rolling_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/window/test_rolling_functions.py b/pandas/tests/window/test_rolling_functions.py index c4519701da7ab..c42f13d4b2235 100644 --- a/pandas/tests/window/test_rolling_functions.py +++ b/pandas/tests/window/test_rolling_functions.py @@ -340,7 +340,7 @@ def test_center_reindex_frame(frame, roll_func, kwargs, minp, fill_value): lambda x: x.rolling(window=10, min_periods=5).var(), lambda x: x.rolling(window=10, min_periods=5).skew(), lambda x: x.rolling(window=10, min_periods=5).kurt(), - lambda x: x.rolling(window=10, min_periods=5).quantile(quantile=0.5), + lambda x: x.rolling(window=10, min_periods=5).quantile(q=0.5), lambda x: x.rolling(window=10, min_periods=5).median(), lambda x: x.rolling(window=10, min_periods=5).apply(sum, raw=False), lambda x: x.rolling(window=10, min_periods=5).apply(sum, raw=True), From 734384a832719069ab1b9a8950d23cf415feaf03 Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Sun, 14 May 2023 13:02:01 +0000 Subject: [PATCH 4/7] docstring modified, removed unnecessary descriptions and typos --- pandas/core/window/rolling.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 741cdf59daf8c..d43273e039cb9 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -2385,8 +2385,11 @@ def kurt(self, numeric_only: bool = False): create_section_header("Parameters"), dedent( """ - q : float - Quantile to compute. 0 <= q <= 1. + quantile : float + Quantile to compute. 0 <= quantile <= 1. + + .. deprecated:: 2.1.0 + This will be renamed to 'q'. interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}} This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points `i` and `j`: @@ -2397,11 +2400,6 @@ def kurt(self, numeric_only: bool = False): * higher: `j`. * nearest: `i` or `j` whichever is nearest. * midpoint: (`i` + `j`) / 2. - cols : float - Alias for keyword 'q', kwargs only. - - .. deprecated:: 2.1.0 - Use keyword 'q' instead. """ ).replace("\n", "", 1), kwargs_numeric_only, From d5730c9f87295fac9082fefdd1a9f54c0301e3f3 Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Sun, 14 May 2023 16:59:08 +0000 Subject: [PATCH 5/7] resolved conversations --- pandas/core/window/rolling.py | 2 +- pandas/tests/window/test_rolling_quantile.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index d43273e039cb9..8a2e5770b1449 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -2389,7 +2389,7 @@ def kurt(self, numeric_only: bool = False): Quantile to compute. 0 <= quantile <= 1. .. deprecated:: 2.1.0 - This will be renamed to 'q'. + This will be renamed to 'q' in a future version. interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}} This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points `i` and `j`: diff --git a/pandas/tests/window/test_rolling_quantile.py b/pandas/tests/window/test_rolling_quantile.py index 3ea3c8060be56..2eba0b36ed187 100644 --- a/pandas/tests/window/test_rolling_quantile.py +++ b/pandas/tests/window/test_rolling_quantile.py @@ -177,7 +177,6 @@ def test_center_reindex_frame(frame, q): def test_keyword_quantile_deprecated(): # GH #52550 - # deprecate keyword 'quantile' of Rolling.quantile, rename as 'q' s = Series([1, 2, 3, 4]) with tm.assert_produces_warning(FutureWarning): s.rolling(2).quantile(quantile=0.4) From 767555ed2b04878111e94dafcace0ee18389ef01 Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Tue, 16 May 2023 02:27:21 +0000 Subject: [PATCH 6/7] deprecate on Expanding as well --- pandas/core/window/expanding.py | 13 ++++++++++--- pandas/tests/window/test_expanding.py | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index fc22ff6c0522a..0d3ad3ee2c99f 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -7,7 +7,10 @@ Callable, ) -from pandas.util._decorators import doc +from pandas.util._decorators import ( + deprecate_kwarg, + doc, +) from pandas.core.indexers.objects import ( BaseIndexer, @@ -575,6 +578,9 @@ def kurt(self, numeric_only: bool = False): """ quantile : float Quantile to compute. 0 <= quantile <= 1. + + .. deprecated:: 2.1.0 + This will be renamed to 'q' in a future version. interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}} This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points `i` and `j`: @@ -596,14 +602,15 @@ def kurt(self, numeric_only: bool = False): aggregation_description="quantile", agg_method="quantile", ) + @deprecate_kwarg(old_arg_name="quantile", new_arg_name="q") def quantile( self, - quantile: float, + q: float, interpolation: QuantileInterpolation = "linear", numeric_only: bool = False, ): return super().quantile( - quantile=quantile, + quantile=q, interpolation=interpolation, numeric_only=numeric_only, ) diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py index b4c5edeae949b..03af2e63da165 100644 --- a/pandas/tests/window/test_expanding.py +++ b/pandas/tests/window/test_expanding.py @@ -707,3 +707,10 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): op2 = getattr(expanding2, kernel) expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) + + +def test_keyword_quantile_deprecated(): + # GH #52550 + ser = Series([1, 2, 3, 4]) + with tm.assert_produces_warning(FutureWarning): + ser.expanding().quantile(quantile=0.5) From 720ad9d1ff405fdd6db92aab3c4ef857f3f60677 Mon Sep 17 00:00:00 2001 From: Yao Xiao Date: Tue, 16 May 2023 17:57:14 +0000 Subject: [PATCH 7/7] resolved conversations; renamed in parent --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/window/expanding.py | 2 +- pandas/core/window/rolling.py | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index cd9ba80f1d597..60ae2336c8668 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -219,7 +219,7 @@ Deprecations ~~~~~~~~~~~~ - Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`) - Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`) -- Deprecated 'quantile' keyword in :meth:`Rolling.quantile`, renamed as 'q' instead (:issue:`52550`) +- Deprecated 'quantile' keyword in :meth:`Rolling.quantile` and :meth:`Expanding.quantile`, renamed as 'q' instead (:issue:`52550`) - Deprecated :meth:`.DataFrameGroupBy.apply` and methods on the objects returned by :meth:`.DataFrameGroupBy.resample` operating on the grouping column(s); select the columns to operate on after groupby to either explicitly include or exclude the groupings and avoid the ``FutureWarning`` (:issue:`7155`) - Deprecated :meth:`.Groupby.all` and :meth:`.GroupBy.any` with datetime64 or :class:`PeriodDtype` values, matching the :class:`Series` and :class:`DataFrame` deprecations (:issue:`34479`) - Deprecated :meth:`Categorical.to_list`, use ``obj.tolist()`` instead (:issue:`51254`) diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 0d3ad3ee2c99f..19dd98851611f 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -610,7 +610,7 @@ def quantile( numeric_only: bool = False, ): return super().quantile( - quantile=q, + q=q, interpolation=interpolation, numeric_only=numeric_only, ) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 8a2e5770b1449..86b4e399fa461 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1604,18 +1604,18 @@ def kurt(self, numeric_only: bool = False): def quantile( self, - quantile: float, + q: float, interpolation: QuantileInterpolation = "linear", numeric_only: bool = False, ): - if quantile == 1.0: + if q == 1.0: window_func = window_aggregations.roll_max - elif quantile == 0.0: + elif q == 0.0: window_func = window_aggregations.roll_min else: window_func = partial( window_aggregations.roll_quantile, - quantile=quantile, + quantile=q, interpolation=interpolation, ) @@ -2438,7 +2438,7 @@ def quantile( numeric_only: bool = False, ): return super().quantile( - quantile=q, + q=q, interpolation=interpolation, numeric_only=numeric_only, )