Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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:\Users\somebody\projects\my_project\my_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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading