Pandas: sum of two boolean rows

In Python:

In [1]: True+True
Out[1]: 2

So, after the following setup:

import pandas as pd
ser1 = pd.Series([True,True,False,False])
ser2 = pd.Series([True,False,True,False])

I want to find an elementary sum ser1and ser2, moreover, logical variables are considered as integers to add, as in the Python example.

But Pandas considers the addition as elemental or "operator" and gives the following (undesirable) output:

In [5]: ser1+ser2
*/lib/python2.7/site-packages/pandas/computation/expressions.py:184: UserWarning: evaluating in Python space because the '+' operator is not supported by numexpr for the bool dtype, use '|' instead
  unsupported[op_str]))
Out[5]: 
0     True
1     True
2     True
3    False
dtype: bool

I know that I get the opportunity to get the desired output using astype(int)any series:

In [6]: ser1.astype(int) + ser2
Out[6]: 
0    2
1    1
2    1
3    0
dtype: int64

Is there any other (more "pandonic") way to get the series [2,1,1,0]? Is there a good explanation why simply adding a series doesn't work here?

+4
source share
2 answers

IIUC, , , - numpy bool, python bools:

>>> a = True
>>> a+a
2
>>> import numpy as np
>>> np.array([a])
array([ True], dtype=bool)
>>> np.array([a]) + np.array([a])
array([ True], dtype=bool)

, , pandas, , , .

+3

+ &

import pandas as pd
ser1 = pd.Series([True,True,False,False])
ser2 = pd.Series([True,False,True,False]) 

print(ser1 & ser2) 

>> 0     True
>> 1    False
>> 2    False
>> 3    False
>> dtype: bool
+1

All Articles