Skip to content

ENH/API: add right-hand-side bool methods to DataFrame #4334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pandas 0.13

**API Changes**

- ``DataFrame`` now supports being the right hand side operand in boolean
operators (:issue:`4331`)

**Experimental Features**

**Bug Fixes**
Expand Down
3 changes: 3 additions & 0 deletions doc/source/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ enhancements along with a large number of bug fixes.
API changes
~~~~~~~~~~~

- ``DataFrame`` now supports being the right hand side operand in boolean
operators (:issue:`4331`)

Enhancements
~~~~~~~~~~~~

Expand Down
10 changes: 8 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,12 +848,17 @@ def __contains__(self, key):
# boolean operators
__and__ = _arith_method(operator.and_, '__and__', '&')
__or__ = _arith_method(operator.or_, '__or__', '|')
__xor__ = _arith_method(operator.xor, '__xor__')
__xor__ = _arith_method(operator.xor, '__xor__', '^')

__rand__ = _arith_method(lambda x, y: operator.and_(y, x), '__rand__', '&')
__ror__ = _arith_method(lambda x, y: operator.or_(y, x), '__ror__', '|')
__rxor__ = _arith_method(lambda x, y: operator.xor(y, x), '__rxor__', '^')

# Python 2 division methods
if not py3compat.PY3:
__div__ = _arith_method(operator.div, '__div__', '/',
default_axis=None, fill_zeros=np.inf, truediv=False)
default_axis=None, fill_zeros=np.inf,
truediv=False)
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__',
default_axis=None, fill_zeros=np.inf)

Expand Down Expand Up @@ -5939,6 +5944,7 @@ def _homogenize(data, index, dtype=None):

return homogenized


def _from_nested_dict(data):
# TODO: this should be seriously cythonized
new_data = OrderedDict()
Expand Down
41 changes: 33 additions & 8 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4098,11 +4098,30 @@ def test_logical_operators(self):
import operator

def _check_bin_op(op):
result = op(df1, df2)
expected = DataFrame(op(df1.values, df2.values), index=df1.index,
columns=df1.columns)
self.assert_(result.values.dtype == np.bool_)
assert_frame_equal(result, expected)
import itertools
operands = itertools.product((df1, s1), (df2, s2))
for opr_set in operands:
lhs, rhs = opr_set
if not (isinstance(lhs, Series) or isinstance(rhs, Series)):
result = op(*opr_set)
expected = DataFrame(op(lhs.values, rhs.values),
index=lhs.index, columns=lhs.columns)
self.assert_(result.values.dtype == np.bool_)
assert_frame_equal(result, expected)

for df in (df1, df2):
for b in (True, False):
lhs, rhs = b, df
result = op(lhs, rhs)
expected = DataFrame(op(lhs, rhs.values), index=rhs.index,
columns=rhs.columns)
assert_frame_equal(result, expected)

lhs, rhs = df, b
result = op(lhs, rhs)
expected = DataFrame(op(lhs.values, rhs), index=lhs.index,
columns=lhs.columns)
assert_frame_equal(result, expected)

def _check_unary_op(op):
result = op(df1)
Expand Down Expand Up @@ -4130,10 +4149,16 @@ def _check_unary_op(op):

df1 = DataFrame(df1)
df2 = DataFrame(df2)
s1 = df1.a
s2 = df2.b

ops = (operator.and_, operator.or_, operator.xor,
lambda x, y: operator.and_(y, x),
lambda x, y: operator.or_(y, x),
lambda x, y: operator.xor(y, x))

_check_bin_op(operator.and_)
_check_bin_op(operator.or_)
_check_bin_op(operator.xor)
for binop in ops:
_check_bin_op(binop)

_check_unary_op(operator.neg)

Expand Down