Skip to content

BUG: rsplit not behaving as expected with regex "\s" for whitespace #49006

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

Closed
3 tasks done
adesuwa-osagie opened this issue Oct 8, 2022 · 5 comments
Closed
3 tasks done
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@adesuwa-osagie
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

s = pd.Series("this is a regular sentence")

s.str.rsplit("\s", n=1, expand=True) # <-- this produces the error

Issue Description

Splitting is not done at the whitespace before the string "sentence"

Also tried: s.str.rsplit(r"\s", n=1, expand=True)

What worked: s.str.rsplit(" ", n=1, expand=True)

Expected Behavior

expected-result

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 167 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.4
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@adesuwa-osagie adesuwa-osagie added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2022
@adesuwa-osagie adesuwa-osagie changed the title BUG: rsplit not behaving as expected with regex "\s" BUG: rsplit not behaving as expected with regex "\s" for whitespace Oct 8, 2022
@dannyi96
Copy link
Contributor

dannyi96 commented Oct 8, 2022

An observation - Pandas .str.split supports pat argument to be of regex form but .str.rsplit does not support pat to be a regex

split -

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "pat"])
@forbid_nonstring_types(["bytes"])
def split(
self,
pat: str | re.Pattern | None = None,
n=-1,
expand: bool = False,
*,
regex: bool | None = None,
):
if regex is False and is_re(pat):
raise ValueError(
"Cannot use a compiled regex as replacement pattern with regex=False"
)
if is_re(pat):
regex = True
result = self._data.array._str_split(pat, n, expand, regex)
return self._wrap_result(result, returns_string=expand, expand=expand)

rsplit -

@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "pat"])
@forbid_nonstring_types(["bytes"])
def rsplit(self, pat=None, n=-1, expand: bool = False):
result = self._data.array._str_rsplit(pat, n=n)
return self._wrap_result(result, expand=expand, returns_string=expand)

I believe this is the expected behavior as per current pandas code
but it can be enhanced so that rsplit supports regex based inputs as well

@MarcoGorelli
Copy link
Member

it can be enhanced so that rsplit supports regex based inputs as well

I don't think this is possible - can you check the issue tracker for duplicates please? Pretty sure this is there

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Oct 8, 2022

Here it is #46488

re.Pattern has split, but not rsplit, so this isn't straightforward without some crazy workaround. Let's just separate out the split and rsplit docs such that the rsplit ones don't have the regex argument

@MarcoGorelli
Copy link
Member

Closing then, but thanks for the suggestion

@adesuwa-osagie
Copy link
Author

adesuwa-osagie commented Oct 8, 2022

@dannyi96 @MarcoGorelli Thank you for taking the time to look at it and providing the link to the issue that explains that regex not working with rsplit is actually an expected behavior. I appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants