Python:AttributeError ‘numpy.ndarray’ object has no attribute ‘fillna’

Published on:
Last updated:

This post is also available in: Japanese

This is an error often encountered while doing data analysis such as machine learning using Python,Numpy,Pandas.
I often forget, so I am writing this post as a memo.

AttributeError: 'numpy.ndarray' object has no attribute 'fillna'

The following is a sample when you want to do replace missing values(Nan) with previous values.
If you want to replace missing values(NaN) with next values, use method=’bfill’.

I think it is more easy to use ‘fillna’ in order to keep the number of data with train_y unchanged.
However, since it affects the results of data analysis, you need to pay attention to the data to be replaced.

# Replace missing values with previous values
train_X = train_df.fillna(method='ffill').values

The following is a sample when you want to delete a column with missing values.

# Delete a column with missing values
train_X = train_df.drop(train_df.columns[np.isnan(train_df).any()], axis=1).values
No tags for this post.

Kuniyoshi Takemoto is the founder of LLC, and editor of this blog( more and connect with me on Google+,LinkedIn(English).