From 266081089a11b95035c2d7b2399178a1c99abf95 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 16 May 2021 13:14:13 +0100 Subject: [PATCH 1/2] deprecate passing args as positional in dropna --- doc/source/whatsnew/v1.3.0.rst | 1 + pandas/core/frame.py | 2 ++ pandas/core/series.py | 2 ++ pandas/tests/frame/methods/test_dropna.py | 10 ++++++++++ pandas/tests/series/methods/test_dropna.py | 10 ++++++++++ 5 files changed, 25 insertions(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 622029adf357f..e85e9b46d5676 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.dropna` and :meth:`Series.dropna` (:issue:`41485`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2941b6ac01904..e07d5526c07be 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, ) @@ -5804,6 +5805,7 @@ def notna(self) -> DataFrame: def notnull(self) -> DataFrame: return ~self.isna() + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) def dropna( self, axis: Axis = 0, diff --git a/pandas/core/series.py b/pandas/core/series.py index c8e9898f9462a..38ee00bdf6b23 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 ( @@ -5059,6 +5060,7 @@ def notna(self) -> Series: def notnull(self) -> Series: return super().notnull() + @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) def dropna(self, axis=0, inplace=False, how=None): """ Return a new Series with missing values removed. diff --git a/pandas/tests/frame/methods/test_dropna.py b/pandas/tests/frame/methods/test_dropna.py index b671bb1afb27a..74fa45a5b1376 100644 --- a/pandas/tests/frame/methods/test_dropna.py +++ b/pandas/tests/frame/methods/test_dropna.py @@ -231,3 +231,13 @@ def test_dropna_with_duplicate_columns(self): result = df.dropna(subset=["A", "C"], how="all") tm.assert_frame_equal(result, expected) + + def test_dropna_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 dropna except for the " + r"argument 'self' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.dropna(1) diff --git a/pandas/tests/series/methods/test_dropna.py b/pandas/tests/series/methods/test_dropna.py index 5bff7306fac33..aac9b39defc22 100644 --- a/pandas/tests/series/methods/test_dropna.py +++ b/pandas/tests/series/methods/test_dropna.py @@ -101,3 +101,13 @@ def test_datetime64_tz_dropna(self): ) assert result.dtype == "datetime64[ns, Asia/Tokyo]" tm.assert_series_equal(result, expected) + + def test_dropna_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 dropna except for the " + r"argument 'self' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + ser.dropna(0) From d75abb153bc35b8b7b5dd5902220bbd0463b3a6c Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 23 May 2021 21:26:18 +0100 Subject: [PATCH 2/2] fixup tests --- pandas/core/frame.py | 2 +- pandas/core/series.py | 2 +- pandas/tests/frame/methods/test_dropna.py | 4 ++-- pandas/tests/series/methods/test_dropna.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d62c38a902bba..adc1b8cc51e7e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5856,7 +5856,7 @@ def notna(self) -> DataFrame: def notnull(self) -> DataFrame: return ~self.isna() - @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) def dropna( self, axis: Axis = 0, diff --git a/pandas/core/series.py b/pandas/core/series.py index 168a296f0487c..5e9a7f343d67d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5093,7 +5093,7 @@ def notna(self) -> Series: def notnull(self) -> Series: return super().notnull() - @deprecate_nonkeyword_arguments(version="2.0", allowed_args=["self"]) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) def dropna(self, axis=0, inplace=False, how=None): """ Return a new Series with missing values removed. diff --git a/pandas/tests/frame/methods/test_dropna.py b/pandas/tests/frame/methods/test_dropna.py index c6a2448517048..76a6f3aa25362 100644 --- a/pandas/tests/frame/methods/test_dropna.py +++ b/pandas/tests/frame/methods/test_dropna.py @@ -236,8 +236,8 @@ def test_dropna_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 dropna except for the " - r"argument 'self' will be keyword-only" + r"In a future version of pandas all arguments of DataFrame\.dropna " + r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): result = df.dropna(1) diff --git a/pandas/tests/series/methods/test_dropna.py b/pandas/tests/series/methods/test_dropna.py index 58839c827f329..0dab9271bfee5 100644 --- a/pandas/tests/series/methods/test_dropna.py +++ b/pandas/tests/series/methods/test_dropna.py @@ -106,8 +106,8 @@ def test_dropna_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 dropna except for the " - r"argument 'self' will be keyword-only" + r"In a future version of pandas all arguments of Series\.dropna " + r"will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): result = ser.dropna(0)