Can there be a way to to do masking that is dependent on more than one boolean?
0 < pd.Series(x) < 1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-132-06fa1595460a> in <module>()
----> 1 0 < pd.Series(x) < 1
/Users/jespinoz/anaconda/lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self)
890 raise ValueError("The truth value of a {0} is ambiguous. "
891 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 892 .format(self.__class__.__name__))
893
894 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
My solution for it is:
Comment From: chris-b1
As far as I understand, this is impossible due python syntax limitations.
0 < x < 1
is just syntactic sugar for x > 0 and x < 1
. The behavior of and
is not overridable, which is in part why pandas
and numpy
use the bitwise and (&
) for masks.
Comment From: chris-b1
The way to write this would be (pd.Series(x) > 0) & (pd.Series(x) < 1)
Comment From: jreback
The pandas dialect add this extension (and use of and) within .query
see docs here
In [7]: df = pd.DataFrame({'A' : [0, 1, 2]})
In [8]: df
Out[8]:
A
0 0
1 1
2 2
In [9]: df[(df.A > 0) & (df.A < 2)]
Out[9]:
A
1 1
In [10]: df.query('0 < A < 2')
Out[10]:
A
1 1
In [11]: df.query('0 < A & A < 2')
Out[11]:
A
1 1
In [12]: df.query('0 < A and A < 2')
Out[12]:
A
1 1
Comment From: jolespin
Thanks guys, sorry I didn't know that was already a functionality. I also didn't know you could use &
as well.