Tuesday, 4 June 2019

Why isn't pandas logical operator aligning on the index like it should?

Consider this simple setup:

x = pd.Series([1, 2, 3], index=list('abc'))
y = pd.Series([2, 3, 3], index=list('bca'))

x

a    1
b    2
c    3
dtype: int64

y

b    2
c    3
a    3
dtype: int64

As you can see, the indexes are the same, just in a different order.

Now, consider a simple logical comparison using the equality (==) operator:

x > y
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

This throws a ValueError, most likely because the indexes do not match. On the other hand, calling the equivalent eq operator works:

x.eq(y)

a    False
b     True
c     True
dtype: bool

OTOH, the operator method works given y is first reordered...

x == y.reindex_like(x)

a    False
b     True
c     True
dtype: bool

My understanding was that the function and operator comparison should do the same thing, all other things equal. What is eq doing that the operator comparison doesn't?



from Why isn't pandas logical operator aligning on the index like it should?

No comments:

Post a Comment