From caa1f3436b0869c0fcb40848c780ac9252b7c0c8 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 16 May 2021 14:37:00 +0100 Subject: [PATCH 1/4] deprecate nonkeywordargs in ffill --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/core/generic.py | 2 ++ pandas/tests/frame/methods/test_fillna.py | 10 ++++++++++ pandas/tests/series/methods/test_fillna.py | 10 ++++++++++ 4 files changed, 23 insertions(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 622029adf357f..201addec2d758 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -647,6 +647,7 @@ Deprecations - Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`) - Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`) - Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`) +- Deprecated passing arguments as positional in :meth:`DataFrame.ffill` and :meth:`Series.ffill` (:issue:`41485`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a09cc0a6324c0..1072516bd5991 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -61,6 +61,7 @@ InvalidIndexError, ) from pandas.util._decorators import ( + deprecate_nonkeyword_arguments, doc, rewrite_axis_style_signature, ) @@ -6427,6 +6428,7 @@ def ffill( ) -> FrameOrSeries | None: ... + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) @final @doc(klass=_shared_doc_kwargs["klass"]) def ffill( diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index b827547b0753e..8f1ca49e03874 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -326,6 +326,16 @@ def test_ffill(self, datetime_frame): datetime_frame.ffill(), datetime_frame.fillna(method="ffill") ) + def test_ffill_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"Starting with Pandas version 2\.0 all arguments of ffill except " + r"for the argument 'self' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.ffill(0) + def test_bfill(self, datetime_frame): datetime_frame["A"][:5] = np.nan datetime_frame["A"][-5:] = np.nan diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 51864df915f8c..8545254a59b8e 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -765,6 +765,16 @@ def test_ffill(self): ts[2] = np.NaN tm.assert_series_equal(ts.ffill(), ts.fillna(method="ffill")) + def test_ffill_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + ser = Series([1, 2, 3]) + msg = ( + r"Starting with Pandas version 2\.0 all arguments of ffill except " + r"for the argument 'self' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + ser.ffill(0) + def test_ffill_mixed_dtypes_without_missing_data(self): # GH#14956 series = Series([datetime(2015, 1, 1, tzinfo=pytz.utc), 1]) From b2f6f50c8422d8297c3aa64a65ae61337b029f0e Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 16 May 2021 14:41:07 +0100 Subject: [PATCH 2/4] deprecate nonkeyword args for bfill --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/core/generic.py | 1 + pandas/tests/frame/methods/test_fillna.py | 10 ++++++++++ pandas/tests/series/methods/test_fillna.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 201addec2d758..d90bf96bf0c4c 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -647,7 +647,7 @@ Deprecations - Deprecated setting :attr:`Categorical._codes`, create a new :class:`Categorical` with the desired codes instead (:issue:`40606`) - Deprecated behavior of :meth:`DatetimeIndex.union` with mixed timezones; in a future version both will be cast to UTC instead of object dtype (:issue:`39328`) - Deprecated using ``usecols`` with out of bounds indices for ``read_csv`` with ``engine="c"`` (:issue:`25623`) -- Deprecated passing arguments as positional in :meth:`DataFrame.ffill` and :meth:`Series.ffill` (:issue:`41485`) +- Deprecated passing arguments as positional in :meth:`DataFrame.ffill`, :meth:`Series.ffill`, :meth:`DataFrame.bfill`, and :meth:`Series.bfill` (:issue:`41485`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1072516bd5991..afe425da86c57 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6492,6 +6492,7 @@ def bfill( ) -> FrameOrSeries | None: ... + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) @final @doc(klass=_shared_doc_kwargs["klass"]) def bfill( diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 8f1ca49e03874..935311821fbcc 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -344,6 +344,16 @@ def test_bfill(self, datetime_frame): datetime_frame.bfill(), datetime_frame.fillna(method="bfill") ) + def test_bfill_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"Starting with Pandas version 2\.0 all arguments of bfill except " + r"for the argument 'self' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.bfill(0) + def test_frame_pad_backfill_limit(self): index = np.arange(10) df = DataFrame(np.random.randn(10, 4), index=index) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 8545254a59b8e..a4bf459e4e3e5 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -786,6 +786,16 @@ def test_bfill(self): ts[2] = np.NaN tm.assert_series_equal(ts.bfill(), ts.fillna(method="bfill")) + def test_bfill_pos_args_deprecation(self): + # https://github.com/pandas-dev/pandas/issues/41485 + ser = Series([1, 2, 3]) + msg = ( + r"Starting with Pandas version 2\.0 all arguments of bfill except " + r"for the argument 'self' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + ser.bfill(0) + def test_pad_nan(self): x = Series( [np.nan, 1.0, np.nan, 3.0, np.nan], ["z", "a", "b", "c", "d"], dtype=float From 6bc7c2c586731da41adc5cb3b52aaec11b4bddc5 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Tue, 18 May 2021 21:46:40 +0100 Subject: [PATCH 3/4] wip --- pandas/core/frame.py | 21 +++++++++++++++++++++ pandas/core/generic.py | 5 ----- pandas/core/series.py | 11 +++++++++++ pandas/tests/frame/methods/test_fillna.py | 8 ++++---- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2941b6ac01904..bbfc96ada72fc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -77,6 +77,7 @@ Appender, Substitution, deprecate_kwarg, + deprecate_nonkeyword_arguments, doc, rewrite_axis_style_signature, ) @@ -10586,6 +10587,26 @@ def _values(self) -> np.ndarray: """internal implementation""" return self.values + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) + def ffill( + self: DataFrame, + axis: None | Axis = None, + inplace: bool = False, + limit: None | int = None, + downcast=None, + ) -> DataFrame | None: + super().ffill(axis, inplace, limit, downcast) + + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) + def bfill( + self: DataFrame, + axis: None | Axis = None, + inplace: bool = False, + limit: None | int = None, + downcast=None, + ) -> DataFrame: + super().bfill(axis, inplace, limit, downcast) + DataFrame._add_numeric_operations() diff --git a/pandas/core/generic.py b/pandas/core/generic.py index afe425da86c57..290ee8101d64c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -61,7 +61,6 @@ InvalidIndexError, ) from pandas.util._decorators import ( - deprecate_nonkeyword_arguments, doc, rewrite_axis_style_signature, ) @@ -6428,8 +6427,6 @@ def ffill( ) -> FrameOrSeries | None: ... - @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) - @final @doc(klass=_shared_doc_kwargs["klass"]) def ffill( self: FrameOrSeries, @@ -6492,8 +6489,6 @@ def bfill( ) -> FrameOrSeries | None: ... - @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) - @final @doc(klass=_shared_doc_kwargs["klass"]) def bfill( self: FrameOrSeries, diff --git a/pandas/core/series.py b/pandas/core/series.py index c8e9898f9462a..46e419cfab380 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -51,6 +51,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_nonkeyword_arguments, doc, ) from pandas.util._validators import ( @@ -5256,6 +5257,16 @@ def to_period(self, freq=None, copy=True) -> Series: self, method="to_period" ) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) + def ffill( + self: Series, + axis: None | Axis = None, + inplace: bool = False, + limit: None | int = None, + downcast=None, + ) -> Series | None: + return super().ffill(axis, inplace, limit, downcast) + # ---------------------------------------------------------------------- # Add index _AXIS_ORDERS = ["index"] diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index 935311821fbcc..fc32a8fea5177 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -330,8 +330,8 @@ def test_ffill_pos_args_deprecation(self): # https://github.com/pandas-dev/pandas/issues/41485 df = DataFrame({"a": [1, 2, 3]}) msg = ( - r"Starting with Pandas version 2\.0 all arguments of ffill except " - r"for the argument 'self' will be keyword-only" + r"In a future version of pandas all arguments of DataFrame.ffill " + r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): df.ffill(0) @@ -348,8 +348,8 @@ def test_bfill_pos_args_deprecation(self): # https://github.com/pandas-dev/pandas/issues/41485 df = DataFrame({"a": [1, 2, 3]}) msg = ( - r"Starting with Pandas version 2\.0 all arguments of bfill except " - r"for the argument 'self' will be keyword-only" + r"In a future version of pandas all arguments of DataFrame.bfill " + r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): df.bfill(0) From 026c1cc04ec47400d2365ae821875775f0adbb72 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Tue, 18 May 2021 21:54:17 +0100 Subject: [PATCH 4/4] fixup after merge --- pandas/core/frame.py | 4 ++-- pandas/core/series.py | 10 ++++++++++ pandas/tests/frame/methods/test_fillna.py | 8 ++++++-- pandas/tests/series/methods/test_fillna.py | 16 ++++++++++------ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 81ac40803f2a6..f1eae15b606d0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10635,7 +10635,7 @@ def ffill( limit: None | int = None, downcast=None, ) -> DataFrame | None: - super().ffill(axis, inplace, limit, downcast) + return super().ffill(axis, inplace, limit, downcast) @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) def bfill( @@ -10645,7 +10645,7 @@ def bfill( limit: None | int = None, downcast=None, ) -> DataFrame: - super().bfill(axis, inplace, limit, downcast) + return super().bfill(axis, inplace, limit, downcast) DataFrame._add_numeric_operations() diff --git a/pandas/core/series.py b/pandas/core/series.py index bd16539b2de68..c71fae37f79e2 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5267,6 +5267,16 @@ def ffill( ) -> Series | None: return super().ffill(axis, inplace, limit, downcast) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) + def bfill( + self: Series, + axis: None | Axis = None, + inplace: bool = False, + limit: None | int = None, + downcast=None, + ) -> Series: + return super().bfill(axis, inplace, limit, downcast) + # ---------------------------------------------------------------------- # Add index _AXIS_ORDERS = ["index"] diff --git a/pandas/tests/frame/methods/test_fillna.py b/pandas/tests/frame/methods/test_fillna.py index fc32a8fea5177..71640ccd87be2 100644 --- a/pandas/tests/frame/methods/test_fillna.py +++ b/pandas/tests/frame/methods/test_fillna.py @@ -334,7 +334,9 @@ def test_ffill_pos_args_deprecation(self): r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): - df.ffill(0) + result = df.ffill(0) + expected = DataFrame({"a": [1, 2, 3]}) + tm.assert_frame_equal(result, expected) def test_bfill(self, datetime_frame): datetime_frame["A"][:5] = np.nan @@ -352,7 +354,9 @@ def test_bfill_pos_args_deprecation(self): r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): - df.bfill(0) + result = df.bfill(0) + expected = DataFrame({"a": [1, 2, 3]}) + tm.assert_frame_equal(result, expected) def test_frame_pad_backfill_limit(self): index = np.arange(10) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index a4bf459e4e3e5..a8e71618c14ea 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -769,11 +769,13 @@ def test_ffill_pos_args_deprecation(self): # https://github.com/pandas-dev/pandas/issues/41485 ser = Series([1, 2, 3]) msg = ( - r"Starting with Pandas version 2\.0 all arguments of ffill except " - r"for the argument 'self' will be keyword-only" + r"In a future version of pandas all arguments of Series.ffill " + r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): - ser.ffill(0) + result = ser.ffill(0) + expected = Series([1, 2, 3]) + tm.assert_series_equal(result, expected) def test_ffill_mixed_dtypes_without_missing_data(self): # GH#14956 @@ -790,11 +792,13 @@ def test_bfill_pos_args_deprecation(self): # https://github.com/pandas-dev/pandas/issues/41485 ser = Series([1, 2, 3]) msg = ( - r"Starting with Pandas version 2\.0 all arguments of bfill except " - r"for the argument 'self' will be keyword-only" + r"In a future version of pandas all arguments of Series.bfill " + r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): - ser.bfill(0) + result = ser.bfill(0) + expected = Series([1, 2, 3]) + tm.assert_series_equal(result, expected) def test_pad_nan(self): x = Series(