Does pandas Series.update() not really allow the index parameter?

Question:

In Pandas documentation for their Series.update function, they give the following example

s = pd.Series(['a', 'b', 'c'])
s.update(pd.Series(['d', 'e'], index=[0, 2]))
s
0    d
1    b
2    e
dtype: object

https://pandas.pydata.org/docs/reference/api/pandas.Series.update.html

But when I recreate a similar example, I get a TypeError that says
Series.update() got an unexpected keyword argument 'index'


My Try, using pandas version 1.4.3:

INPUT:
ser = pd.Series(['a', 'b', 'c'])
print(ser)

OUTPUT:
0    a
1    b
2    c
dtype: object

INPUT:
replacer = pd.Series(['x','z'])
print(replacer)

OUTPUT:
0    x
1    z
dtype: object

INPUT:
ser.update(replacer, index=[0, 1])

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
c:Userssomebodyprojectsmy_projectmy_notebook.ipynb Cell 11 in <cell line: 1>()
----> 1 ser.update(replacer, index=[0, 1])

TypeError: Series.update() got an unexpected keyword argument 'index'


Is this ‘index’ keyword parameter just not implemented or is there something very obvious that I am not doing?

Asked By: Nesha25

||

Answers:

I assume it’s a documentation bug. In the source code, the function is defined as

    def update(self, other) -> None:

It doesn’t even take an *args to allow for other options. You might file a bug report.

Answered By: Tim Roberts

This is not a bug. Look carefully at the brackets:

s.update(pd.Series ( [‘d’, ‘e’], index=[0, 2] ) )

They cover both the index and the "d" and "e". In your code you only bracketed the two letters without the index no. inside as well.

As @Tim_Roberts has mentioned, this is from the source codeand the function is:

    def update(self, other) -> None:

This function has no *args and therefore you cannot simply use indexon its own in the function.

Answered By: DialFrost
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.