No Alignment When Comparing pandas Series

When you do mathematical operations on two pandas Series (e.g., s1 + s2), the two Series align on their indexes, but when you compare the same two Series (e.g., s1 == s2), the two Series do not align.

Updated 7/4/2020.

Example Code

import pandas as pd
grades1 = pd.Series([10, 25, 20, 6, 3], index=['A', 'B', 'C', 'D', 'F'])
grades2 = pd.Series([3, 11, 24, 10, 3], index=['D', 'C', 'B', 'A', 'F'])

Notice that there are the same number of As and Fs, but different numbers of Bs, Cs, and Ds.

Add the two series (this works). Series aligned by index.

grades1 + grades2


A    20
B    49
C    31
D     9
F     6
dtype: int64

Trying to compare the two series results in a ValueError.

grades1 == grades2


ValueError                                Traceback (most recent call last)
<ipython-input-3-53914bd4150d> in <module>
----> 1 grades1 == grades2

In older versions of pandas, this didn’t error, but the series did not align, so the result was confusing. See this and this.

There are a few ways to get around the problem:

Solution 1: Replace the comparison with an operation and compare the result to a scalar:

grades1 - grades2 == 0

Remember, with operations, the series will align.

Solution 2: Sort both Series on index before comparing:

grades1.sort_index() == grades2.sort_index()

Solution 3: Reindex one Series according to the other:

grades1 == grades2.reindex_like(grades1)

All of these will appropriately return the following boolean Series:

A     True
B    False
C    False
D    False
F     True
dtype: bool

Related Training: Advanced Python

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.