Fixed: cannot mask with non-boolean array containing na / nan values

If you are getting an error like cannot mask with non-boolean array containing na / nan values, then there is a way to resolve or fix this error.

Before that, it’s better to understand when and why we are getting this error.

When do we get this error

While working with Pandas in Python we can face an error like this:

ValueError: Cannot mask with non-boolean array containing NA / NaN values


This error often pops up when you’re trying to locate rows in a pandas DataFrame based on a specific string, but the catch is, the column you’re searching through contains NaN (Not a Number) values. It’s like trying to find a word in a book, but some of the pages are blank.

If you run the below code, you will better understand when we are getting this error:

import pandas as pd
import numpy as np

# Initialize a new DataFrame
data_frame = pd.DataFrame({'team_label': ['Team1', 'Team1', 'Team1', 'Team2', 'Team2'],
                           'role': ['Defender', 'Defender', np.nan, 'Defender', 'Striker'],
                           'score': [22, 28, 14, 13, 19]})

# Display the DataFrame
print(data_frame)

# Select and display rows where 'role' column is 'Defender'
filtered_data = data_frame[data_frame['role'].str.contains('Defender')]
print(filtered_data)

Output:

How to fix cannot mask with non-boolean array containing na / nan values

I have just added na=False in line number 13.

Take a look:

filtered_data = data_frame[data_frame['role'].str.contains('Defender', na=False)]

This will fix the error.

There are other ways to solve this too, but in my opinion this is the best choice.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top