Skip to content

Commit cc7f0bf

Browse files
committed
ENH: Add StringMethods.partition and rpartition
1 parent cb8c130 commit cc7f0bf

File tree

5 files changed

+150
-11
lines changed

5 files changed

+150
-11
lines changed

doc/source/api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,12 @@ strings and apply several methods to it. These can be acccessed like
539539
Series.str.lstrip
540540
Series.str.match
541541
Series.str.pad
542+
Series.str.partition
542543
Series.str.repeat
543544
Series.str.replace
544545
Series.str.rfind
545546
Series.str.rjust
547+
Series.str.rpartition
546548
Series.str.rstrip
547549
Series.str.slice
548550
Series.str.slice_replace

doc/source/text.rst

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ Method Summary
229229
:meth:`~Series.str.strip`,Equivalent to ``str.strip``
230230
:meth:`~Series.str.rstrip`,Equivalent to ``str.rstrip``
231231
:meth:`~Series.str.lstrip`,Equivalent to ``str.lstrip``
232+
:meth:`~Series.str.partition`,Equivalent to ``str.partition``
233+
:meth:`~Series.str.rpartition`,Equivalent to ``str.rpartition``
232234
:meth:`~Series.str.lower`,Equivalent to ``str.lower``
233235
:meth:`~Series.str.upper`,Equivalent to ``str.upper``
234236
:meth:`~Series.str.find`,Equivalent to ``str.find``

doc/source/whatsnew/v0.16.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Enhancements
1919

2020

2121

22-
22+
- Added ``StringMethods.partition()`` and ``rpartition()`` which behave as the same as standard ``str`` (:issue:`9773`)
2323

2424

2525
.. _whatsnew_0161.api:

pandas/core/strings.py

+45-10
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,19 @@ def str_pad(arr, width, side='left', fillchar=' '):
622622
return _na_map(f, arr)
623623

624624

625+
def _return_type_wrapper(f, arr, return_type):
626+
if return_type not in ('series', 'frame'):
627+
raise ValueError("return_type must be {'series', 'frame'}")
628+
629+
if return_type == 'frame':
630+
from pandas.core.frame import DataFrame
631+
from pandas.core.series import Series
632+
return DataFrame((Series(x) for x in _na_map(f, arr)), index=arr.index)
633+
634+
else:
635+
return _na_map(f, arr)
636+
637+
625638
def str_split(arr, pat=None, n=None, return_type='series'):
626639
"""
627640
Split each string (a la re.split) in array by given pattern, propagating NA
@@ -644,11 +657,6 @@ def str_split(arr, pat=None, n=None, return_type='series'):
644657
-------
645658
split : array
646659
"""
647-
from pandas.core.series import Series
648-
from pandas.core.frame import DataFrame
649-
650-
if return_type not in ('series', 'frame'):
651-
raise ValueError("return_type must be {'series', 'frame'}")
652660
if pat is None:
653661
if n is None or n == 0:
654662
n = -1
@@ -663,11 +671,7 @@ def str_split(arr, pat=None, n=None, return_type='series'):
663671
n = 0
664672
regex = re.compile(pat)
665673
f = lambda x: regex.split(x, maxsplit=n)
666-
if return_type == 'frame':
667-
res = DataFrame((Series(x) for x in _na_map(f, arr)), index=arr.index)
668-
else:
669-
res = _na_map(f, arr)
670-
return res
674+
return _return_type_wrapper(f, arr, return_type)
671675

672676

673677
def str_slice(arr, start=None, stop=None, step=None):
@@ -978,6 +982,37 @@ def split(self, pat=None, n=-1, return_type='series'):
978982
result = str_split(self.series, pat, n=n, return_type=return_type)
979983
return self._wrap_result(result)
980984

985+
_shared_docs['str_partition'] = ("""
986+
Split the string at the %(side)s occurrence of sep, and return a 3-tuple containing the part
987+
before the separator, the separator itself, and the part after the separator.
988+
If the separator is not found, return %(return)s.
989+
990+
Parameters
991+
----------
992+
pat : string, default whitespace
993+
String to split on.
994+
return_type : {'series', 'frame'}, default 'frame'
995+
If frame, returns a DataFrame (elements are strings)
996+
If series, returns an Series (elements are lists of strings).
997+
998+
Returns
999+
-------
1000+
split : array
1001+
""")
1002+
@Appender(_shared_docs['str_partition'] % {'side': 'first',
1003+
'return': 'a 3-tuple containing the string itself, followed by two empty strings'})
1004+
def partition(self, pat=' ', return_type='frame'):
1005+
f = lambda x: x.partition(pat)
1006+
result = _return_type_wrapper(f, self.series, return_type)
1007+
return self._wrap_result(result)
1008+
1009+
@Appender(_shared_docs['str_partition'] % {'side': 'last',
1010+
'return': 'a 3-tuple containing two empty strings, followed by the string itself'})
1011+
def rpartition(self, pat=' ', return_type='frame'):
1012+
f = lambda x: x.rpartition(pat)
1013+
result = _return_type_wrapper(f, self.series, return_type)
1014+
return self._wrap_result(result)
1015+
9811016
@copy(str_get)
9821017
def get(self, i):
9831018
result = str_get(self.series, i)

pandas/tests/test_strings.py

+100
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ def test_empty_str_methods(self):
617617
tm.assert_series_equal(empty_str, empty.str.pad(42))
618618
tm.assert_series_equal(empty_str, empty.str.center(42))
619619
tm.assert_series_equal(empty_list, empty.str.split('a'))
620+
tm.assert_series_equal(empty_list, empty.str.partition('a', return_type='series'))
621+
tm.assert_series_equal(empty_list, empty.str.rpartition('a', return_type='series'))
620622
tm.assert_series_equal(empty_str, empty.str.slice(stop=1))
621623
tm.assert_series_equal(empty_str, empty.str.slice(step=1))
622624
tm.assert_series_equal(empty_str, empty.str.strip())
@@ -637,6 +639,13 @@ def test_empty_str_methods(self):
637639
tm.assert_series_equal(empty_str, empty.str.isnumeric())
638640
tm.assert_series_equal(empty_str, empty.str.isdecimal())
639641

642+
def test_empty_str_methods_to_frame(self):
643+
empty_str = empty = Series(dtype=str)
644+
empty_df = DataFrame([])
645+
646+
tm.assert_frame_equal(empty_df, empty.str.partition('a'))
647+
tm.assert_frame_equal(empty_df, empty.str.rpartition('a'))
648+
640649
def test_ismethods(self):
641650
values = ['A', 'b', 'Xy', '4', '3A', '', 'TT', '55', '-', ' ']
642651
str_s = Series(values)
@@ -1125,6 +1134,97 @@ def test_split_to_dataframe(self):
11251134
with tm.assertRaisesRegexp(ValueError, "return_type must be"):
11261135
s.str.split('_', return_type="some_invalid_type")
11271136

