Python: Pandas Dataframe AttributeError: 'numpy.ndarray' object has no attribute 'fillna'

45,490

Solution 1

(M - 3) is getting interpreted as a numpy.ndarray. This implies that M is defined somewhere as a numpy.ndarray. Test it out by running:

print type(M)

Solution 2

You are calling the .fillna() method on a numpy array. And numpy arrays don't have that method defined.

You can probably convert the numpy array to a pandas.DataFrame and then apply the .fillna() method.

Solution 3

Your code is not complete at the moment, so it is hard to pin point why M is causing an error. There could be a couple reasons:

  1. You have a typo and (M - 3) should be (M2 - 3)

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M2 - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  2. You need to define/convert M as pandas.DataFrame somewhere else in your code

    # With out seeing this part of the code, no one can really help you
    M = pd.DataFrame(...)
    # ...
    # ...
    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
  3. You could convert it to a pandas.DataFrame right before you use it.

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (pd.DataFrame(M) - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
Share:
45,490
jeangelj
Author by

jeangelj

BY DAY: Data Analyst BY NIGHT: Assassin's Creed & Tomb Raider player "The Matrix has you ..." - Trinity

Updated on October 07, 2020

Comments

  • jeangelj
    jeangelj over 3 years

    Since I am creating a dataframe, I don't understand why I am getting an array error.

    M2 = df.groupby(['song_id', 'user_id']).rating.mean().unstack()
    M2 = np.maximum(-1, (M - 3).fillna(0) / 2.)  # scale to -1..+1  (treat "0" scores as "1" scores)
    M2.head(2)
    
    AttributeError: 'numpy.ndarray' object has no attribute 'fillna'
    
  • emgf_co
    emgf_co about 2 years
    Yes. I used : y = [np.nan, np.nan, 2, 3, 4, 5, np.nan] y_temp = pd.DataFrame(y).fillna(1E-8)[0].values