diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index a5cb716317689..4c23e5e5b1906 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` 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 abd380299ba02..ae92926195273 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 +from pandas.util._decorators import ( + Appender, + deprecate_nonkeyword_arguments, +) from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( @@ -843,6 +846,7 @@ def cat( """, } ) + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "pat"]) @forbid_nonstring_types(["bytes"]) def split( self, @@ -874,6 +878,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..7d73414a672c8 100644 --- a/pandas/tests/strings/test_split_partition.py +++ b/pandas/tests/strings/test_split_partition.py @@ -130,6 +130,23 @@ def test_rsplit_max_number(any_string_dtype): tm.assert_series_equal(result, exp) +@pytest.mark.parametrize("method", ["split", "rsplit"]) +def test_posargs_deprecation(method): + # GH 47423; Deprecate passing n as positional. + s = Series(["foo,bar,lorep"]) + + msg = ( + 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 = getattr(s.str, method)(",", 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)