How to Check if Numpy Array is Empty or Not in Python

Check if Numpy Array is Empty or Not in Python

Sometime there an empty numpy error occurs when you execute your code when there is no elements in the series or array. It can be removed if code is executed after you check if numpy array is empty or not. If you are also one of them who are looking for its solution then this post is for you.

In this entire tutorial I will show you two methods for checking if numpy array is empty or not.

 

Step by Steps to check if numpy array is empty or not

Step 1: Import all the required libraries.

The first step is to import all the necessary libraries. In our example we are using only numpy array. Lets import it using the import statement.

import numpy as np

Step 2: Create an Empty and Non- Empty Array

Lets create both an empty and non-empty array for single dimension and multi-dimensional array. The method for the array creation is np.array().

Single Dimensional Numpy Array

Empty Array

array2 = np.array([])

Non-Empty Array

array1 = np.array([1,2,3,4,5])

Multi-Dimensional Numpy Array

Non-Empty Array

array_2d = np.arange(0,10).reshape(5,2)

Step 3: Follow the methods to check emtpy numpy array

Method 1: Checking empty or not using the .size operator

The template for this method is array_name.size. It  returns size of the array that is total number of elements in the array.

Eexecute the code below for checking non-empty or empty array.

Checking 1D Non-Empty Array

import numpy as np
array1 = np.array([1,2,3,4,5])
array1.size

Output

Checking 1D Non-Empty Array
Checking 1D Non-Empty Array

Checking 1D Empty Array

import numpy as np
array2 = np.array([])
array2.size

Output

Checking 1D Empty Array
Checking 1D Empty Array

You can see in the output figure, empty numpy array will return size 0 and other array will return its size that is greater than 0.

The above case was for single dimension. The same method applies for two-dimensional array.

Run the below code to look for empty array in two dimensional array.

import numpy as np
array_2d = np.arange(0,10).reshape(5,2)
array_2d.size

Output

Checking 2D Non-Empty Array
Checking 2D Non-Empty Array

The output will return the number of elements present in the array. In this case it is 10.

In the next section We will learn the second method to check numpy array is emtpy or not.

Method 2: Checking empty or not using the len() function.

Here I will first change the numpy array to list using typecasting. And then using the len() function, I will check what is the lenght of the list.

Checking 1D Non-Empty Array

import numpy as np
array1 = np.array([1,2,3,4,5])
array_list1 = list(array1)
len(array_list1)

Output

Checking 1D Non-Empty Array using len() method
Checking 1D Non-Empty Array using len() method

Checking 1D Empty Array

import numpy as np
array2 = np.array([])
array_list2 = list(array2)
len(array_list2)

Output

Checking 1D Empty Array using len() function
Checking 1D Empty Array using len() function

In the same way We can check non-emptiness of the numpy array for two dimensioanl array.

import numpy as np
array_2d = np.arange(0,10).reshape(5,2)
array_2d_list = list(array_2d)
len(array_2d_list)

Output

Checking 2D Non-Empty Array using len() method
Checking 2D Non-Empty Array using len() method

Using these methods you can easily check if numpy array is empty or not. If the length of the list is greater than 0 , then the numpy array is not empty else it is empty.

Thats all for now. These are methods for checking emptiness of numpy array.

Hope you have enjoyed this article. If you have any query regaring it then you can contact us.

 

Source:

Offical Numpy Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner