1

I'm trying to use Series.str.rsplit to return everything up to the delimiter, but can't seem to get past this TypeError I'm receiving:

TypeError: StringMethods.rsplit() takes from 1 to 2 positional arguments but 3 were given

I am using pandas 2.0.3. Here is my code:

import pandas as pd

df1 = pd.DataFrame({'name': ['tom ent', 'nick', 'krish std', 'jack whatever'],
                    'age': ['5', '6', '7', '8']})

df1['name'] = df1['name'].str.rsplit(' ', 1).str[0]

Output should look like:

name    age
tom       5
nick      6
krish     7
jack      8

Not sure what I'm missing here.

ouroboros1
  • 9,113
  • 3
  • 7
  • 26
surge3333
  • 133
  • 1
  • 8
  • 2
    I don't get an error with the code you provided. What version of `pandas` are you using? – not_speshal Aug 01 '23 at 14:01
  • 2
    Wow, you guys should definitely update Pandas then :) This change was not even introduced with the 2.x branch that made everyone complain about the breaking changes. – Karl Knechtel Aug 01 '23 at 14:07
  • @KarlKnechtel - Thank you. Just did. [Anaconda](https://anaconda.org/anaconda/pandas) is still on 1.5.3 (which gave a Warning but not an error) – not_speshal Aug 01 '23 at 14:11

1 Answers1

2

In Pandas 1.4 and before, the .rsplit method used ordinary parameters, so a call like this would mean that ' ' is the delimiter and 1 is the maximum number of times to split - just like the built-in method of the str class.

However, starting in 1.5, n and expand are keyword-only parameters. To use them, the name needs to be specified explicitly, like: .rsplit(' ', n=1).

The idea is that, because the programmer is forced to use names, there is no chance of mistaking the order.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153