• Post author:
  • Post category:Pandas
  • Post last modified:May 22, 2024
  • Reading time:14 mins read
You are currently viewing Pandas Series.max() Function

The max() function in Pandas Series is used to find the maximum value within a Series. It returns the highest value present in the Series. It returns a float value representing the maximum value within the series. This function always returns a Series even if only one value is present.

Advertisements

In this article, I will explain the syntax of Series.max() function, its parameters, and how to find the maximum value of a given Series object.

Key Points –

  • Pandas Series.max() efficiently computes the maximum value within a Series object.
  • It offers flexibility in handling missing values, providing options to include or exclude them from the computation.
  • Pandas Series.max() function returns the maximum value in a Series.
  • The function is efficient for large datasets due to its optimized implementation.
  • It supports various data types including numeric and datetime.

Syntax of Series.max() Method

Following is the syntax for creating Series.max() method.


# Syntax of Series.max() function
Series.max(axis=_NoDefault.no_default, skipna=True, level=None, numeric_only=None, **kwargs)

Following are the parameters of the max().

  • axis – {index (0), columns (1)}. Axis along which the operation is performed.
  • skipna – bool, default True. Exclude NA/null values when computing the result.
  • level –  If the axis is a MultiIndex, then the level specifies the level to compute the maximum along. Default is None.
  • numeric_only – bool, default None. Include only float, int, boolean data. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
  • **kwargs – additional keyword arguments are passed to the underlying function like numpy.amax() or numpy.ndarray.max(), depending on the dtype of the Series.

Use Series.max() Function

Series.max() function is used to get the max of the values in pandas. This function gets the maximum value of the given object elements in Pandas.

A Pandas Series is a one-dimensional, index-labeled data structure provided by the Pandas library. It’s capable of storing various data types including strings, integers, floats, and other Python objects. Each element in the Series can be accessed using its corresponding default indices.

Now, let’s create pandas series using a list of values and use the max() function.


import pandas as pd
import numpy as np

# Create a Series
ser = pd.Series([25, 44, 66, 30, 12, 55, 20])

# Use Series.max() function
ser2 = ser.max()
print(ser2)

# Output:
# 66

Pandas Series Max Ignore NaN

By default, the max() function in Pandas Series uses skipna=True, meaning it automatically excludes NaN (Not a Number) values when calculating the maximum value. This ensures that if a series contains NaN values, they are automatically ignored, and the maximum value is computed based on the non-NaN elements of the series.


# Pandas series max ignore nan 
ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
ser2 = ser.max(skipna = True)
print(ser2)

# Output:
# 94.6

In the above example, the max() function automatically ignores NaN values and computes the maximum among the non-NaN values in the series, resulting in the output 94.6.

Similarly, when using the max() function in Pandas Series, setting skipna=False ensures that NaN values are not ignored. In this case, if the series contains NaN values, the result will be NaN as well. It’s important to note that NaN represents a missing value in Python, and for more examples on NaN refer to handling missing values in Pandas.


# Pandas series max ignore nan use skipna = False
ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
ser2 = ser.max(skipna = False)
print(ser2)

# Output:
# nan

You can also follow the below program to get the maximum value in the pandas Series object. Here, I am using the multiindex.


import pandas as pd
import numpy as np

inx = pd.MultiIndex.from_arrays([
    ['Bigdata','Bigdata','ProgramLanguage','ProgramLanguage'],
    ['Spark','PySpark','Java','JavaScript']],
    names=['Technology', 'Course'])

ser = pd.Series([2000, 3500, 2500, 1000], name='Courses', index=inx)
print(ser)

# Output:
# Technology       Course    
# Bigdata          Spark         2000
#                  PySpark       3500
# ProgramLanguage  Java          2500
#                  JavaScript    1000
# Name: Courses, dtype: int64

# Use series.max() function
ser2 = ser.max()
print(ser2)

# Output:
# 3500

Complete Example of Series.max() Method


import pandas as pd
import numpy as np

# Create a Series
ser = pd.Series([25, 44, 66, 30, 12, 55, 20])
print(ser)

# Use Series.max() function
ser2 = ser.max()
print(ser2)

# Pandas series max ignore nan 
ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
ser2 = ser.max(skipna = True)
print(ser2)

# Pandas series max ignore nan use skipna = False
ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
ser2 = ser.max(skipna = False)
print(ser2)

FAQ on Series.max() Method

What does the Pandas Series.max() function do?

The Pandas Series.max() function is used to find the maximum value within a Pandas Series. It returns the highest value present in the Series. By default, it ignores any NaN (missing) values in the Series while computing the maximum value.

How does Pandas handle NaN values with Series.max()?

Pandas handles NaN (Not a Number) values with the Series.max() function by defaulting to skipping them during the computation of the maximum value. This means that NaN values are ignored, and only the non-NaN values in the Series are considered when determining the maximum value. If all values in the Series are NaN or if NaN values are the maximum, the result of Series.max() will be NaN. However, you can override this behavior by setting the skipna parameter to False, in which case NaN values will be considered in the computation, possibly resulting in a NaN maximum if any NaN values are present.

What happens if all values are NaN in the Series?

If all values in the Series are NaN (Not a Number), the result of the Series.max() function will be NaN. This is because the function defaults to skipping NaN values during the computation of the maximum value, and if all values are NaN, there are no valid values to compute the maximum from, resulting in NaN as the output.

Are there any alternative functions to Series.max()?

You can also use the numpy.max() function directly on the Series or the .max() method of NumPy arrays if the Series is converted.

Conclusion

In conclusion, the max() function in Pandas Series is a valuable tool for obtaining the maximum value within a Series object. By default, it ignores NaN values, ensuring accurate calculations. However, by setting skipna=False, NaN values can be included in the computation.

Happy Learning !!

Related Articles

References