17.6 C
London
Monday, July 21, 2025
HomePythonFix Common Errors in PythonHow to Fix: ValueError: Cannot mask with non-boolean array containing NA /...

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

One error you may encounter when using pandas is:

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

This error usually occurs when you attempt to find the rows in a pandas DataFrame that contain a particular string, but the column you’re searching in has NaN values.

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we have the following pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B'],
                   'position': ['Guard', 'Guard', np.nan, 'Guard', 'Forward'],
                   'points': [22, 28, 14, 13, 19]})

#view DataFrame
print(df)

  team position  points
0    A    Guard      22
1    A    Guard      28
2    A      NaN      14
3    B    Guard      13
4    B  Forward      19

Now suppose we attempt to access all rows in the DataFrame where the position column contains the string “Guard”:

#access all rows where position column contains 'Guard'
df[df['position'].str.contains('Guard')]

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

We receive an error because there is a NaN value in the position column.

How to Fix the Error

To avoid this error, we simply need to use the argument na=False within the str.contains() function:

#access all rows where position column contains 'Guard', ignore NaN
df[df['position'].str.contains('Guard', na=False)]

        team	position  points
0	A	Guard	  22
1	A	Guard	  28
3	B	Guard	  13

This time we’re able to access all rows that contain “Guard” in the position column without any errors.

Another way to avoid this error is to use .fillna(False) as follows:

#access all rows where position column contains 'Guard', ignore NaN
df[df['position'].str.contains('Guard').fillna(False)]

        team	position  points
0	A	Guard	  22
1	A	Guard	  28
3	B	Guard	  13

Once again we’re able to access all rows that contain “Guard” in the position column without any errors.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories