Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API/DEPR: replace kwarg "pat" with "sep" in str.[r]partition #23767

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ Deprecations
- :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` have deprecated the ``errors`` argument in favor of the ``nonexistent`` argument (:issue:`8917`)
- The class ``FrozenNDArray`` has been deprecated. When unpickling, ``FrozenNDArray`` will be unpickled to ``np.ndarray`` once this class is removed (:issue:`9031`)
- The methods :meth:`DataFrame.update` and :meth:`Panel.update` have deprecated the ``raise_conflict=False|True`` keyword in favor of ``errors='ignore'|'raise'`` (:issue:`23585`)
- The methods :meth:`Series.str.partition` and :meth:`Series.str.rpartition` have deprecated the ``pat`` keyword in favor of ``sep`` (:issue:`22676`)
- Deprecated the `nthreads` keyword of :func:`pandas.read_feather` in favor of
`use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`)
- :func:`pandas.read_excel` has deprecated accepting ``usecols`` as an integer. Please pass in a list of ints from 0 to ``usecols`` inclusive instead (:issue:`23527`)
Expand Down
17 changes: 11 additions & 6 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pandas.core.algorithms import take_1d
import pandas.compat as compat
from pandas.core.base import NoNewAttributesMixin
from pandas.util._decorators import Appender
from pandas.util._decorators import Appender, deprecate_kwarg
import re
import pandas._libs.lib as lib
import pandas._libs.ops as libops
Expand Down Expand Up @@ -2410,8 +2410,11 @@ def rsplit(self, pat=None, n=-1, expand=False):

Parameters
----------
pat : str, default whitespace
sep : str, default whitespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's still keep pat documented but with a deprecated directive

String to split on.
pat : str, default whitespace
.. deprecated:: 0.24.0
Use ``sep`` instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Do we need a newline after this or does it render OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this directive recently in https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html, and then took the code from:

.. deprecated:: 0.20.0

IOW, should be fine. :)

expand : bool, default True
If True, return DataFrame/MultiIndex expanding dimensionality.
If False, return Series/Index.
Expand Down Expand Up @@ -2485,8 +2488,9 @@ def rsplit(self, pat=None, n=-1, expand=False):
'empty strings',
'also': 'rpartition : Split the string at the last occurrence of `sep`'
})
def partition(self, pat=' ', expand=True):
f = lambda x: x.partition(pat)
@deprecate_kwarg(old_arg_name='pat', new_arg_name='sep')
def partition(self, sep=' ', expand=True):
f = lambda x: x.partition(sep)
result = _na_map(f, self._parent)
return self._wrap_result(result, expand=expand)

Expand All @@ -2496,8 +2500,9 @@ def partition(self, pat=' ', expand=True):
'string itself',
'also': 'partition : Split the string at the first occurrence of `sep`'
})
def rpartition(self, pat=' ', expand=True):
f = lambda x: x.rpartition(pat)
@deprecate_kwarg(old_arg_name='pat', new_arg_name='sep')
def rpartition(self, sep=' ', expand=True):
f = lambda x: x.rpartition(sep)
result = _na_map(f, self._parent)
return self._wrap_result(result, expand=expand)

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,24 @@ def test_partition_with_name(self):
assert res.nlevels == 1
tm.assert_index_equal(res, exp)

def test_partition_deprecation(self):
# GH 22676; depr kwarg "pat" in favor of "sep"
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])

# str.partition
# using sep -> no warning
expected = values.str.partition(sep='_')
with tm.assert_produces_warning(FutureWarning):
result = values.str.partition(pat='_')
tm.assert_frame_equal(result, expected)

# str.rpartition
# using sep -> no warning
expected = values.str.rpartition(sep='_')
with tm.assert_produces_warning(FutureWarning):
result = values.str.rpartition(pat='_')
tm.assert_frame_equal(result, expected)

def test_pipe_failures(self):
# #2119
s = Series(['A|B|C'])
Expand Down