Pandas: Checking if values in a Series are monotonically increasing/decreasing

Updated: February 18, 2024 By: Guest Contributor Post a comment

Overview

In data analysis, ensuring the integrity and order of your data is crucial for drawing accurate conclusions. One common scenario involves checking whether the values in a series are monotonically increasing or decreasing. This means the values either consistently go up or down without any jumps in the opposite direction. Pandas, a powerful data manipulation library in Python, provides straightforward methods to perform this check efficiently. In this tutorial, we’ll explore various techniques to determine if the values in a Pandas Series are monotonically increasing or decreasing, starting from basic examples and gradually moving towards more advanced applications.

Creating a Simple Pandas Series

Before diving into the specifics of monotonic checks, let’s briefly review what a Pandas Series is. A Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It’s essentially a column in a spreadsheet or a database table. Understanding how to manipulate Series is fundamental when working with Pandas. Let’s begin with the basics of creating a Series.

import pandas as pd

# Create a simple Series
s = pd.Series([1, 2, 3, 4, 5])
print(s)

Checking Monotonicity

Now, let’s discuss how to check for monotonicity within a Series. Pandas provides two direct attributes to make this task easier: is_monotonic_increasing and is_monotonic_decreasing. These attributes return a Boolean value indicating whether the Series is monotonically increasing or decreasing, respectively.

# Check if the Series is monotonically increasing
print(s.is_monotonic_increasing)

# Check if the Series is monotonically decreasing
print(s.is_monotonic_decreasing)

When the above code is executed with the initial Series we created, s.is_monotonic_increasing will return True, and s.is_monotonic_decreasing will return False, demonstrating that our series is indeed monotonically increasing.

Variations in Monotonicity

It’s also possible for a Series to neither be strictly increasing nor decreasing. A common scenario might include non-unique values where the Series is still considered to be monotonically increasing or decreasing if subsequent values are either equal or follow the trend. Pandas handles these cases with two additional attributes: is_monotonic_increasing_or_equal and is_monotonic_decreasing_or_equal.

# Modified Series with duplicate values
modified_s = pd.Series([1, 2, 2, 3, 4])
print(modified_s.is_monotonic_increasing_or_equal)
print(modified_s.is_monotonic_decreasing_or_equal)

In the above example, the outcome shows that modified_s.is_monotonic_increasing_or_equal returns True, indicating the allowance for non-unique, but non-decreasing sequences within the Series.

Handling Null Values

In dealing with real-world data, encountering null values is inevitable. It’s important to understand how these null values affect monotonic checks. By default, Pandas treats null values as the smallest possible number, allowing a Series with null values at the beginning to still be considered as monotonically increasing. Let’s see how this works with a practical example.

# Series with null values
null_series = pd.Series([None, 1, 2, 3])
print(null_series.is_monotonic_increasing)

This behavior might not always be desirable for your analysis, so being aware of it is crucial.

Advanced Scenarios: Using Custom Functions

In certain situations, the built-in methods might not suffice. For example, you may need a more nuanced approach for defining monotonicity, especially when dealing with time-series data or when the direction of monotonicity might change within the series itself. This is where custom functions using lambda expressions or more complex functions with the apply method can come in handy.

Lets create a function that checks for overall monotonicity by evaluating whether a series is either entirely monotonically increasing or decreasing.

def check_global_monotonicity(series):
    return series.is_monotonic_increasing or series.is_monotonic_decreasing

# Use the function on our Series
print(check_global_monotonicity(s))

This function offers a basic example of how you can start building custom logic to suit your specific needs in data analysis.

Conclusion

Throughout this tutorial, we’ve explored various methods to check for the monotonicity of values in a Pandas Series. From utilizing built-in attributes for quick checks to implementing custom functions for more complex scenarios, understanding how to analyze the ordering and consistency of your data is essential for effective data analysis. As we’ve seen, Pandas provides robust tools to assist in this process, ensuring you can maintain confidence in the integrity of your datasets.