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

ZeLobster picture ZeLobster · Aug 1, 2018 · Viewed 7.5k times · Source

I'm trying to follow a tutorial on sound classification in neural networks, and I've found 3 different versions of the same tutorial, all of which work, but they all reach a snag at this point in the code, where I get the "AttributeError: 'Series' object has no attribute 'label'" issue. I'm not particularly au fait with either NNs or Python, so apologies if this is something trivial like a deprecation error, but I can't seem to figure it out myself.

from sklearn.preprocessing import LabelEncoder

X = np.array(temp.feature.tolist())
y = np.array(temp.label.tolist())

lb = LabelEncoder()

y = np_utils.to_categorical(lb.fit_transform(y))

As mentioned, I've seen three different tutorials on the same subject, all of which end with the same "temp = train.apply(parser, axis=1) temp.columns = ['feature', 'label']" fragment, so I'm assuming this is assigning correctly, but I don't know where it's going wrong otherwise. Help appreciated!

Edit: Traceback as requested, turns out I'd added the wrong traceback. Also I've since found out that this is a case of converting the series object to a dataframe, so any help with that would be great.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-1613f53e2d98> in <module>()
  1 from sklearn.preprocessing import LabelEncoder
  2 
----> 3 X = np.array(temp.feature.tolist())
  4 y = np.array(temp.label.tolist())
  5 

/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   4370             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4371                 return self[name]
-> 4372             return object.__getattribute__(self, name)
   4373 
   4374     def __setattr__(self, name, value):

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

Answer

Vivek Kumar picture Vivek Kumar · Aug 3, 2018

Your current implementation of parser(row) method returns a list for each row of data from train DataFrame. But this is then collected as a pandas.Series object.

So your temp is actually a Series object. Then the following line dont have any effect:

temp.columns = ['feature', 'label']

Since temp is a Series, it does not have any columns, and hence temp.feature and temp.label dont exist and hence the error.

Change your parser() method as following:

def parser(row):
    ...
    ...
    ...

    # Return pandas.Series instead of List
    return pd.Series([feature, label])

By doing this, the apply method from temp = train.apply(parser, axis=1) will return a DataFrame, so your other code will work.

I cannot say about the tutorials you are following. Maybe they followed an older version of pandas which allowed a list to be automatically converted to DataFrame.