python16
python16

Reputation: 101

In Python Pandas, boolean operation

I am performing boolean operation on two Series. I was expecting the boolean operation to automatically do the operation corresponding to the same index. But instead it just does it by order. Is this the expected behavior or there is some different way of doing this? Thanks

b
Out[47]: 
AEIS    False
AAPL     True
ACFN    False
Name: OldPosition, dtype: bool

a
Out[48]: 
AAPL     True
ACFN    False
AEIS     True
dtype: bool

a&b
Out[50]: 
AAPL    False
ACFN    False
AEIS    False
dtype: bool

Upvotes: 8

Views: 18433

Answers (2)

Cameron Stark
Cameron Stark

Reputation: 1312

If you have the same length Series you should be able to use the index of one Series to order the other Series to line up to fit your needs.

In [15]: a[b.index]
Out[15]:
a     True
b     True
c    False
dtype: bool

In [16]: b
Out[16]:
a    False
b     True
c    False
dtype: bool

In [17]: a[b.index] & b
Out[17]:
a    False
b     True
c    False
dtype: bool

I can confirm that as of pandas 0.17.1 the desired functionality is in place.

In [1]: import pandas as pd

In [2]: a = pd.Series([True, False, True], list('bca'))

In [3]: b = pd.Series([False, True, False], list('abc'))

In [4]: b & a
Out[4]:
a    False
b     True
c    False

Upvotes: 2

Andy Hayden
Andy Hayden

Reputation: 375495

This seems like a bug to me:

In [1]: a = pd.Series([True, False, True], list('bca'))

In [2]: b = pd.Series([False, True, False], list('abc'))

In [3]: a & b
Out[3]:
b    False
c    False
a    False
dtype: bool

One way to workaround is to reindex using the same index:

In [4]: index = a.index | b.index

In [5]: a.reindex(index) & b.reindex(index)
Out[5]:
a    False
b     True
c    False
dtype: bool

Upvotes: 9

Related Questions