How to fix AttributeError: 'Series' object has no attribute 'append'
Asked Answered
B

3

7

Hi I'm still learning pandas and numpy in python

I learn from e-learning that you could combine 2 series with append but when I tried it It give me error

students_classes = pd.Series({'Alice': 'Physics',
                   'Jack': 'Chemistry',
                   'Molly': 'English',
                   'Sam': 'History'})
students_classes

kelly_classes = pd.Series(['Philosophy', 'Arts', 'Math'], index=['Kelly', 'Kelly', 'Kelly'])
kelly_classes

all_students_classes = students_classes.append(kelly_classes)

all_students_classes

and It give me error like this

AttributeError                            Traceback (most recent call last)
Cell In\[35\], line 3
1 # Finally, we can append all of the data in this new Series to the first using the .append()
2 # function.
\----\> 3 all_students_classes.str = students_classes.append(kelly_classes)
5 # This creates a series which has our original people in it as well as all of Kelly's courses
6 all_students_classes

File \~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\core\\generic.py:5989, in NDFrame.__getattr__(self, name)
5982 if (
5983     name not in self.\_internal_names_set
5984     and name not in self.\_metadata
5985     and name not in self.\_accessors
5986     and self.\_info_axis.\_can_hold_identifiers_and_holds_name(name)
5987 ):
5988     return self\[name\]
\-\> 5989 return object.__getattribute__(self, name)

AttributeError: 'Series' object has no attribute 'append'

I expect something like this from the e-learning

Bergeron answered 25/4, 2023 at 14:43 Comment(2)
Did you look up the official pandas documentation? Does the Series have a append method? Also what version is your pandas?Lennielenno
It seems that the append method has been removed. In my colab version, there is a warning that says The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead In my conda version, it is unusable.Pakistan
L
15

In relatively recent pandas version, 1.5.2, append works, but gives a warning.

In pd 2.0, append has been removed

https://pandas.pydata.org/docs/dev/whatsnew/v2.0.0.html#deprecations

In [14]: students_classes = pd.Series({'Alice': 'Physics',
    ...:                    'Jack': 'Chemistry',
    ...:                    'Molly': 'English',
    ...:                    'Sam': 'History'})
    ...: kelly_classes = pd.Series(['Philosophy', 'Arts', 'Math'], index=['Kelly', 'Kelly', 'Kelly'])

In [15]: students_classes.append(kelly_classes)
C:\Users\paul\AppData\Local\Temp\ipykernel_6072\990183765.py:1: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  students_classes.append(kelly_classes)
Out[15]: 
Alice       Physics
Jack      Chemistry
Molly       English
Sam         History
Kelly    Philosophy
Kelly          Arts
Kelly          Math
dtype: object

Under the covers, append method uses _append, which works without raising the warning:

In [16]: students_classes._append(kelly_classes)
Out[16]: 
Alice       Physics
Jack      Chemistry
Molly       English
Sam         History
Kelly    Philosophy
Kelly          Arts
Kelly          Math
dtype: object

And using the recommended concat:

In [18]: pd.concat([students_classes,kelly_classes])
Out[18]: 
Alice       Physics
Jack      Chemistry
Molly       English
Sam         History
Kelly    Philosophy
Kelly          Arts
Kelly          Math
dtype: object

Python lists have an efficient append method. numpy has a np.append function which is a poorly named cover for calling np.concatenate, and is often misused (it shouldn't be used iteratively). pandas may be trying to avoid similar problems by getting rid of the append method. With pd.concat you can join many Series (or frames) at once, and aren't (as) tempted to use it in a loop.

Looking up the code for _append (which is still in 2.0), I see it ends up using pd.concat. So there's no value in using this 'work-around'. Use concat as recommended.

Lennielenno answered 25/4, 2023 at 18:3 Comment(4)
Thank you! Also thank you for your recommendation!!! will try to use concat for futureBergeron
I tried your suggestion, and it throws the same error as the original. I am using Pandas 2.0.1. Instead of the error "AttributeError: 'DataFrame' object has no attribute 'concat'", it throws "AttributeError: 'DataFrame' object has no attribute 'concat'Kinetics
No, read the docs. pd.concat([df1, df2,...]). It's a function, not a method!Lennielenno
FYI, using pd.concat results in TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid. I had to cast my string as a Series to add using pd.Series()Nils
V
4

Probably an update of pandas may have changed the syntax, but you probably are looking for:

all_students_classes = students_classes._append(kelly_classes)

(quick tip, you can check the class definition in VS code by right cliking on the pd.Series and choosing Go to Definition, then you can see some of the methods defined at least). Otherwise use the concat method that uses this.

Vamoose answered 25/4, 2023 at 17:29 Comment(1)
I don't think it's advisable to recommend a private function, especially when the correct ".append()" gives the user fair warning that the method call will be removed from pandas 2.0 onwards.Hereford
S
1

When the append method cannot be replaced due to configured environment restrictions, I recommend you to use _append

Sacttler answered 12/2 at 3:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.