Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 358
python16 Avatar asked Sep 23 '13 05:09

python16


2 Answers

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
like image 159
Andy Hayden Avatar answered Sep 27 '22 18:09

Andy Hayden


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
like image 41
Cameron Stark Avatar answered Sep 27 '22 18:09

Cameron Stark