Open In App

Python – Boolean Array in NumPy

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, I’ll be explaining how to generate boolean arrays in NumPy and utilize them in your code.

In NumPy, boolean arrays are straightforward NumPy arrays with array components that are either “True” or “False.”

Note: 0 and None are considered False and everything else is considered True.

Examples:

Input: arr = [1, 0, 1, 0, 0, 1, 0]
Output: [True, False, True, False, False, True, False]
Explanation: 1 is considered as True and 0 is considered as False.

Input: arr = [5, None, 1, 25, -10, 0, ‘A’]
Output: [ True False  True  True  True False  True]

Method 1: Using dtype

Every ndarray has an associated data type (dtype) object. This data type object informs us about the layout of the array. This means it gives us information about:

  • Type of the data (integer, float, Python object, etc.)
  • Size of the data (number of bytes)
  • The byte order of the data (little-endian or big-endian)
  • If the data type is a sub-array, what is its shape and data type?

The values of a ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object.

Python3




import numpy as np
  
arr = np.array([1, 0, 1, 0, 0, 1, 0])
  
print(f'Original Array: {arr}')
  
bool_arr = np.array(arr, dtype='bool')
  
print(f'Boolean Array: {bool_arr}')


Output:

Original Array: [1 0 1 0 0 1 0]
Boolean Array: [ True False  True False False  True False]

When the elements are not 0 and 1:

Python3




import numpy as np
  
arr = np.array([5, None, 1, 25, -10, 0, 'A'])
  
print(f'Original Array: {arr}')
  
bool_arr = np.array(arr, dtype='bool')
  
print(f'Boolean Array: {bool_arr}')


Output:

Original Array: [5 None 1 25 -10 0 'A']
Boolean Array: [ True False  True  True  True False  True]

Method 2: Using astype

It is a method used to cast a pandas object to a specified dtype. The astype() function also provides the capability to convert any suitable existing column to a categorical type.

Syntax: DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)

Parameters:
dtype : Use a numpy.dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame’s columns to column-specific types.
copy : Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).

errors : Control raising of exceptions on invalid data for provided dtype.
raise : allow exceptions to be raised
ignore : suppress exceptions. On error return original object

kwargs :keyword arguments to pass on to the constructor

Returns: casted : type of caller

Python3




import numpy as np
  
arr = np.array([1, 0, 1, 0, 0, 1, 0])
  
print(f'Original Array: {arr}')
  
bool_arr = np.array(arr).astype(bool)
  
print(f'Boolean Array: {bool_arr}')


Output:

Original Array: [1 0 1 0 0 1 0]
Boolean Array: [ True False  True False False  True False]

When the elements are not 0 and 1:

Python3




import numpy as np
  
arr = np.array([5, None, 1, 25, -10, 0, 'A'])
  
print(f'Original Array: {arr}')
  
bool_arr = np.array(arr).astype(bool)
  
print(f'Boolean Array: {bool_arr}')


Output:

Original Array: [5 None 1 25 -10 0 'A']
Boolean Array: [ True False  True  True  True False  True]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads