From f0fdef28552c92a6c67095ec9656875bf1c93d66 Mon Sep 17 00:00:00 2001 From: Anton Shevtsov Date: Tue, 21 Jun 2022 20:22:57 +0300 Subject: [PATCH 1/3] Deprecate non-keyword arguments for rsplit --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/strings/accessor.py | 3 ++- pandas/tests/strings/test_split_partition.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 76f6e864a174f..9b0e0c883221a 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -705,6 +705,7 @@ Other Deprecations - Deprecated the ``closed`` argument in :meth:`interval_range` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) - Deprecated the methods :meth:`DataFrame.mad`, :meth:`Series.mad`, and the corresponding groupby methods (:issue:`11787`) - Deprecated positional arguments to :meth:`Index.join` except for ``other``, use keyword-only arguments instead of positional arguments (:issue:`46518`) +- Deprecated positional arguments to :meth:`StringMethods.rsplit` except for ``pat``, use keyword-only arguments instead of positional arguments (:issue:`47423`) - Deprecated indexing on a timezone-naive :class:`DatetimeIndex` using a string representing a timezone-aware datetime (:issue:`46903`, :issue:`36148`) - Deprecated the ``closed`` argument in :class:`Interval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) - Deprecated the ``closed`` argument in :class:`IntervalIndex` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index abd380299ba02..e27537c7d14c6 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -18,7 +18,7 @@ DtypeObj, F, ) -from pandas.util._decorators import Appender +from pandas.util._decorators import Appender, deprecate_nonkeyword_arguments from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( @@ -874,6 +874,7 @@ def split( "regex_examples": "", } ) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "pat"]) @forbid_nonstring_types(["bytes"]) def rsplit(self, pat=None, n=-1, expand=False): result = self._data.array._str_rsplit(pat, n=n) diff --git a/pandas/tests/strings/test_split_partition.py b/pandas/tests/strings/test_split_partition.py index 74458c13e8df7..e4dd314afb127 100644 --- a/pandas/tests/strings/test_split_partition.py +++ b/pandas/tests/strings/test_split_partition.py @@ -130,6 +130,22 @@ def test_rsplit_max_number(any_string_dtype): tm.assert_series_equal(result, exp) +def test_rsplit_posargs_deprecation(): + # GH 47423; Deprecate passing n as positional. + s = Series(["foo,bar,lorep"]) + + msg = ( + r"In a future version of pandas all arguments of StringMethods.rsplit " + r"except for the argument 'pat' will be keyword-only" + ) + + with tm.assert_produces_warning(FutureWarning, match=msg): + result = s.str.rsplit(",", 3) + + expected = Series([["foo", "bar", "lorep"]]) + tm.assert_series_equal(result, expected) + + def test_split_blank_string(any_string_dtype): # expand blank split GH 20067 values = Series([""], name="test", dtype=any_string_dtype) From 743891546c9f56613eb0795d1985423b38a89e3a Mon Sep 17 00:00:00 2001 From: Anton Shevtsov Date: Tue, 21 Jun 2022 20:38:54 +0300 Subject: [PATCH 2/3] linter fix --- pandas/core/strings/accessor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index e27537c7d14c6..3bcfa61b74c64 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -18,7 +18,10 @@ DtypeObj, F, ) -from pandas.util._decorators import Appender, deprecate_nonkeyword_arguments +from pandas.util._decorators import ( + Appender, + deprecate_nonkeyword_arguments, +) from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( From 78db35870fa56de9ce56423c62823c651382cc54 Mon Sep 17 00:00:00 2001 From: Anton Shevtsov Date: Wed, 22 Jun 2022 14:11:48 +0300 Subject: [PATCH 3/3] Deprecate non-keyword arguments for split --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/core/strings/accessor.py | 1 + pandas/tests/strings/test_split_partition.py | 9 +++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 630e6eb38507e..017b979ecbeed 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -705,7 +705,7 @@ Other Deprecations - Deprecated the ``closed`` argument in :meth:`interval_range` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) - Deprecated the methods :meth:`DataFrame.mad`, :meth:`Series.mad`, and the corresponding groupby methods (:issue:`11787`) - Deprecated positional arguments to :meth:`Index.join` except for ``other``, use keyword-only arguments instead of positional arguments (:issue:`46518`) -- Deprecated positional arguments to :meth:`StringMethods.rsplit` except for ``pat``, use keyword-only arguments instead of positional arguments (:issue:`47423`) +- Deprecated positional arguments to :meth:`StringMethods.rsplit` and :meth:`StringMethods.split` except for ``pat``, use keyword-only arguments instead of positional arguments (:issue:`47423`) - Deprecated indexing on a timezone-naive :class:`DatetimeIndex` using a string representing a timezone-aware datetime (:issue:`46903`, :issue:`36148`) - Deprecated the ``closed`` argument in :class:`Interval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) - Deprecated the ``closed`` argument in :class:`IntervalIndex` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 3bcfa61b74c64..ae92926195273 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -846,6 +846,7 @@ def cat( """, } ) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "pat"]) @forbid_nonstring_types(["bytes"]) def split( self, diff --git a/pandas/tests/strings/test_split_partition.py b/pandas/tests/strings/test_split_partition.py index e4dd314afb127..7d73414a672c8 100644 --- a/pandas/tests/strings/test_split_partition.py +++ b/pandas/tests/strings/test_split_partition.py @@ -130,17 +130,18 @@ def test_rsplit_max_number(any_string_dtype): tm.assert_series_equal(result, exp) -def test_rsplit_posargs_deprecation(): +@pytest.mark.parametrize("method", ["split", "rsplit"]) +def test_posargs_deprecation(method): # GH 47423; Deprecate passing n as positional. s = Series(["foo,bar,lorep"]) msg = ( - r"In a future version of pandas all arguments of StringMethods.rsplit " - r"except for the argument 'pat' will be keyword-only" + f"In a future version of pandas all arguments of StringMethods.{method} " + "except for the argument 'pat' will be keyword-only" ) with tm.assert_produces_warning(FutureWarning, match=msg): - result = s.str.rsplit(",", 3) + result = getattr(s.str, method)(",", 3) expected = Series([["foo", "bar", "lorep"]]) tm.assert_series_equal(result, expected)