From 916f4cb0a54c491d4192f373ed9837d4a06d2755 Mon Sep 17 00:00:00 2001 From: Laksh Date: Sun, 11 Mar 2018 13:06:38 +0530 Subject: [PATCH 1/4] update doc for method slice_replace --- pandas/core/strings.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index fac607f4621a8..52cd30f8251c3 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1180,19 +1180,36 @@ def str_slice(arr, start=None, stop=None, step=None): def str_slice_replace(arr, start=None, stop=None, repl=None): """ + Replace a sliced string. + Replace a slice of each string in the Series/Index with another string. Parameters ---------- start : int or None + Left edge index. stop : int or None + Right edge index. repl : str or None - String for replacement + String for replacement. Returns ------- replaced : Series/Index of objects + + Examples + -------- + >>> s = pd.Series(['This is a Test 1', 'This is a Test 2']) + >>> s + 0 This is a Test 1 + 1 This is a Test 2 + dtype: object + >>> s = s.str.slice_replace(8, 14, 'an Example') + >>> s + 0 This is an Example 1 + 1 This is an Example 2 + dtype: object """ if repl is None: repl = '' From bbed286f136967ac249701f4e6b9ec65789441d7 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 14 Mar 2018 14:12:50 -0500 Subject: [PATCH 2/4] Updated --- pandas/core/strings.py | 77 ++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 52cd30f8251c3..dfa6ddd754b47 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1178,42 +1178,77 @@ def str_slice(arr, start=None, stop=None, step=None): return _na_map(f, arr) -def str_slice_replace(arr, start=None, stop=None, repl=None): +def str_slice_replace(arr, start=None, stop=None, repl=''): """ - Replace a sliced string. - - Replace a slice of each string in the Series/Index with another - string. + Replace a positional slice of a string with another value. Parameters ---------- - start : int or None - Left edge index. - stop : int or None - Right edge index. - repl : str or None + start : int, optional + Left index position to use for the slice. The default of None + implies a slice unbound on the left, i.e. slice from the start + of the string. + stop : int, optional + Right index position to use for the slice. The default of None + implies a slice unbounded on the right, i.e. slice until the + end of the string. + repl : str, default '' String for replacement. Returns ------- - replaced : Series/Index of objects + replaced : Series or Index + Same type as the original object. + + See Also + -------- + Series.str.slice : Just slicing without replacement. Examples -------- - >>> s = pd.Series(['This is a Test 1', 'This is a Test 2']) + >>> s = pd.Series(['a', 'ab', 'abc', 'abdc', 'abcde']) >>> s - 0 This is a Test 1 - 1 This is a Test 2 + 0 a + 1 ab + 2 abc + 3 abdc + 4 abcde dtype: object - >>> s = s.str.slice_replace(8, 14, 'an Example') - >>> s - 0 This is an Example 1 - 1 This is an Example 2 + + Specify just `start`, meaning replace `start` until the end of the + string with `repl`. + + >>> s.str.slice_replace(1, repl='X') + 0 aX + 1 aX + 2 aX + 3 aX + 4 aX dtype: object - """ - if repl is None: - repl = '' + Specify just `stop`, meaning the start of the string to `stop` is replaced + with `repl`, and the rest of the string is included. + + >>> s.str.slice_replace(stop=2, repl='X') + 0 X + 1 X + 2 Xc + 3 Xdc + 4 Xcde + dtype: object + + Specify `start` and `stop`, meaning the slice from `start` to `stop` is + replaced with `repl`. Everything before or after `start` and `stop` is + included as is. + + >>> s.str.slice_replace(start=1, stop=3, repl='X') + 0 aX + 1 aX + 2 aX + 3 aXc + 4 aXde + dtype: object + """ def f(x): if x[start:stop] == '': local_stop = start From 7a0cf0c93ad4778c78a6c7a477f9309cc6e13403 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 14 Mar 2018 14:16:05 -0500 Subject: [PATCH 3/4] Fix tests --- pandas/core/strings.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index dfa6ddd754b47..f959f1ab0f042 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1178,7 +1178,7 @@ def str_slice(arr, start=None, stop=None, step=None): return _na_map(f, arr) -def str_slice_replace(arr, start=None, stop=None, repl=''): +def str_slice_replace(arr, start=None, stop=None, repl=None): """ Replace a positional slice of a string with another value. @@ -1192,8 +1192,9 @@ def str_slice_replace(arr, start=None, stop=None, repl=''): Right index position to use for the slice. The default of None implies a slice unbounded on the right, i.e. slice until the end of the string. - repl : str, default '' - String for replacement. + repl : str, optional + String for replacement. The default of None replaces the slice + with an empty string. Returns ------- @@ -1249,6 +1250,9 @@ def str_slice_replace(arr, start=None, stop=None, repl=''): 4 aXde dtype: object """ + if repl is None: + repl = '' + def f(x): if x[start:stop] == '': local_stop = start From 56a854e4d24af02f497d4d5e27e107c8349da2c1 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 16 Mar 2018 06:46:20 -0500 Subject: [PATCH 4/4] Updated [ci skip] [ci skip] --- pandas/core/strings.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/strings.py b/pandas/core/strings.py index f959f1ab0f042..24799be81884c 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1185,16 +1185,16 @@ def str_slice_replace(arr, start=None, stop=None, repl=None): Parameters ---------- start : int, optional - Left index position to use for the slice. The default of None - implies a slice unbound on the left, i.e. slice from the start + Left index position to use for the slice. If not specified (None), + the slice is unbounded on the left, i.e. slice from the start of the string. stop : int, optional - Right index position to use for the slice. The default of None - implies a slice unbounded on the right, i.e. slice until the + Right index position to use for the slice. If not specified (None), + the slice is unbounded on the right, i.e. slice until the end of the string. repl : str, optional - String for replacement. The default of None replaces the slice - with an empty string. + String for replacement. If not specified (None), the sliced region + is replaced with an empty string. Returns -------