Many numpy operations are not defined for an empty array, and if they are found while operating on it, it will throw unexpected errors, and to prevent it, we need validation. Our code must be robust to handle almost all types of scenarios that ensure data integrity.
Here are four ways to check whether a NumPy array is empty:
- Using numpy.ndarray.size attribute (Efficient way)
- Using numpy.size()
- Using .shape attribute
- Using numpy.any() (Not recommended)
Method 1: Using numpy.ndarray.size
The numpy.ndarray.size is an array object attribute, making it more concise. It returns the total number of elements in the array. If the size is 0, an array is empty; otherwise, it is not.
Checking for 1D empty array
import numpy as np empty_array = np.array([]) if empty_array.size == 0: print("Array is empty") else: print("Array is not empty")
Output
Array is empty
Checking for 2D empty array
You can create an empty 2D array using either np.empty() function or np.array([[]]). For checking its emptiness, we can operate the .size attribute on it.
import numpy as np # Empty 2D array empty_2d_array = np.empty((0, 5)) # 0 rows, 5 columns print(f"Empty 2D array:\n{empty_2d_array}") if empty_2d_array.size == 0: print("The 2D array is empty (using .size)") # Another way to create an empty 2D array empty_2d_array_2 = np.array([[]]) print(f"\nAnother empty 2D array:\n{empty_2d_array_2}") if empty_2d_array_2.size == 0: print("The 2D array is empty (using .size)") # Non-empty 2D array non_empty_2d_array = np.array([[1, 2, 3], [4, 5, 6]]) print(f"\nNon-empty 2D array:\n{non_empty_2d_array}") if non_empty_2d_array.size == 0: print("The 2D array is empty (using .size)") else: print("The 2D array is NOT empty (using .size)")
Output
Empty 2D array: [] The 2D array is empty (using .size) Another empty 2D array: [] The 2D array is empty (using .size) Non-empty 2D array: [[1 2 3] [4 5 6]] The 2D array is NOT empty (using .size)
Checking for multi-dimensional empty array
In this code example, we will create 3D, 4D, and 5D arrays using the np.array() function and then operate the np.ndarray.size attribute on it to check for its emptiness.
import numpy as np # Empty 3D array empty_3d_array = np.empty((0, 2, 3)) # 0 "layers", 2 rows, 3 columns print("Empty 3D array:\n", empty_3d_array) if empty_3d_array.size == 0: print("The 3D array is empty (using .size)") # Another empty 3D array empty_3d_array_two = np.array([[[]]]) print("\nAnother empty 3D array:\n", empty_3d_array_two) if empty_3d_array_two.size == 0: print("The 3D array is empty (using .size)") # Non-empty 3D array non_empty_3d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print("\nNon-empty 3D array:\n", non_empty_3d_array) if non_empty_3d_array.size == 0: print("The 3D array is empty (using .size)") else: print("The 3D array is NOT empty (using .size)") # 3D array with all zeros (NOT empty) zeros_3d_array = np.zeros((2, 2, 2)) print("\n3D array with zeros:\n", zeros_3d_array) if zeros_3d_array.size == 0: print("The 3D array is empty (using .size)") else: print("The 3D array is NOT empty (using .size)") # Example with higher dimensions (4D) empty_4d_array = np.empty((0, 2, 3, 4)) print("\nEmpty 4D array:\n", empty_4d_array) if empty_4d_array.size == 0: print("The 4D array is empty (using .size)") # Example with higher dimensions (4D) using np.array empty_4d_array_two = np.array([[[[]]]]) print("\nAnother empty 4D array:\n", empty_4d_array_two) if empty_4d_array_two.size == 0: print("The 4D array is empty (using .size)") # Example with higher dimensions (5D) empty_5d_array = np.empty((0, 1, 2, 3, 4)) print("\nEmpty 5D array:\n", empty_5d_array) if empty_5d_array.size == 0: print("The 5D array is empty (using .size)") # Example with higher dimensions (5D) using np.array empty_5d_array_two = np.array([[[[[[]]]]]]) print("\nAnother empty 5D array:\n", empty_5d_array_two) if empty_5d_array_two.size == 0: print("The 5D array is empty (using .size)")
Output
Empty 3D array: [] The 3D array is empty (using .size) Another empty 3D array: [] The 3D array is empty (using .size) Non-empty 3D array: [[[1 2] [3 4]] [[5 6] [7 8]]] The 3D array is NOT empty (using .size) 3D array with zeros: [[[0. 0.] [0. 0.]] [[0. 0.] [0. 0.]]] The 3D array is NOT empty (using .size) Empty 4D array: [] The 4D array is empty (using .size) Another empty 4D array: [] The 4D array is empty (using .size) Empty 5D array: [] The 5D array is empty (using .size) Another empty 5D array: [] The 5D array is empty (using .size)
Pros
- It is extremely efficient and the least complex way.
- It works with 1D, 2D, 3D, or any dimensional array.
I would highly recommend using this approach. It takes less time to identify if the array is empty.
Method 2: Using numpy.size()
The numpy.size() is a simple method that returns the total number of elements in the given array. If the array is empty, it returns zero, which means the size is 0. If not, it will return the size of the array.
import numpy as np empty_array = np.array([]) is_empty_array = np.size(empty_array) print(np.size(empty_array)) if is_empty_array == 0: print("Array is empty") else: print("Array is not empty")
Output
0 Array is empty
Pros
- The np.size() is a straightforward and easy-to-understand method.
- It works for arrays of any dimension.
Cons
- It is less concise compared to np.ndarray.size attribute because it directly operates on the array.
Method 3: Using shape attribute
The .shape attribute returns a tuple representing the dimensions of the array. An empty array will have a shape with a dimension size of zero.
import numpy as np empty_array = np.array([]) if empty_array.shape[0] == 0: print("Array is empty") else: print("Array is not empty")
Output
Array is empty
Pros
- You can not only check the emptiness but also check the dimensional details.
Cons
- It requires accessing an element of the shape tuple.
Method 4: Using numpy.any()
The np.any() method is not a direct way to check if an array is empty, but you can use it to check whether any item in an array is along a given axis. If it returns False, the array is empty; otherwise, it is not.
import numpy as np # Create an empty array empty_array = np.array([]) print(empty_array) is_empty_array = np.any(empty_array) if is_empty_array == False: print("Array is empty") else: print("Array is not empty")
Output
[] Array is empty
Pros
- It provides easy and readable syntax.
Cons
- It is the least recommended way because its intention is not to check for emptiness.
- It can give us the least recommended way to check for emptiness because it can give false positives for arrays containing only zeros.
That’s all!