# 5 Best Ways to Return the Maximum of an Array with Positive Infinity or Maximum Ignoring Any NaNs in Python

Rate this post

π‘ Problem Formulation: Dealing with arrays in Python can be tricky when they contain `NaN` (Not a Number) values or infinite values. The goal is to find the maximum value of an array that also might contain `numpy.inf` (positive infinity) and `NaN` elements. Proper handling of these special cases is essential. For instance, given an input array `[3, NaN, 5, inf, 7]`, the desired output is `inf` or `7`, depending on whether we include infinity or not.

## Method 1: Use NumPy’s nanmax Function

Numpy’s `nanmax` function returns the maximum value within an array while ignoring all `NaN` values. This is the perfect tool for cases where the inclusion of infinite values is acceptable. It’s efficient and widely used in numerical computations with Python.

Here’s an example:

```import numpy as np

arr = np.array([3, np.nan, 5, np.inf, 7])
max_value = np.nanmax(arr)```

Output:

`inf`

This code snippet first imports the `numpy` library under the alias `np` and creates an array containing positive infinity and `NaN` values. It then applies `numpy.nanmax` to find the maximum value while ignoring `NaN`s, returning positive infinity in this case.

## Method 2: Combine max with a Filtered Comprehension

For those avoiding NumPy, one can use a list comprehension to filter out `NaN` values combined with Python’s built-in `max` function to find the maximum finite number. This method will not return infinity as part of the maximum.

Here’s an example:

```import math

arr = [3, math.nan, 5, math.inf, 7]
finite_max = max(x for x in arr if not math.isnan(x) and x != math.inf)```

Output:

`7`

We use the `math` module to identify `NaN` and infinite values. The line of code with `max` goes through a filtered array comprehension that excludes `NaN` and `inf` values, finding the maximal finite number.

## Method 3: Use pandas Series max

Pandas Series objects provide a high-level interface that can handle `NaN` values and infinity when calculating the maximum. This can be particularly useful when working within the pandas framework for data analysis.

Here’s an example:

```import pandas as pd

s = pd.Series([3, np.nan, 5, np.inf, 7])
max_value = s.max()```

Output:

`inf`

After importing pandas and defining a Series with our array, the `max` method on the Series object is called. It correctly returns `inf`, as pandas, by default, treats infinity as a valid value for max computations.

## Method 4: Using max with pandas and Argument skipna

Adjusting behavior with pandas Series, one can set `skipna=True` to ignore `NaN` values while using `max`. For added control, one could first replace infinity with `NaN` and then calculate the max, which would only focus on finite numbers.

Here’s an example:

```import pandas as pd

s = pd.Series([3, np.nan, 5, np.inf, 7])
finite_max = s.replace(np.inf, np.nan).max()```

Output:

`7`

This approach builds upon the pandas library’s features for handling missing or infinite data. By replacing `inf` values with `NaN` temporarily, applying `max` will only return the maximum finite value thanks to `skipna=True` by default.

## Bonus One-Liner Method 5: Using Filter and max Function

When working with pure Python and you’re looking for a simple one-liner, combine `filter` with `max` to exclude `NaN` and infinity. Caution is advised though since this approach only works well with finite numbers.

Here’s an example:

```import math

arr = [3, math.nan, 5, math.inf, 7]
finite_max = max(filter(lambda x: x != math.inf and not math.isnan(x), arr))```

Output:

`7`

This concise snippet applies a lambda function to filter out `NaN` and infinity before determining the maximum with Python’s built-in `max` function.

## Summary/Discussion

• Method 1: NumPy’s nanmax. Efficient with arrays; includes infinity. It’s the best fit for numerical computations within the NumPy environment.
• Method 2: Max with Filtered Comprehension. Pure Python; excludes infinity. It’s simple and does not require additional libraries, though potentially less efficient than NumPy on large data sets.
• Method 3: Pandas Series max. Handles NaNs and infinity well; integrates smoothly into pandas workflows. It is particularly good for data tables and pre-existing pandas data structures.
• Method 4: Pandas max with skipna. Offers control over how NaNs and infinity are treated prior to finding max. It’s versatile within pandas, allowing for precise handling of special cases.
• Method 5: Filter and max Function. Pure Python one-liner; excludes infinity. Best for quick computations where a library like NumPy or pandas is not desired or needed.