Can Only Compare Identically-Labeled Series Objects

0
606

Can Only Compare Identically-Labeled Series Objects

Can Only Compare Identically-Labeled Series Objects is the easiest topic in computer science, In this article, you will learn easily Can Only Compare Identically-Labeled Series Objects. In this tutorial, we are going to learn about Can Only Compare Identically-Labeled Series Objects.

When you try to compare two pandas Series with different indexes, you’ll get this mistake. The following are two scenarios that can result in this error:

Can Only Compare Identically-Labeled Series Objects
s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
s2 = pd.Series([1, 2, 3], index=['d', 'e', 'f'])
s1 == s2

As a result, you’ll find that the two Series artefacts have the same length but different indexes. Similarly, if the index is integer but the index is not the same.

s1 = pd.Series([1, 2, 3], index=[0, 1, 2])
s2 = pd.Series([1, 2, 3], index=[1, 2, 3])
s1 == s2

Two pandas Series have different length.

s1 = pd.Series([1, 2, 3])
s2 = pd.Series([1, 2])
s1 == s2

The error occurs because two pandas Series are compared element by element through vectorization; each element must have the same index value; and the order matters. Even the following will not fit because of the last one:

s1 = pd.Series([1, 2, 3], index=['a', 'a', 'c'])
s2 = pd.Series([1, 2, 3], index=['c', 'a', 'a'])
print(s1 == s2)

The two Series in this case are the same length and have the same set of indexes, but they are in a different order.

s1 = pd.Series([1, 2, 3], index=['a', 'a', 'c'])
s2 = pd.Series([1, 2, 3], index=['c', 'a', 'a'])
s1.reset_index(drop=True) == s2.reset_index(drop=True)
 
#0    True
#1    True
#2    True
#dtype: bool

Notice how the original index has been dropped and reset with an integer sequence.

LEAVE A REPLY

Please enter your comment!
Please enter your name here