- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Return the maximum of an array or maximum ignoring any NaNs in Python

To return the maximum of an array or maximum ignoring any NaNs, use the numpy.nanmax() method in Python. The method returns an array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, an ndarray scalar is returned. The same dtype as a is returned. The 1st parameter, a is an array containing numbers whose maximum is desired. If a is not an array, a conversion is attempted.

The 2nd parameter, axis is an axis or axes along which the maximum is computed. The default is to compute the maximum of the flattened array. The 3rd parameter, out ia an alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary.

The 4th parameter, keepdims If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a. If the value is anything but the default, then keepdims will be passed through to the max method of sub-classes of ndarray. If the sub-classes methods does not implement keepdims any exceptions will be raised. The 5th parameter is the minimum value of an output element. Must be present to allow computation on empty slice

## Steps

At first, import the required libraries −

import numpy as np

Creating a numpy array using the array() method. We have added elements of int type with nan −

arr = np.array([[10, 20, 30], [40, np.nan, 60]])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

To return the maximum of an array or maximum ignoring any NaNs, use the numpy.nanmax() method in Python. The method returns an array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, an ndarray scalar is returned. The same dtype as a is returned −

print("\nResult (nanmax)...\n",np.nanmax(arr))

## Example

import numpy as np # Creating a numpy array using the array() method # We have added elements of int type with nan arr = np.array([[10, 20, 30], [40, np.nan, 60]]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # To return the maximum of an array or maximum ignoring any NaNs, use the numpy.nanmax() method in Python print("\nResult (nanmax)...\n",np.nanmax(arr))

## Output

Our Array... [[10. 20. 30.] [40. nan 60.]] Dimensions of our Array... 2 Datatype of our Array object... float64 Result (nanmax)... 60.0

- Related Articles
- Return the maximum of an array along an axis or maximum ignoring any NaNs in Python
- Return the maximum of an array with negative infinity or maximum ignoring any NaNs in Python
- Return the maximum of an array along axis 1 or maximum ignoring any NaNs in Python
- Return the maximum of an array with positive infinity or maximum ignoring any NaNs in Python
- Return the maximum of an array along axis 0 or maximum ignoring any NaNs in Python
- Return the minimum of an array or minimum ignoring any NaNs in Python
- Return the minimum of an array along an axis or minimum ignoring any NaNs in Python
- Return the minimum of an array along axis 0 or minimum ignoring any NaNs in Python
- Return the minimum of an array along axis 1 or minimum ignoring any NaNs in Python
- Return the minimum of an array with positive infinity or minimum ignoring any NaNs in Python
- Return the minimum of an array with negative infinity or minimum ignoring any NaNs in Python
- Compare two arrays and return the element-wise maximum ignoring NaNs in Numpy
- Compare two Numpy arrays and return the element-wise minimum ignoring NaNs
- Return the cumulative sum of array elements treating NaNs as zero in Python
- Return the cumulative product of array elements treating NaNs as one in Python