From eedfd0edc9a852e3471ef47b1d1e5a164d5b086c Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 12:14:07 +0530 Subject: [PATCH 01/16] deprication of resample --- pandas/core/frame.py | 4 +++- pandas/core/generic.py | 38 +++++++++++++++++++++++++++++++---- pandas/core/series.py | 15 +++++++++++++- pandas/core/window/rolling.py | 28 +++++++++++++++++++++++--- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3a48c3b88e071..74938861704de 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11408,7 +11408,7 @@ def asfreq( def resample( self, rule, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, closed: str | None = None, label: str | None = None, convention: str = "start", @@ -11419,6 +11419,8 @@ def resample( offset: TimedeltaConvertibleTypes | None = None, group_keys: bool | lib.NoDefault = no_default, ) -> Resampler: + print(f'axis={axis}') + print(f'super()={super().resample}') return super().resample( rule=rule, axis=axis, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a4dfb085c766f..dafba278ca9b5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8531,7 +8531,7 @@ def between_time( def resample( self, rule, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, closed: str | None = None, label: str | None = None, convention: str = "start", @@ -8910,8 +8910,23 @@ def resample( Freq: 17T, dtype: int64 """ from pandas.core.resample import get_resampler + if axis is not lib.no_default: + axis = self._get_axis_number(axis) + if axis == 1: + warnings.warn( + "DataFrame.resample with axis=1 is deprecated. Do " + "`frame.T.resample(...)` without axis instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + warnings.warn( + "The 'axis' keyword in DataFrame.resample is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) - axis = self._get_axis_number(axis) return get_resampler( cast("Series | DataFrame", self), freq=rule, @@ -11891,12 +11906,27 @@ def rolling( center: bool_t = False, win_type: str | None = None, on: str | None = None, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, closed: str | None = None, step: int | None = None, method: str = "single", ) -> Window | Rolling: - axis = self._get_axis_number(axis) + if axis is not lib.no_default: + axis = self._get_axis_number(axis) + if axis == 1: + warnings.warn( + "DataFrame.rolling with axis=1 is deprecated. Do " + "`frame.T.rolling(...)` without axis instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + warnings.warn( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) if win_type is not None: return Window( diff --git a/pandas/core/series.py b/pandas/core/series.py index 25cb18b0135fa..bc75987527d0d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5560,7 +5560,7 @@ def asfreq( def resample( self, rule, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, closed: str | None = None, label: str | None = None, convention: str = "start", @@ -5571,6 +5571,19 @@ def resample( offset: TimedeltaConvertibleTypes | None = None, group_keys: bool | lib.NoDefault = no_default, ) -> Resampler: + print(f'dummy axis={axis}') + if axis is not lib.no_default: + warnings.warn( + "resample axis keyword is deprecated and will be removed in a " + "future version. To group on axis=1, use obj.T.resample(...) " + "instead", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + axis = 0 + print(f'axis={axis}') + print(f'super()={super()}') return super().resample( rule=rule, axis=axis, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index b11ff11421ed4..a30696a6ce678 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -20,6 +20,9 @@ ) import numpy as np +import warnings +from pandas.util._exceptions import find_stack_level +from pandas._libs import lib from pandas._libs.tslibs import ( BaseOffset, @@ -122,7 +125,7 @@ def __init__( min_periods: int | None = None, center: bool | None = False, win_type: str | None = None, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, on: str | Index | None = None, closed: str | None = None, step: int | None = None, @@ -138,7 +141,26 @@ def __init__( self.min_periods = min_periods self.center = center self.win_type = win_type - self.axis = obj._get_axis_number(axis) if axis is not None else None + print(f'axis={axis}') + if axis is not lib.no_default: + self.axis = self.obj._get_axis_number(axis) + if self.axis == 1: + warnings.warn( + "DataFrame.window with axis=1 is deprecated. Do " + "`frame.T.window(...)` without axis instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + warnings.warn( + "The 'axis' keyword in DataFrame.window is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + self.axis = 0 + self.method = method self._win_freq_i8: int | None = None if self.on is None: @@ -685,7 +707,7 @@ def __init__( **kwargs, ) -> None: from pandas.core.groupby.ops import BaseGrouper - + print(f'BaseWindowGroupby') if not isinstance(_grouper, BaseGrouper): raise ValueError("Must pass a BaseGrouper object.") self._grouper = _grouper From 0160fedd35aabdd65b3886267417470d548579e9 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 12:21:37 +0530 Subject: [PATCH 02/16] deprication of resample --- pandas/core/frame.py | 2 -- pandas/core/generic.py | 17 +---------------- pandas/core/series.py | 3 --- pandas/core/window/rolling.py | 27 ++------------------------- 4 files changed, 3 insertions(+), 46 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 74938861704de..63bcebd342c31 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11419,8 +11419,6 @@ def resample( offset: TimedeltaConvertibleTypes | None = None, group_keys: bool | lib.NoDefault = no_default, ) -> Resampler: - print(f'axis={axis}') - print(f'super()={super().resample}') return super().resample( rule=rule, axis=axis, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index dafba278ca9b5..85f874719bc05 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -11911,22 +11911,7 @@ def rolling( step: int | None = None, method: str = "single", ) -> Window | Rolling: - if axis is not lib.no_default: - axis = self._get_axis_number(axis) - if axis == 1: - warnings.warn( - "DataFrame.rolling with axis=1 is deprecated. Do " - "`frame.T.rolling(...)` without axis instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - else: - warnings.warn( - "The 'axis' keyword in DataFrame.rolling is deprecated and " - "will be removed in a future version.", - FutureWarning, - stacklevel=find_stack_level(), - ) + axis = self._get_axis_number(axis) if win_type is not None: return Window( diff --git a/pandas/core/series.py b/pandas/core/series.py index bc75987527d0d..0d09674100551 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5571,7 +5571,6 @@ def resample( offset: TimedeltaConvertibleTypes | None = None, group_keys: bool | lib.NoDefault = no_default, ) -> Resampler: - print(f'dummy axis={axis}') if axis is not lib.no_default: warnings.warn( "resample axis keyword is deprecated and will be removed in a " @@ -5582,8 +5581,6 @@ def resample( ) else: axis = 0 - print(f'axis={axis}') - print(f'super()={super()}') return super().resample( rule=rule, axis=axis, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index a30696a6ce678..7e22d58d22d9d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -20,9 +20,6 @@ ) import numpy as np -import warnings -from pandas.util._exceptions import find_stack_level -from pandas._libs import lib from pandas._libs.tslibs import ( BaseOffset, @@ -125,7 +122,7 @@ def __init__( min_periods: int | None = None, center: bool | None = False, win_type: str | None = None, - axis: Axis | lib.NoDefault = lib.no_default, + axis: Axis = 0, on: str | Index | None = None, closed: str | None = None, step: int | None = None, @@ -141,26 +138,7 @@ def __init__( self.min_periods = min_periods self.center = center self.win_type = win_type - print(f'axis={axis}') - if axis is not lib.no_default: - self.axis = self.obj._get_axis_number(axis) - if self.axis == 1: - warnings.warn( - "DataFrame.window with axis=1 is deprecated. Do " - "`frame.T.window(...)` without axis instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - else: - warnings.warn( - "The 'axis' keyword in DataFrame.window is deprecated and " - "will be removed in a future version.", - FutureWarning, - stacklevel=find_stack_level(), - ) - else: - self.axis = 0 - + self.axis = obj._get_axis_number(axis) if axis is not None else None self.method = method self._win_freq_i8: int | None = None if self.on is None: @@ -707,7 +685,6 @@ def __init__( **kwargs, ) -> None: from pandas.core.groupby.ops import BaseGrouper - print(f'BaseWindowGroupby') if not isinstance(_grouper, BaseGrouper): raise ValueError("Must pass a BaseGrouper object.") self._grouper = _grouper From 78072b6d84633608cf01c793ef9ade116be7ef75 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 12:22:52 +0530 Subject: [PATCH 03/16] deprication of resample --- pandas/core/generic.py | 2 +- pandas/core/window/rolling.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 85f874719bc05..b127ce8938a44 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -11906,7 +11906,7 @@ def rolling( center: bool_t = False, win_type: str | None = None, on: str | None = None, - axis: Axis | lib.NoDefault = lib.no_default, + axis: Axis = 0, closed: str | None = None, step: int | None = None, method: str = "single", diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 7e22d58d22d9d..b11ff11421ed4 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -685,6 +685,7 @@ def __init__( **kwargs, ) -> None: from pandas.core.groupby.ops import BaseGrouper + if not isinstance(_grouper, BaseGrouper): raise ValueError("Must pass a BaseGrouper object.") self._grouper = _grouper From 2e8ae9e8f288eab88fb589633a85e3dc572a6fb8 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 12:39:54 +0530 Subject: [PATCH 04/16] deprication of resample --- pandas/core/generic.py | 2 ++ pandas/core/series.py | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b127ce8938a44..275da88a7489c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8926,6 +8926,8 @@ def resample( FutureWarning, stacklevel=find_stack_level(), ) + else: + axis = 0 return get_resampler( cast("Series | DataFrame", self), diff --git a/pandas/core/series.py b/pandas/core/series.py index 0d09674100551..51838d605255f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5579,8 +5579,7 @@ def resample( FutureWarning, stacklevel=find_stack_level(), ) - else: - axis = 0 + return super().resample( rule=rule, axis=axis, From 8d2f2dd3a90672db3fe0045c524195628cf3dd0a Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 13:48:13 +0530 Subject: [PATCH 05/16] deprication of resample --- pandas/core/generic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 275da88a7489c..0dc6ba7433130 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8558,6 +8558,8 @@ def resample( Which axis to use for up- or down-sampling. For `Series` this parameter is unused and defaults to 0. Must be `DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`. + .. deprecated:: 2.0.0 + Use frame.T.resample(...) instead. closed : {{'right', 'left'}}, default None Which side of bin interval is closed. The default is 'left' for all frequency offsets except for 'M', 'A', 'Q', 'BM', From bef38c99cb01605cdf39ddd80bacb1c7d6c88646 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 14:01:28 +0530 Subject: [PATCH 06/16] pre commit fixes --- pandas/core/generic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0dc6ba7433130..651d7de8ae87e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8912,6 +8912,7 @@ def resample( Freq: 17T, dtype: int64 """ from pandas.core.resample import get_resampler + if axis is not lib.no_default: axis = self._get_axis_number(axis) if axis == 1: From cd2015011f4ddeb2800a226c6a64a46f64968ba9 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 20:48:49 +0530 Subject: [PATCH 07/16] updated testcases & whatsnew --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/tests/resample/test_datetime_index.py | 24 ++++++++++++-------- pandas/tests/resample/test_resample_api.py | 10 +++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 4f6a6a8f0487b..448ee5f33c37a 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -99,6 +99,7 @@ Deprecations - Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`) - Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`) - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) +- Deprecated ``axis=1`` in :meth:`DataFrame.resample`, do ``frame.T.resample(...)`` instead (:issue:`51778`) - .. --------------------------------------------------------------------------- diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 3c46887dad859..eb60f3ca4ceee 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -641,12 +641,16 @@ def test_resample_dup_index(): columns=[Period(year=2000, month=i + 1, freq="M") for i in range(12)], ) df.iloc[3, :] = np.nan - result = df.resample("Q", axis=1).mean() - msg = "DataFrame.groupby with axis=1 is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean() - expected.columns = [Period(year=2000, quarter=i + 1, freq="Q") for i in range(4)] - tm.assert_frame_equal(result, expected) + warning_msg = "DataFrame.resample with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.resample("Q", axis=1).mean() + msg = "DataFrame.groupby with axis=1 is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean() + expected.columns = [ + Period(year=2000, quarter=i + 1, freq="Q") for i in range(4) + ] + tm.assert_frame_equal(result, expected) def test_resample_reresample(unit): @@ -729,9 +733,11 @@ def test_resample_axis1(unit): rng = date_range("1/1/2000", "2/29/2000").as_unit(unit) df = DataFrame(np.random.randn(3, len(rng)), columns=rng, index=["a", "b", "c"]) - result = df.resample("M", axis=1).mean() - expected = df.T.resample("M").mean().T - tm.assert_frame_equal(result, expected) + warning_msg = "DataFrame.resample with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.resample("M", axis=1).mean() + expected = df.T.resample("M").mean().T + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("freq", ["t", "5t", "15t", "30t", "4h", "12h"]) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index 7ce4f482b6414..e4266fbdab48a 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -570,9 +570,13 @@ def test_multi_agg_axis_1_raises(func): index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D") index.name = "date" df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T - res = df.resample("M", axis=1) - with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): - res.agg(func) + warning_msg = "DataFrame.resample with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + res = df.resample("M", axis=1) + with pytest.raises( + NotImplementedError, match="axis other than 0 is not supported" + ): + res.agg(func) def test_agg_nested_dicts(): From 559191bcefc77bc6f71eef8dbd265da5b544dbb6 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Sat, 11 Mar 2023 21:28:18 +0530 Subject: [PATCH 08/16] added more testcases --- pandas/core/series.py | 5 ++-- pandas/tests/resample/test_resample_api.py | 30 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 51838d605255f..e0ce60a42a0d4 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5573,9 +5573,8 @@ def resample( ) -> Resampler: if axis is not lib.no_default: warnings.warn( - "resample axis keyword is deprecated and will be removed in a " - "future version. To group on axis=1, use obj.T.resample(...) " - "instead", + "Series resample axis keyword is deprecated and will be removed in a " + "future version.", FutureWarning, stacklevel=find_stack_level(), ) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index e4266fbdab48a..a9507d8772811 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -977,3 +977,33 @@ def test_args_kwargs_depr(method, raises): with tm.assert_produces_warning(FutureWarning, match=warn_msg): with pytest.raises(TypeError, match=error_msg_type): func(*args, 1, 2, 3) + + +def test_df_axis_param_depr(): + np.random.seed(1234) + index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D") + index.name = "date" + df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T + + # Deprication error when axis=1 is explicitly passed + warning_msg = "DataFrame.resample with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.resample("M", axis=1) + + # Deprication error when axis=0 is explicitly passed + df = df.T + warning_msg = ( + "The 'axis' keyword in DataFrame.resample is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.resample("M", axis=0) + + +def test_series_axis_param_depr(): + warning_msg = ( + "Series resample axis keyword is deprecated and will be removed in a " + "future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + test_series.resample("H", axis=0) From d130495decb9014f048cdce9518fe34c78613515 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Tue, 14 Mar 2023 00:08:09 +0530 Subject: [PATCH 09/16] addressing review comments --- pandas/tests/resample/test_datetime_index.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 48eba43fd6cf9..cbf530e995c43 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -644,13 +644,12 @@ def test_resample_dup_index(): warning_msg = "DataFrame.resample with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=warning_msg): result = df.resample("Q", axis=1).mean() - msg = "DataFrame.groupby with axis=1 is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean() - expected.columns = [ - Period(year=2000, quarter=i + 1, freq="Q") for i in range(4) - ] - tm.assert_frame_equal(result, expected) + + msg = "DataFrame.groupby with axis=1 is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean() + expected.columns = [Period(year=2000, quarter=i + 1, freq="Q") for i in range(4)] + tm.assert_frame_equal(result, expected) def test_resample_reresample(unit): @@ -736,8 +735,8 @@ def test_resample_axis1(unit): warning_msg = "DataFrame.resample with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=warning_msg): result = df.resample("M", axis=1).mean() - expected = df.T.resample("M").mean().T - tm.assert_frame_equal(result, expected) + expected = df.T.resample("M").mean().T + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("freq", ["t", "5t", "15t", "30t", "4h", "12h"]) From 9d8334e1843c62f4fa783d24d0de17cd7f7232c7 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Fri, 17 Mar 2023 02:24:38 +0530 Subject: [PATCH 10/16] deprication of axis param in rolling functions --- pandas/core/generic.py | 65 ++++++++++++++++-- pandas/tests/window/test_api.py | 28 +++++--- pandas/tests/window/test_apply.py | 5 +- pandas/tests/window/test_ewm.py | 19 +++++- pandas/tests/window/test_expanding.py | 54 ++++++++++++--- pandas/tests/window/test_rolling.py | 66 ++++++++++++++++--- pandas/tests/window/test_timeseries_window.py | 11 +++- pandas/tests/window/test_win_type.py | 5 +- 8 files changed, 215 insertions(+), 38 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 651d7de8ae87e..4116b3a64e460 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -11911,12 +11911,29 @@ def rolling( center: bool_t = False, win_type: str | None = None, on: str | None = None, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, closed: str | None = None, step: int | None = None, method: str = "single", ) -> Window | Rolling: - axis = self._get_axis_number(axis) + if axis is not lib.no_default: + axis = self._get_axis_number(axis) + if axis == 1: + warnings.warn( + "DataFrame.rolling with axis=1 is deprecated. Do " + "`frame.T.rolling(...)` without axis instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + warnings.warn( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + axis = 0 if win_type is not None: return Window( @@ -11950,10 +11967,28 @@ def rolling( def expanding( self, min_periods: int = 1, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, method: str = "single", ) -> Expanding: - axis = self._get_axis_number(axis) + if axis is not lib.no_default: + axis = self._get_axis_number(axis) + if axis == 1: + warnings.warn( + "DataFrame.expanding with axis=1 is deprecated. Do " + "`frame.T.expanding(...)` without axis instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + warnings.warn( + "The 'axis' keyword in DataFrame.expanding is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + axis = 0 + return Expanding(self, min_periods=min_periods, axis=axis, method=method) @final @@ -11967,11 +12002,29 @@ def ewm( min_periods: int | None = 0, adjust: bool_t = True, ignore_na: bool_t = False, - axis: Axis = 0, + axis: Axis | lib.NoDefault = lib.no_default, times: np.ndarray | DataFrame | Series | None = None, method: str = "single", ) -> ExponentialMovingWindow: - axis = self._get_axis_number(axis) + if axis is not lib.no_default: + axis = self._get_axis_number(axis) + if axis == 1: + warnings.warn( + "DataFrame.ewm with axis=1 is deprecated. Do " + "`frame.T.ewm(...)` without axis instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + warnings.warn( + "The 'axis' keyword in DataFrame.ewm is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + axis = 0 + return ExponentialMovingWindow( self, com=com, diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index 68b3f2b9f8e77..e35a92b551d1f 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -129,7 +129,10 @@ def test_agg(step): def test_multi_axis_1_raises(func): # GH#46904 df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]}) - r = df.rolling(window=3, axis=1) + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + r = df.rolling(window=3, axis=1) with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): r.agg(func) @@ -344,7 +347,14 @@ def test_dont_modify_attributes_after_methods( def test_centered_axis_validation(step): # ok - Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean() + axis_0_warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) + axis_1_warning_msg = "DataFrame.rolling with axis=1 is deprecated." + + with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg): + Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean() # bad axis msg = "No axis named 1 for object type Series" @@ -352,12 +362,14 @@ def test_centered_axis_validation(step): Series(np.ones(10)).rolling(window=3, center=True, axis=1, step=step).mean() # ok ok - DataFrame(np.ones((10, 10))).rolling( - window=3, center=True, axis=0, step=step - ).mean() - DataFrame(np.ones((10, 10))).rolling( - window=3, center=True, axis=1, step=step - ).mean() + with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg): + DataFrame(np.ones((10, 10))).rolling( + window=3, center=True, axis=0, step=step + ).mean() + with tm.assert_produces_warning(FutureWarning, match=axis_1_warning_msg): + DataFrame(np.ones((10, 10))).rolling( + window=3, center=True, axis=1, step=step + ).mean() # bad axis msg = "No axis named 2 for object type DataFrame" diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py index 56c2432ab1429..82455a6c8c036 100644 --- a/pandas/tests/window/test_apply.py +++ b/pandas/tests/window/test_apply.py @@ -321,6 +321,9 @@ def test_center_reindex_frame(raw, frame): def test_axis1(raw): # GH 45912 df = DataFrame([1, 2]) - result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw) + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw) expected = DataFrame([1.0, 2.0]) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index 9870c02692a7f..1ed5aa85edcb3 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -198,7 +198,9 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype): df = DataFrame( {0: range(5), 1: range(6, 11), 2: range(10, 20, 2)}, dtype=float_numpy_dtype ) - e = df.ewm(alpha=0.5, axis=1) + warning_msg = "DataFrame.ewm with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + e = df.ewm(alpha=0.5, axis=1) result = getattr(e, func)() tm.assert_frame_equal(result, expected) @@ -719,3 +721,18 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): op2 = getattr(ewm2, kernel) expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) + + +def test_df_ewn_axis_param_depr(): + df = DataFrame({"a": [1], "b": 2, "c": 3}) + + warning_msg = ( + "The 'axis' keyword in DataFrame.ewm is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.ewm(span=2, axis=0) + + warning_msg = "DataFrame.ewm with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.ewm(span=2, axis=1) diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py index 638df10fa6d50..48f7823e174e3 100644 --- a/pandas/tests/window/test_expanding.py +++ b/pandas/tests/window/test_expanding.py @@ -82,17 +82,22 @@ def test_missing_minp_zero(): def test_expanding_axis(axis_frame): # see gh-23372. df = DataFrame(np.ones((10, 20))) - axis = df._get_axis_number(axis_frame) - + axis = df._get_axis_number(axis_frame) if axis == 0: expected = DataFrame( {i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20)} ) + warning_msg = ( + "The 'axis' keyword in DataFrame.expanding is deprecated and " + "will be removed in a future version." + ) else: # axis == 1 expected = DataFrame([[np.nan] * 2 + [float(i) for i in range(3, 21)]] * 10) + warning_msg = "DataFrame.expanding with axis=1 is deprecated." - result = df.expanding(3, axis=axis_frame).sum() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.expanding(3, axis=axis_frame).sum() tm.assert_frame_equal(result, expected) @@ -323,7 +328,12 @@ def test_expanding_corr_pairwise(frame): ) def test_expanding_func(func, static_comp, frame_or_series): data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10)) - result = getattr(data.expanding(min_periods=1, axis=0), func)() + warning_msg = ( + "The 'axis' keyword in DataFrame.expanding is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = getattr(data.expanding(min_periods=1, axis=0), func)() assert isinstance(result, frame_or_series) expected = static_comp(data[:11]) @@ -341,26 +351,36 @@ def test_expanding_func(func, static_comp, frame_or_series): def test_expanding_min_periods(func, static_comp): ser = Series(np.random.randn(50)) - result = getattr(ser.expanding(min_periods=30, axis=0), func)() + warning_msg = ( + "The 'axis' keyword in DataFrame.expanding is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = getattr(ser.expanding(min_periods=30, axis=0), func)() assert result[:29].isna().all() tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) # min_periods is working correctly - result = getattr(ser.expanding(min_periods=15, axis=0), func)() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = getattr(ser.expanding(min_periods=15, axis=0), func)() assert isna(result.iloc[13]) assert notna(result.iloc[14]) ser2 = Series(np.random.randn(20)) - result = getattr(ser2.expanding(min_periods=5, axis=0), func)() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = getattr(ser2.expanding(min_periods=5, axis=0), func)() assert isna(result[3]) assert notna(result[4]) # min_periods=0 - result0 = getattr(ser.expanding(min_periods=0, axis=0), func)() - result1 = getattr(ser.expanding(min_periods=1, axis=0), func)() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result0 = getattr(ser.expanding(min_periods=0, axis=0), func)() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result1 = getattr(ser.expanding(min_periods=1, axis=0), func)() tm.assert_almost_equal(result0, result1) - result = getattr(ser.expanding(min_periods=1, axis=0), func)() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = getattr(ser.expanding(min_periods=1, axis=0), func)() tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50])) @@ -693,3 +713,17 @@ 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_df_expanding_axis_param_depr(): + df = DataFrame({"a": [1], "b": [2], "c": [3]}) + + warning_msg = ( + "The 'axis' keyword in DataFrame.expanding is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.expanding(2, axis=0) + + warning_msg = "DataFrame.expanding with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.expanding(2, axis=1) \ No newline at end of file diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index e8533b3ca2619..9b5c494b91c98 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -531,11 +531,17 @@ def test_rolling_axis_sum(axis_frame): if axis == 0: expected = DataFrame({i: [np.nan] * 2 + [3.0] * 8 for i in range(20)}) + warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) else: # axis == 1 expected = DataFrame([[np.nan] * 2 + [3.0] * 18] * 10) + warning_msg = "DataFrame.rolling with axis=1 is deprecated." - result = df.rolling(3, axis=axis_frame).sum() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(3, axis=axis_frame).sum() tm.assert_frame_equal(result, expected) @@ -547,10 +553,16 @@ def test_rolling_axis_count(axis_frame): if axis in [0, "index"]: expected = DataFrame({"x": [1.0, 2.0, 2.0], "y": [1.0, 2.0, 2.0]}) + warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) else: expected = DataFrame({"x": [1.0, 1.0, 1.0], "y": [2.0, 2.0, 2.0]}) + warning_msg = "DataFrame.rolling with axis=1 is deprecated." - result = df.rolling(2, axis=axis_frame, min_periods=0).count() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(2, axis=axis_frame, min_periods=0).count() tm.assert_frame_equal(result, expected) @@ -570,9 +582,16 @@ def test_rolling_datetime(axis_frame, tz_naive_fixture): {i: [1] * 2 for i in date_range("2019-8-01", "2019-08-03", freq="D", tz=tz)} ) if axis_frame in [0, "index"]: - result = df.T.rolling("2D", axis=axis_frame).sum().T + warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.T.rolling("2D", axis=axis_frame).sum().T else: - result = df.rolling("2D", axis=axis_frame).sum() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling("2D", axis=axis_frame).sum() expected = DataFrame( { **{ @@ -1076,7 +1095,9 @@ def test_rolling_mixed_dtypes_axis_1(func, value): # GH: 20649 df = DataFrame(1, index=[1, 2], columns=["a", "b", "c"]) df["c"] = 1.0 - result = getattr(df.rolling(window=2, min_periods=1, axis=1), func)() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = getattr(df.rolling(window=2, min_periods=1, axis=1), func)() expected = DataFrame( {"a": [1.0, 1.0], "b": [value, value], "c": [value, value]}, index=[1, 2], @@ -1093,7 +1114,9 @@ def test_rolling_axis_one_with_nan(): [0, 2, 2, np.nan, 2, np.nan, 1], ] ) - result = df.rolling(window=7, min_periods=1, axis="columns").sum() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(window=7, min_periods=1, axis="columns").sum() expected = DataFrame( [ [0.0, 1.0, 3.0, 7.0, 7.0, 7.0, 7.0], @@ -1112,7 +1135,9 @@ def test_rolling_axis_1_non_numeric_dtypes(value): # GH: 20649 df = DataFrame({"a": [1, 2]}) df["b"] = value - result = df.rolling(window=2, min_periods=1, axis=1).sum() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(window=2, min_periods=1, axis=1).sum() expected = DataFrame({"a": [1.0, 2.0]}) tm.assert_frame_equal(result, expected) @@ -1121,7 +1146,9 @@ def test_rolling_on_df_transposed(): # GH: 32724 df = DataFrame({"A": [1, None], "B": [4, 5], "C": [7, 8]}) expected = DataFrame({"A": [1.0, np.nan], "B": [5.0, 5.0], "C": [11.0, 13.0]}) - result = df.rolling(min_periods=1, window=2, axis=1).sum() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(min_periods=1, window=2, axis=1).sum() tm.assert_frame_equal(result, expected) result = df.T.rolling(min_periods=1, window=2).sum().T @@ -1583,7 +1610,9 @@ def test_rolling_float_dtype(float_numpy_dtype): {"A": [np.nan] * 5, "B": range(10, 20, 2)}, dtype=float_numpy_dtype, ) - result = df.rolling(2, axis=1).sum() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(2, axis=1).sum() tm.assert_frame_equal(result, expected, check_dtype=False) @@ -1603,7 +1632,9 @@ def test_rolling_numeric_dtypes(): "j": "uint64", } ) - result = df.rolling(window=2, min_periods=1, axis=1).min() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(window=2, min_periods=1, axis=1).min() expected = DataFrame( { "a": range(0, 40, 10), @@ -1933,3 +1964,18 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): op2 = getattr(rolling2, kernel) expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) + + +def test_df_rolling_axis_param_depr(): + df = DataFrame({"a": [1], "b": [2], "c": [3]}) + + warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.rolling(2, axis=0) + + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.rolling(2, axis=1) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index 0de4b863183df..e828308228e10 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -683,5 +683,14 @@ def test_nat_axis_error(msg, axis): idx = [Timestamp("2020"), NaT] kwargs = {"columns" if axis == 1 else "index": idx} df = DataFrame(np.eye(2), **kwargs) + if axis == 0: + warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) + else: + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + with pytest.raises(ValueError, match=f"{msg} values must not have NaT"): - df.rolling("D", axis=axis).mean() + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + df.rolling("D", axis=axis).mean() diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index a0d052257ec6b..7d449557dd63b 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -679,7 +679,10 @@ def test_rolling_center_axis_1(): {"a": [1, 1, 0, 0, 0, 1], "b": [1, 0, 0, 1, 0, 0], "c": [1, 0, 0, 1, 0, 1]} ) - result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() + warning_msg = "DataFrame.rolling with axis=1 is deprecated." + + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum() expected = DataFrame( {"a": [np.nan] * 6, "b": [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], "c": [np.nan] * 6} From 17d5026d3b2f06481357ce8061b9bbf2dc7f7623 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Fri, 17 Mar 2023 02:38:34 +0530 Subject: [PATCH 11/16] deprication of axis param in rolling functions --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/tests/window/test_api.py | 2 +- pandas/tests/window/test_apply.py | 2 +- pandas/tests/window/test_expanding.py | 5 +++-- pandas/tests/window/test_timeseries_window.py | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index f009eb5cd71de..6528c12d1e53e 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -102,8 +102,8 @@ Deprecations - Deprecated ``axis=1`` in :meth:`DataFrame.groupby` and in :class:`Grouper` constructor, do ``frame.T.groupby(...)`` instead (:issue:`51203`) - Deprecated passing a :class:`DataFrame` to :meth:`DataFrame.from_records`, use :meth:`DataFrame.set_index` or :meth:`DataFrame.drop` instead (:issue:`51353`) - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) -- 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 ``axis=1`` in :meth:`DataFrame.resample`, :meth:`DataFrame.rolling`, :meth:`DataFrame.ewm` and :meth:`DataFrame.expanding`, do ``frame.T.method(...)`` instead (:issue:`51778`) +- 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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index e35a92b551d1f..ad4e86a22affe 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -347,7 +347,7 @@ def test_dont_modify_attributes_after_methods( def test_centered_axis_validation(step): # ok - axis_0_warning_msg = ( + axis_0_warning_msg = ( "The 'axis' keyword in DataFrame.rolling is deprecated and " "will be removed in a future version." ) diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py index 82455a6c8c036..14f2ac53a5658 100644 --- a/pandas/tests/window/test_apply.py +++ b/pandas/tests/window/test_apply.py @@ -322,7 +322,7 @@ def test_axis1(raw): # GH 45912 df = DataFrame([1, 2]) warning_msg = "DataFrame.rolling with axis=1 is deprecated." - + with tm.assert_produces_warning(FutureWarning, match=warning_msg): result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw) expected = DataFrame([1.0, 2.0]) diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py index 48f7823e174e3..81d9512aed06e 100644 --- a/pandas/tests/window/test_expanding.py +++ b/pandas/tests/window/test_expanding.py @@ -82,7 +82,7 @@ def test_missing_minp_zero(): def test_expanding_axis(axis_frame): # see gh-23372. df = DataFrame(np.ones((10, 20))) - axis = df._get_axis_number(axis_frame) + axis = df._get_axis_number(axis_frame) if axis == 0: expected = DataFrame( {i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20)} @@ -714,6 +714,7 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) + def test_df_expanding_axis_param_depr(): df = DataFrame({"a": [1], "b": [2], "c": [3]}) @@ -726,4 +727,4 @@ def test_df_expanding_axis_param_depr(): warning_msg = "DataFrame.expanding with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.expanding(2, axis=1) \ No newline at end of file + df.expanding(2, axis=1) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index e828308228e10..af1701b01b03a 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -684,7 +684,7 @@ def test_nat_axis_error(msg, axis): kwargs = {"columns" if axis == 1 else "index": idx} df = DataFrame(np.eye(2), **kwargs) if axis == 0: - warning_msg = ( + warning_msg = ( "The 'axis' keyword in DataFrame.rolling is deprecated and " "will be removed in a future version." ) From 220aff7b8285b1d39bc6f7924897443e08cec454 Mon Sep 17 00:00:00 2001 From: Daniel Isaac Date: Wed, 22 Mar 2023 20:21:30 +0530 Subject: [PATCH 12/16] deprication of axis param in rolling functions - addressing review comments --- pandas/core/generic.py | 18 +++++++++--------- pandas/tests/window/test_api.py | 16 ++++++++++------ pandas/tests/window/test_ewm.py | 11 +++++++++++ pandas/tests/window/test_expanding.py | 15 +++++++++++++-- pandas/tests/window/test_rolling.py | 11 +++++++++++ 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 922dca03691f0..90577398397bd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -11983,15 +11983,15 @@ def rolling( axis = self._get_axis_number(axis) if axis == 1: warnings.warn( - "DataFrame.rolling with axis=1 is deprecated. Do " + f"{type(self).__name__}.rolling with axis=1 is deprecated. Do " "`frame.T.rolling(...)` without axis instead.", FutureWarning, stacklevel=find_stack_level(), ) else: warnings.warn( - "The 'axis' keyword in DataFrame.rolling is deprecated and " - "will be removed in a future version.", + f"The 'axis' keyword in {type(self).__name__}.rolling " + "is deprecated and will be removed in a future version.", FutureWarning, stacklevel=find_stack_level(), ) @@ -12037,15 +12037,15 @@ def expanding( axis = self._get_axis_number(axis) if axis == 1: warnings.warn( - "DataFrame.expanding with axis=1 is deprecated. Do " + f"{type(self).__name__}.expanding with axis=1 is deprecated. Do " "`frame.T.expanding(...)` without axis instead.", FutureWarning, stacklevel=find_stack_level(), ) else: warnings.warn( - "The 'axis' keyword in DataFrame.expanding is deprecated and " - "will be removed in a future version.", + f"The 'axis' keyword in {type(self).__name__}.expanding " + "is deprecated and will be removed in a future version.", FutureWarning, stacklevel=find_stack_level(), ) @@ -12073,15 +12073,15 @@ def ewm( axis = self._get_axis_number(axis) if axis == 1: warnings.warn( - "DataFrame.ewm with axis=1 is deprecated. Do " + f"{type(self).__name__}.ewm with axis=1 is deprecated. Do " "`frame.T.ewm(...)` without axis instead.", FutureWarning, stacklevel=find_stack_level(), ) else: warnings.warn( - "The 'axis' keyword in DataFrame.ewm is deprecated and " - "will be removed in a future version.", + f"The 'axis' keyword in {type(self).__name__}.ewm " + "is deprecated and will be removed in a future version.", FutureWarning, stacklevel=find_stack_level(), ) diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index ad4e86a22affe..bee6238fa84f0 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -347,13 +347,12 @@ def test_dont_modify_attributes_after_methods( def test_centered_axis_validation(step): # ok - axis_0_warning_msg = ( - "The 'axis' keyword in DataFrame.rolling is deprecated and " + series_axis_0_warning_msg = ( + "The 'axis' keyword in Series.rolling is deprecated and " "will be removed in a future version." ) - axis_1_warning_msg = "DataFrame.rolling with axis=1 is deprecated." - with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg): + with tm.assert_produces_warning(FutureWarning, match=series_axis_0_warning_msg): Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean() # bad axis @@ -361,12 +360,17 @@ def test_centered_axis_validation(step): with pytest.raises(ValueError, match=msg): Series(np.ones(10)).rolling(window=3, center=True, axis=1, step=step).mean() + df_axis_0_warning_msg = ( + "The 'axis' keyword in DataFrame.rolling is deprecated and " + "will be removed in a future version." + ) + df_axis_1_warning_msg = "DataFrame.rolling with axis=1 is deprecated." # ok ok - with tm.assert_produces_warning(FutureWarning, match=axis_0_warning_msg): + with tm.assert_produces_warning(FutureWarning, match=df_axis_0_warning_msg): DataFrame(np.ones((10, 10))).rolling( window=3, center=True, axis=0, step=step ).mean() - with tm.assert_produces_warning(FutureWarning, match=axis_1_warning_msg): + with tm.assert_produces_warning(FutureWarning, match=df_axis_1_warning_msg): DataFrame(np.ones((10, 10))).rolling( window=3, center=True, axis=1, step=step ).mean() diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index 1ed5aa85edcb3..25f614dbba3ed 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -736,3 +736,14 @@ def test_df_ewn_axis_param_depr(): warning_msg = "DataFrame.ewm with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=warning_msg): df.ewm(span=2, axis=1) + + +def test_series_ewn_axis_param_depr(): + series = Series([1, 2, 3]) + + warning_msg = ( + "The 'axis' keyword in Series.ewm is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + series.ewm(span=2, axis=0) diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py index 81d9512aed06e..63ab3f074566f 100644 --- a/pandas/tests/window/test_expanding.py +++ b/pandas/tests/window/test_expanding.py @@ -329,7 +329,7 @@ def test_expanding_corr_pairwise(frame): def test_expanding_func(func, static_comp, frame_or_series): data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10)) warning_msg = ( - "The 'axis' keyword in DataFrame.expanding is deprecated and " + f"The 'axis' keyword in {type(data).__name__}.expanding is deprecated and " "will be removed in a future version." ) with tm.assert_produces_warning(FutureWarning, match=warning_msg): @@ -352,7 +352,7 @@ def test_expanding_min_periods(func, static_comp): ser = Series(np.random.randn(50)) warning_msg = ( - "The 'axis' keyword in DataFrame.expanding is deprecated and " + f"The 'axis' keyword in {type(ser).__name__}.expanding is deprecated and " "will be removed in a future version." ) with tm.assert_produces_warning(FutureWarning, match=warning_msg): @@ -728,3 +728,14 @@ def test_df_expanding_axis_param_depr(): warning_msg = "DataFrame.expanding with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=warning_msg): df.expanding(2, axis=1) + + +def test_series_expanding_axis_param_depr(): + series = Series([1, 2, 3]) + + warning_msg = ( + "The 'axis' keyword in Series.expanding is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + series.expanding(2, axis=0) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 9b5c494b91c98..a988f67cf8d0b 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1979,3 +1979,14 @@ def test_df_rolling_axis_param_depr(): warning_msg = "DataFrame.rolling with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=warning_msg): df.rolling(2, axis=1) + + +def test_series_rolling_axis_param_depr(): + series = Series([1, 2, 3]) + + warning_msg = ( + "The 'axis' keyword in Series.rolling is deprecated and " + "will be removed in a future version." + ) + with tm.assert_produces_warning(FutureWarning, match=warning_msg): + series.rolling(2, axis=0) From fcba2fb379a3192ce6457a8514a49304eca620f7 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Thu, 23 Mar 2023 23:42:09 +0530 Subject: [PATCH 13/16] revert changes --- pandas/tests/window/test_api.py | 5 ----- pandas/tests/window/test_ewm.py | 26 ---------------------- pandas/tests/window/test_expanding.py | 26 ---------------------- pandas/tests/window/test_rolling.py | 31 --------------------------- 4 files changed, 88 deletions(-) diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index f492fc46a10b8..d6cca5061671b 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -355,11 +355,6 @@ def test_centered_axis_validation(step): with pytest.raises(ValueError, match=msg): Series(np.ones(10)).rolling(window=3, center=True, axis=1, step=step).mean() - df_axis_0_warning_msg = ( - "The 'axis' keyword in DataFrame.rolling is deprecated and " - "will be removed in a future version." - ) - df_axis_1_warning_msg = "DataFrame.rolling with axis=1 is deprecated." # ok ok df = DataFrame(np.ones((10, 10))) msg = "The 'axis' keyword in DataFrame.rolling is deprecated" diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index b1428e6554c0b..f4643be53f120 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -721,29 +721,3 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): op2 = getattr(ewm2, kernel) expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) - - -def test_df_ewn_axis_param_depr(): - df = DataFrame({"a": [1], "b": 2, "c": 3}) - - warning_msg = ( - "The 'axis' keyword in DataFrame.ewm is deprecated and " - "will be removed in a future version." - ) - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.ewm(span=2, axis=0) - - warning_msg = "DataFrame.ewm with axis=1 is deprecated." - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.ewm(span=2, axis=1) - - -def test_series_ewn_axis_param_depr(): - series = Series([1, 2, 3]) - - warning_msg = ( - "The 'axis' keyword in Series.ewm is deprecated and " - "will be removed in a future version." - ) - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - series.ewm(span=2, axis=0) diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py index 7472efedc68ec..61eb3686aad83 100644 --- a/pandas/tests/window/test_expanding.py +++ b/pandas/tests/window/test_expanding.py @@ -706,29 +706,3 @@ 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_df_expanding_axis_param_depr(): - df = DataFrame({"a": [1], "b": [2], "c": [3]}) - - warning_msg = ( - "The 'axis' keyword in DataFrame.expanding is deprecated and " - "will be removed in a future version." - ) - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.expanding(2, axis=0) - - warning_msg = "DataFrame.expanding with axis=1 is deprecated." - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.expanding(2, axis=1) - - -def test_series_expanding_axis_param_depr(): - series = Series([1, 2, 3]) - - warning_msg = ( - "The 'axis' keyword in Series.expanding is deprecated and " - "will be removed in a future version." - ) - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - series.expanding(2, axis=0) diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index bed1d1dc5412b..4a35ff0162194 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -532,15 +532,10 @@ def test_rolling_axis_sum(axis_frame): if axis == 0: msg = "The 'axis' keyword in DataFrame.rolling" expected = DataFrame({i: [np.nan] * 2 + [3.0] * 8 for i in range(20)}) - warning_msg = ( - "The 'axis' keyword in DataFrame.rolling is deprecated and " - "will be removed in a future version." - ) else: # axis == 1 msg = "Support for axis=1 in DataFrame.rolling is deprecated" expected = DataFrame([[np.nan] * 2 + [3.0] * 18] * 10) - warning_msg = "DataFrame.rolling with axis=1 is deprecated." with tm.assert_produces_warning(FutureWarning, match=msg): result = df.rolling(3, axis=axis_frame).sum() @@ -1962,29 +1957,3 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): op2 = getattr(rolling2, kernel) expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) - - -def test_df_rolling_axis_param_depr(): - df = DataFrame({"a": [1], "b": [2], "c": [3]}) - - warning_msg = ( - "The 'axis' keyword in DataFrame.rolling is deprecated and " - "will be removed in a future version." - ) - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.rolling(2, axis=0) - - warning_msg = "DataFrame.rolling with axis=1 is deprecated." - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - df.rolling(2, axis=1) - - -def test_series_rolling_axis_param_depr(): - series = Series([1, 2, 3]) - - warning_msg = ( - "The 'axis' keyword in Series.rolling is deprecated and " - "will be removed in a future version." - ) - with tm.assert_produces_warning(FutureWarning, match=warning_msg): - series.rolling(2, axis=0) From ef4eeb0b292df56e5f0e280b03be65c093e2b814 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Thu, 23 Mar 2023 23:43:01 +0530 Subject: [PATCH 14/16] revert changes --- pandas/tests/window/test_expanding.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py index 61eb3686aad83..b4c5edeae949b 100644 --- a/pandas/tests/window/test_expanding.py +++ b/pandas/tests/window/test_expanding.py @@ -83,6 +83,7 @@ def test_expanding_axis(axis_frame): # see gh-23372. df = DataFrame(np.ones((10, 20))) axis = df._get_axis_number(axis_frame) + if axis == 0: msg = "The 'axis' keyword in DataFrame.expanding is deprecated" expected = DataFrame( From 373efd9cd56a50787fcdeb7ac9547b6b4b42d683 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Thu, 23 Mar 2023 23:50:40 +0530 Subject: [PATCH 15/16] revert changes --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index db900ddd1f85b..0cb1641e40e05 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -107,6 +107,7 @@ Deprecations - Deprecated accepting slices in :meth:`DataFrame.take`, call ``obj[slicer]`` or pass a sequence of integers instead (:issue:`51539`) - Deprecated ``axis=1`` in :meth:`DataFrame.ewm`, :meth:`DataFrame.rolling`, :meth:`DataFrame.expanding`, transpose before calling the method instead (:issue:`51778`) - Deprecated the ``axis`` keyword in :meth:`DataFrame.ewm`, :meth:`Series.ewm`, :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.expanding`, :meth:`Series.expanding` (:issue:`51778`) +- Deprecated the ``axis`` keyword in :meth:`DataFrame.resample`, :meth:`Series.resample` (:issue:`51778`) - 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 the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`) - From 28af613346e3e756ef18a8ac41a428503de5a8ae Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Fri, 24 Mar 2023 01:08:53 +0530 Subject: [PATCH 16/16] revert changes --- pandas/core/generic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 6e57c9222d494..aa04c601a81cd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8582,6 +8582,7 @@ def resample( Which axis to use for up- or down-sampling. For `Series` this parameter is unused and defaults to 0. Must be `DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`. + .. deprecated:: 2.0.0 Use frame.T.resample(...) instead. closed : {{'right', 'left'}}, default None