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

The Pandas Series.max() function is used to get the maximum of the values. It returns a float value representing the max of the series. This function always returns a Series even if only one value is present. In this article, I will explain the syntax of Series.max() function, its parameters, and how to get the max values of a given Series object with examples.

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() Function

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


# 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)}: Specify the axis for the function to be applied on. For Series, this parameter is unused and defaults to 0.
  • skipna – bool, default True: Excludes all None/NaN from the mean/computing the result. Default set to True.
  • level – If the axis is a MultiIndex, then the level specifies the level to compute the maximum along. Default is None.
  • numeric_only – Boolean value indicating whether to include only numeric data types when computing the maximum. Default is None.
  • **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.

Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of 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 uses skipna=True meaning it ignores the NaN (Not a Number) values when finding the maximum value. If a series contains NaN values, they are automatically ignored and get the maximum value of the given series object. For examples,


# 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, you can also use the skipna=False to not ignore NaN values, and if you have Nan values in the series it returns nan values. NaN is considered 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() Function


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)

Frequently Asked Questions on Pandas Series.max() Function

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.

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 when all values in the Series are NaN, there are no valid numeric values to determine the maximum from, so the result is NaN. In other words, the maximum value cannot be computed if there are no valid numeric values in the Series.

Is Series.max() sensitive to the data type of the Series?

The Series.max() function is sensitive to the data type of the Series. It behaves differently depending on the data type of the elements in the Series

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 this article, I have explained the pandas series max() function that returns the maximum value of a given series object with examples.

Happy Learning !!

Related Articles

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium