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
Implement StringMethod slice_replace #8888
Comments
can you show a pseudo-example of what you are contemplating? |
Sure thing. Looks like the skeleton got added in #1621 but behavior was never specified (Docs show the info for Basic functionality is straightforward, remove the specified slice, replace with a specified string. A test:
Tricky cases are when the slice returns an empty string. I could see two ways of doing it: leave the string as is, or force the replacement string in there, depending on exactly why the slice returned nothing. I think leave alone is the right way to go, but maybe I'm wrong.
|
I think this should be a natural extension to In [6]: s = pd.Series(['foo', 'bar', 'baz']); s
Out[6]:
0 foo
1 bar
2 baz
dtype: object
In [7]: s.str[:2]
Out[7]:
0 fo
1 ba
2 ba
dtype: object It's quite natural to also support setting them all at once, e.g. And this would allow us to deprecate and drop slice and slice_replace methods completely and use their pythonic counterparts. |
Well, strings being immutable there is no slice_replace-like behavior to replicate, but we can replicate the behavior with lists, which is the second option in each case (insert at start index or append to end). Implementing setitem is fairly easy as well. |
The
StringMethod
slice_replace
currently raisesNotImplementedError
. Couldn't find an issue for it, so opening this. PR with implementation in the works.Could use thoughts on how to handle slices that return an empty string on some subset of strings - should the replacement string appear or not? This could happen when the input string is empty, the stop index is less than or equal to the start index (separate cases?), or both start and stop are greater than the length of the string.
The text was updated successfully, but these errors were encountered: