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

jeangelj picture jeangelj · Jun 4, 2016 · Viewed 33.9k times · Source

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'

Answer

piRSquared picture piRSquared · Jun 4, 2016

(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)