Skip to content

BUG: Unexpected keyword argument maxsplit on rsplit even though it is accepted in vanilla Python #47423

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
paw-lu opened this issue Jun 19, 2022 · 5 comments · Fixed by #47446
Closed
3 tasks done
Labels
Deprecate Functionality to remove in pandas good first issue Strings String extension data type and string data
Milestone

Comments

@paw-lu
Copy link
Contributor

paw-lu commented Jun 19, 2022

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
>>> string = "foo,bar,lorep"
>>> string.rsplit(",", maxsplit=1)  # This works as expected
['foo,bar', 'lorep']
>>> pd.DataFrame({"a": [string]})["a"].str.rsplit(",", maxsplit=1)  # Use kwarg maxsplit
TypeError: rsplit() got an unexpected keyword argument 'maxsplit'
>>> pd.DataFrame({"a": [string]})["a"].str.rsplit(",", 1)  # Change kwarg to arg
0    [foo,bar, lorep]
Name: a, dtype: object

Issue Description

When using pd.Series.str.rsplit in Pandas, the accepted kwargs are inconsistent with Python's.

Python's str.rsplit accepts the kwarg maxsplit=, while Panda's pd.Series.str.rsplit does not.

Expected Behavior

I expect Panda's pd.Series.str.rsplit to behave identically to Python's str.rsplit, this includes accepting the same kwargs, namely maxsplit= in this case.

Installed Versions

pd.show_versions()
/Users/pawlu/.virtualenvs/pandas-debug-kqdr/lib/python3.9/site-packages/_distutils_hack/__init__.py:30: UserWarning: Setuptools is replacing distutils.
  warnings.warn("Setuptools is replacing distutils.")

INSTALLED VERSIONS
------------------
commit           : 47494a48edf25d5a49b0fb5b896b454c15c83595
python           : 3.9.12.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 21.4.0
Version          : Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.5.0.dev0+978.g47494a48e
numpy            : 1.22.4
pytz             : 2022.1
dateutil         : 2.8.2
setuptools       : 62.2.0
pip              : 22.1.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.4.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
@paw-lu paw-lu added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 19, 2022
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Jun 19, 2022

Hey @paw-lu

The parameter rpslit does accept (n), but it's a bit different to maxsplits, as it treats 0 and -1 the same way (split as much as possible):

>>> string
'foo,bar,lorep'
>>> string.rsplit(',', maxsplit=0)  # no splits
['foo,bar,lorep']
>>> pd.Series(string).str.rsplit(',', n=0)  # all splits
0    [foo, bar, lorep]
dtype: object

Another difference is that n can accept None, but maxsplits can't

>>> string.rsplit(',', None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object cannot be interpreted as an integer
>>> pd.Series(string).str.rsplit(',', n=None)
0    [foo, bar, lorep]
dtype: object

I'm not sure it's worth going through a deprecation cycle to introduce maxsplits and remove n, but I do think it'd be worthwhile to deprecate passing n as positional. This way, people won't confuse it with maxsplits

i.e. to disallow

pd.Series(string).str.rsplit(',', 1)

in favour of

pd.Series(string).str.rsplit(',', n=1)

This would require using the deprecate_nonkeyword_arguments decorator

(apologies for closing, was an accident, I reopened straight away)

@MarcoGorelli MarcoGorelli reopened this Jun 19, 2022
@MarcoGorelli MarcoGorelli added Deprecate Functionality to remove in pandas Strings String extension data type and string data and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 19, 2022
@paw-lu
Copy link
Contributor Author

paw-lu commented Jun 19, 2022

The parameter rpslit does accept (n), but it's a bit different to maxsplits, as it treats 0 and -1 the same way (split as much as possible)

Another difference is that n can accept None, but maxsplits can't

Completeley missed both of these! Didn't know the Pandas implimentation had drifted from the standard library like this.

I'm not sure it's worth going through a deprecation cycle to introduce maxsplits and remove n

Yeah now that I know some of the context (thanks btw) I agree with you.

but I do think it'd be worthwhile to deprecate passing n as positional. This way, people won't confuse it with maxsplits

Good idea here. I agree on this one too.

I can take responsibility for implimenting this—but it will be a while until I get to it! It's all good if someone beats me to it!

@MarcoGorelli
Copy link
Member

Cool - if anyone wants to take this on, here's a similar PR you can use as an example https://github.com/pandas-dev/pandas/pull/41699/files

@MrShevan
Copy link
Contributor

Hi, Can I take on me? :)

@MarcoGorelli
Copy link
Member

Yeah go ahead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Deprecate Functionality to remove in pandas good first issue Strings String extension data type and string data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants