# Python NumPy nan – Complete tutorial

In this Python tutorial, we will discuss Python NumPy nan with a few examples like below:

• Python numpy Nan to zero
• Python numpy create Nan array
• Python numpy Nan to none
• Python numpy Nan mean
• Python numpy Nan max
• Python numpy Nan min
• Python numpy Nan index
• Python numpy remove Nan from array
• Python numpy replace Nan with empty string
• Python numpy Nan average
• Python numpy Nan equal
• Python numpy Nan compare

## Python numpy nan

• In this section, we will discuss Python numpy nan
• To check for NaN values in a Python Numpy array you can use the np.isnan() method.
• NaN stands for Not a Number. NaN is used to representing entries that are undefined. It is also used for representing missing NAN values in a given array.
• This method is a special floating-point value that cannot be converted to any other type than float.
• The important point I would like you to take along from this is that all of our integers values have been converted to floats, and that’s because the NumPy module has defined the NaN float data type
• It is defined in a numpy array with special values like nan,inf.
• Numpy uses the IEEE standard for floating-point for arithmetic. This means that nan is not equivalent to infinity value.

Syntax:

Here is the Syntax of numpy.isnan

``````numpy.isnan
(
x,
out=None,
where=True,
casting='same_kind',
order='k',
dtype=None
)
``````
• It consists of few parameters
• X: Input array
• Out: A location into which the result is stored. If provided, it should have a shape that the inputs provided. If not provided or None value, is freshly-allocated array is returned.
• Where: It is an optional parameter. This condition is broadcast over the input. At a position where all the condition is True, the out parameter will show in the array will be set to the function result. Elsewhere, the out numpy array will obtain its original value.
• Returns: It always returns a boolean expression of the size that of the original array.

Example:

``````import numpy as np
arr = np.array([2, np.nan, 6, 8, 9, 10, np.nan])
b = np.isnan(arr)
print(b) ``````
• In the above code, we will import a numpy library and create an array by using the function np. array. Now we will create a variable and store the values in the isnan method.
• The output array has a boolean mask true for the indices which are not a number in the original array and false for the rest.

Here is the Screenshot of the following given code

## How to check the nan values in numpy array (another method)

• Not a number(nan) implemented the standard key is the only value for which the inequality compare with itself should return True otherwise false.
• In this method we use the function is_nan and for a loop.
• If it is a number the comparison should succeed and return the result in the form of a boolean mask.

Example:

``````def is_nan(x):
return (x != x)
import numpy as np
values = [float('nan'), np.nan, 55, "string", lambda x : x]
for value in values:
print(f"{repr(value):<8} : {is_nan(value)}")``````

Here is the Screenshot of the following given code

## Python numpy nan to zero

• In this method, we can easily use the function numpy.nan_to_num.
• Replacing NaN values with zeros in an array converts every Nan value to zero.
• We can easily use the np.nan_to_num method to convert numpy nan to zero.
• nan_to_num() function is used if we want to convert nan values with zero. It always returns positive infinity with the biggest number and negative infinity with the very smallest number.

Syntax:

``````numpy.nan_to_num
(
x,
copy=True,
nan=0.0,
posinf=None,
neginf=None
)``````
• It consists of few parameters
• X: input data
• Copy: It is an optional parameter. Whether to create a copy of x or to exchange values in place. The in-place function only occurs if cast to an array that does not require the same array. The Default argument is True.
• Nan: Value to be used to fill Not a number values. If none of the values is passed through an array then NaN values will be replaced with 0.0.
• posinf: It is used to fill positive infinity values. If no value is passed then positive values will be exchanged with a large number.

Example:

``````import numpy as np
arr = np.array([2, np.nan, np.nan, np.nan, np.nan, 10, np.nan])
b = np.nan_to_num(arr)
print(b) ``````
• In the above code, we will import a numpy library and create an array by using the function numpy.array. Now create a variable and contains the values in the nan_to_num function.
• The output array will be displayed in the form of zero’s values and posinf values.

Here is the Screenshot of the following given code

## Another method to convert nan values with zero’s in numpy array

• In this method, the function isnan produces a boolean array, and it indicates where the NaN values are. A boolean array can always be used to index.
• We could use np.where to find an index value where you have Nan values.
• We can easily use the function np.where and np.nan to convert nan values with zero’s.

Example:

``````import numpy as np
arr = np.array([[2, np.nan, np.nan, np.nan, np.nan, 10, np.nan],
[3, np.nan, np.nan, np.nan, np.nan, 7, np.nan ],
[4, np.nan, np.nan, np.nan, np.nan, 6, np.nan]])
b = np.where(np.isnan(arr),0,arr)
print(b) ``````

Here is the Screenshot of the following given code

## Python numpy create nan array

• In this section, we will discuss Python numpy create nan array.
• To create an array with nan values we have to use the numpy.empty() and fill() function.
• It returns an array with the same shape and type as a given array.
• Use np. empty((x,y)) to create an uninitialized numpy array with x rows and y columns. Then, we have to assign NaN values in the array.

Example:

``````import numpy as np

arr = np.empty((5,5))
arr[:] = np.NaN
print(arr)``````
• In the above code, we will import a numpy library and we can create an uninitialized array and assign it to all entries at once.
• In this example we can easily use the function numpy.empty and np.nan to create the nan values in the array.

Here is the Screenshot of the following given code

## Another method to check how to create numpy nan array

• In this example, we can easily use the functions numpy.nan and numpy.ones. To declare an initialized numpy array with x rows and y columns.
• We use the multiplication method to get the nan values in an array.

Example:

Let’s take an example to check how to create a NumPy nan array

``````import numpy as np

b= np.nan * np.ones(shape=(3,2))
print(b)``````

Here is the Screenshot of the following given code

## Python numpy nan to none

• In this section, we will discuss Python numpy nan to none.
• Not a number(Nan) can be used as a numerical value on mathematical statistics operation, while None value can’t at least shouldn’t contain the value. NaN is a numeric floating value, as defined in IEEE 754 floating-point standard. None is an internal Python type and would be more like inexistence than numerically invalid.
• In this example, we can use the functions numpy.fill() and numpy.nan(). if we set a value in an integer array to np.nan, it will automatically be converted into none value.

Example:

Let’s take an example to check numpy nan to none

``````import numpy as np

A = np.array([2,3,np.nan,np.nan,np.nan])
b = A.fill(np.nan)
print(b)``````

Here is the Screenshot of the following given code

## Python numpy nanmean

• In this section, we will discuss Python numpy nanmean.
• In this example, we can use the function numpy.nanmean()
• This function can be used to calculate the mean of the array. If the array has a NaN value and we can find out the mean without the effect of NaN value.
• It will return the average of the array elements.

Syntax:

Here is the Syntax of numpy can mean

``````numpy.nanmean
(
arr,
axis=None,
dtype=None,
out=None,
)``````

Example:

``````import numpy as np

A = np.array([2,3,4,np.nan,np.nan])
b = np.nanmean(A)
print(b)``````

Here is the Screenshot of the following given code

## Python numpy nanmax

• In this section, we will discuss Python numpy nanmax.
• In this example, we can use the function numpy. nanmax().
• This function is used to returns the maximum value of an array or along any specifically mentioned axis of the array.
• An array with the same shape as arr, with the specific axis, is removed. If a is a 0-dimension numpy array, or if the axis is None, a numpy dimension array scalar is returned.
• The max value of a numpy array along a given axis, any NaNs.

Syntax:

Here is the Syntax of np. nanmax()

``````numpy.nanmax
(
arr,
axis=None,
out=None
)``````

Example:

``````import numpy as np

A = np.array([8,4,6,np.nan,np.nan])
b = np.nanmax(A)
print("max of arr : ", np.amax(A))
print(b)``````

Here is the Screenshot of the following given code

## Python numpy nanmin

• In this section, we will discuss Python numpy nanmin.
• In this example, we can easily use the function np.nanmin().
• This function is used when to returns the minimum value of an array or along any specifically mentioned axis of the array.
• The min value of an array along a given axis, providing any NaNs value.

Syntax:

Here is the Syntax of numpy.nanmin()

``````numpy.nanmin
(
arr,
axis=None,
out=None
)``````

Example:

``````import numpy as np

A = np.array([7,2,6,np.nan,np.nan])
b = np.nanmin(A)
print("min of arr : ", np.amin(A))
print(b)``````
• In the above code first, we will create a numpy library and create a function using np.array function, and assign the nan and non-nan values in the arguments.
• Now we can easily use the function nanmin() to Return the minimum value of an array or minimum along an axis.

Here is the Screenshot of the following given code

## Python numpy nan index

• In this section, we will discuss Python numpy nan index.
• In this example, we can easily use the function np.argmin to get the index of nan values.
• It will always return the indices of the min values in the specified axis ignoring NaN value

Syntax:

Here is the Syntax of numpy.argmin

``````numpy.nanargmin
(
arr,
axis=None
)``````
• It consists of few parameters
• arr: Input data
• axis: It’s an optional parameter. Axis along which to operate. By default flattened input is used.
• Returns: An array of the single index values.

Example:

``````import numpy as np

A = np.array([7,2,6,np.nan])
b = np.argmin(A)

print(b)``````

Here is the Screenshot of the following given code

## Python numpy remove nan from array

• In this section, we will discuss Python numpy remove nan from the array.
• In this method, we can use the functions logical_not() and isnan() to delete nan values from a given array.
• Logical_not() is used to implement logical Not to elements of a numpy array. isnan() method is a boolean function that checks whether an element is a nan value or not.
• So, in the last, we get index value for all the elements which are not nan.

Example:

``````import numpy as np

arr = np.array([4,2,6,np.nan,np.nan,8,np.nan])
b = arr[np.logical_not(np.isnan(arr))]

print(b)``````

Here is the Screenshot of the following given code

## Another method to remove nan values from array

• To delete nan values from the numpy array in Python, we can easily use the function isfinite().
• isfinite() method is a boolean function that checks whether a value is finite or not.

Example:

``````import numpy as np

arr = np.array([3,4,6,np.nan,np.nan,9,np.nan])
b = arr[np.isfinite(arr)]

print(b)``````

Here is the Screenshot of the following given code

## Operator method to remove nan values from array

• In this example, we can join ~ operator with np.isnan() function.
• If the dimension of the numpy array is 2dimension, it will convert into a 1dimension array.

Example:

``````import numpy as np

arr = np.array([[3,4,6,np.nan,np.nan,9,np.nan],
[2,5,6,np.nan,np.nan,3,np.nan]])
b = arr[~(np.isnan(arr))]

print(b)``````

Here is the Screenshot of the following given code

## Python numpy replace nan with empty string

• In this section, we will discuss Python numpy replace Nan with empty string.
• In this example, we can use the function replace() to convert the Nan value with an empty string.
• In this method, we can also use the panda’s module to exchange the Nan value with the empty string

Example:

``````import numpy as np
import pandas as pd
df = pd.DataFrame({
'A': ['a', 'b', 'c'],
'B': [np.nan, 1, np.nan]})
df1 = df.replace(np.nan, '', regex=True)
print(df1)``````

Here is the Screenshot of the following given code

## Python numpy nan average

• In this section, we will discuss Python numpy nan average.
• In this example, it will originate the arithmetic mean along the given axis, ignoring NaNs value. it will return the average of the array elements.
• The average is taken over the input array by default, otherwise over the given axis.
• In this example to check the numpy nan average, we can use the function np.nanmean().
• It will return the mean of the array values

Syntax:

``````numpy.nanmean
(
arr,
axis=None,
dtype=None,
out=None,
)``````

Example:

``````import numpy as np

C = np.array([4,5,6,np.nan,np.nan,8,9,5])
b = np.nanmean(C)
print(b)``````

Here is the Screenshot of the following given code

## Python numpy nan equal

• In this method, we will discuss numpy nan equal.
• In this method, we can use numpy. testing. assert equal with a try-except block.
• This method equal() indicates that it consider that Nan values are equal or not.
• It will return the result in the form of a boolean mask.
• If the nan values are not the same it will always return false otherwise true.

Example:

``````import numpy as np

def nan_equal(a,b):
try:
np.testing.assert_equal(a,b)
except AssertionError:
return False
return True

a=np.array([3, 2, np.NaN])
b=np.array([3, 2, np.NaN])
c= nan_equal(a,b)
print(c)``````

Here is the Screenshot of the following given code

## Python numpy nan compare

• In this section, we will discuss Python numpy nan compare.
• To check for NaN values in an array you can use the numpy. isnan() method. This result will display a boolean mask of the size that of the original array.
•  The output given array has true for the indices values which are NaNs in the originally given array and false for the rest of the values.

Example:

``````def isNaN(num):
return num!= num
x=float("nan")
b =isNaN(x)
print(b)``````

Here is the Screenshot of the following given code

You may like the following Python tutorials:

In this Python tutorial, we will discuss Python NumPy nan with a few examples like below:

• Python numpy Nan to zero
• Python numpy create Nan array
• Python numpy Nan to none
• Python numpy Nan mean
• Python numpy Nan max
• Python numpy Nan min
• Python numpy Nan index
• Python numpy remove Nan from array
• Python numpy replace Nan with empty string
• Python numpy Nan average
• Python numpy Nan equal
• Python numpy Nan compare