Table of Contents
A ValueError is an error that occurs when trying to perform an operation on an array that is not in the correct format. In this case, the ValueError is occurring due to an attempt to mask a non-boolean array that contains NA or NaN values. To fix this error, the array must be converted to a boolean format using the “np.isnan” or “np.isna” functions. This will allow the array to be properly masked without causing a ValueError. Additionally, checking for and properly handling any NA or NaN values in the array before attempting to mask it can prevent this error from occurring.
Fix: ValueError: Cannot mask with non-boolean array containing NA / NaN values
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 19Now 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 13Once 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:
Cite this article
stats writer (2024). How can I fix a ValueError that occurs when trying to mask a non-boolean array containing NA or NaN values?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-a-valueerror-that-occurs-when-trying-to-mask-a-non-boolean-array-containing-na-or-nan-values/
stats writer. "How can I fix a ValueError that occurs when trying to mask a non-boolean array containing NA or NaN values?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-a-valueerror-that-occurs-when-trying-to-mask-a-non-boolean-array-containing-na-or-nan-values/.
stats writer. "How can I fix a ValueError that occurs when trying to mask a non-boolean array containing NA or NaN values?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-a-valueerror-that-occurs-when-trying-to-mask-a-non-boolean-array-containing-na-or-nan-values/.
stats writer (2024) 'How can I fix a ValueError that occurs when trying to mask a non-boolean array containing NA or NaN values?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-a-valueerror-that-occurs-when-trying-to-mask-a-non-boolean-array-containing-na-or-nan-values/.
[1] stats writer, "How can I fix a ValueError that occurs when trying to mask a non-boolean array containing NA or NaN values?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I fix a ValueError that occurs when trying to mask a non-boolean array containing NA or NaN values?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
