-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: update the pandas.Series.str.slice_replace docstring #20273
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
Changes from 3 commits
916f4cb
bbed286
7a0cf0c
56a854e
5263522
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1180,19 +1180,75 @@ def str_slice(arr, start=None, stop=None, step=None): | |
|
||
def str_slice_replace(arr, start=None, stop=None, repl=None): | ||
""" | ||
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 | ||
stop : int or None | ||
repl : str or None | ||
String for replacement | ||
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, optional | ||
String for replacement. The default of None replaces the slice | ||
with an empty string. | ||
|
||
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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a See Also, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we also have |
||
Examples | ||
-------- | ||
>>> s = pd.Series(['a', 'ab', 'abc', 'abdc', 'abcde']) | ||
>>> s | ||
0 a | ||
1 ab | ||
2 abc | ||
3 abdc | ||
4 abcde | ||
dtype: object | ||
|
||
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 | ||
|
||
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 | ||
""" | ||
if repl is None: | ||
repl = '' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General question: do we more want to use the phrase of "If not specified" instead of "The default of None" if we say something is optional?
On the one hand I think this can be clearer as that is what you do in practice, you don't specify it. But on the other hand of course the signature shows None.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, if not specified is probably best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went with
if not specified (None)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah,
If not specified (None)
, is a good combination of both, we should use that pattern more