1137+
def test_partition_series(self):
1138+
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
1139+
1140+
result = values.str.partition('_', return_type='series')
1141+
exp = Series([['a', '_', 'b_c'], ['c', '_', 'd_e'], NA, ['f', '_', 'g_h']])
1142+
tm.assert_series_equal(result, exp)
1143+
1144+
result = values.str.rpartition('_', return_type='series')
1145+
exp = Series([['a_b', '_', 'c'], ['c_d', '_', 'e'], NA, ['f_g', '_', 'h']])
1146+
tm.assert_series_equal(result, exp)
1147+
1148+
# more than one char
1149+
values = Series(['a__b__c', 'c__d__e', NA, 'f__g__h'])
1150+
result = values.str.partition('__', return_type='series')
1151+
exp = Series([['a', '__', 'b__c'], ['c', '__', 'd__e'], NA, ['f', '__', 'g__h']])
1152+
tm.assert_series_equal(result, exp)
1153+
1154+
result = values.str.rpartition('__', return_type='series')
1155+
exp = Series([['a__b', '__', 'c'], ['c__d', '__', 'e'], NA, ['f__g', '__', 'h']])
1156+
tm.assert_series_equal(result, exp)
1157+
1158+
# None
1159+
values = Series(['a b c', 'c d e', NA, 'f g h'])
1160+
result = values.str.partition(return_type='series')
1161+
exp = Series([['a', ' ', 'b c'], ['c', ' ', 'd e'], NA, ['f', ' ', 'g h']])
1162+
tm.assert_series_equal(result, exp)
1163+
1164+
result = values.str.rpartition(return_type='series')
1165+
exp = Series([['a b', ' ', 'c'], ['c d', ' ', 'e'], NA, ['f g', ' ', 'h']])
1166+
tm.assert_series_equal(result, exp)
1167+
1168+
# Not splited
1169+
values = Series(['abc', 'cde', NA, 'fgh'])
1170+
result = values.str.partition('_', return_type='series')
1171+
exp = Series([['abc', '', ''], ['cde', '', ''], NA, ['fgh', '', '']])
1172+
tm.assert_series_equal(result, exp)
1173+
1174+
result = values.str.rpartition('_', return_type='series')
1175+
exp = Series([['', '', 'abc'], ['', '', 'cde'], NA, ['', '', 'fgh']])
1176+
tm.assert_series_equal(result, exp)
1177+
1178+
# unicode
1179+
values = Series([u('a_b_c'), u('c_d_e'), NA, u('f_g_h')])
1180+
1181+
result = values.str.partition('_', return_type='series')
1182+
exp = Series([[u('a'), u('_'), u('b_c')], [u('c'), u('_'), u('d_e')],
1183+
NA, [u('f'), u('_'), u('g_h')]])
1184+
tm.assert_series_equal(result, exp)
1185+
1186+
result = values.str.rpartition('_', return_type='series')
1187+
exp = Series([[u('a_b'), u('_'), u('c')], [u('c_d'), u('_'), u('e')],
1188+
NA, [u('f_g'), u('_'), u('h')]])
1189+
tm.assert_series_equal(result, exp)
1190+
1191+
# compare to standard lib
1192+
values = Series(['A_B_C', 'B_C_D', 'E_F_G', 'EFGHEF'])
1193+
result = values.str.partition('_', return_type='series').tolist()
1194+
self.assertEqual(result, [v.partition('_') for v in values])
1195+
result = values.str.rpartition('_', return_type='series').tolist()
1196+
self.assertEqual(result, [v.rpartition('_') for v in values])
1197+
1198+
def test_partition_to_dataframe(self):
1199+
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
1200+
result = values.str.partition('_')
1201+
exp = DataFrame({0: ['a', 'c', np.nan, 'f'],
1202+
1: ['_', '_', np.nan, '_'],
1203+
2: ['b_c', 'd_e', np.nan, 'g_h']})
1204+
tm.assert_frame_equal(result, exp)
1205+
1206+
result = values.str.rpartition('_')
1207+
exp = DataFrame({0: ['a_b', 'c_d', np.nan, 'f_g'],
1208+
1: ['_', '_', np.nan, '_'],
1209+
2: ['c', 'e', np.nan, 'h']})
1210+
tm.assert_frame_equal(result, exp)
1211+
1212+
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
1213+
result = values.str.partition('_', return_type='frame')
1214+
exp = DataFrame({0: ['a', 'c', np.nan, 'f'],
1215+
1: ['_', '_', np.nan, '_'],
1216+
2: ['b_c', 'd_e', np.nan, 'g_h']})
1217+
tm.assert_frame_equal(result, exp)
1218+
1219+
result = values.str.rpartition('_', return_type='frame')
1220+
exp = DataFrame({0: ['a_b', 'c_d', np.nan, 'f_g'],
1221+
1: ['_', '_', np.nan, '_'],
1222+
2: ['c', 'e', np.nan, 'h']})
1223+
tm.assert_frame_equal(result, exp)
1224+
1225+
with tm.assertRaisesRegexp(ValueError, "return_type must be"):
1226+
values.str.partition('_', return_type="some_invalid_type")
1227+
11281228
def test_pipe_failures(self):
11291229
# #2119
11301230
s = Series(['A|B|C'])

0 commit comments

Comments
 (0)