Skip to content

Commit ce77643

Browse files
cpcloudjeffreystarr
authored andcommitted
BUG/API: disallow boolean arithmetic operations
1 parent bb25a97 commit ce77643

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ Bug Fixes
311311
- Bug in unpickling ``NaT (NaTType)`` (:issue:`4606`)
312312
- Bug in ``DataFrame.replace()`` where regex metacharacters were being treated
313313
as regexs even when ``regex=False`` (:issue:`6777`).
314+
- Bug in timedelta ops on 32-bit platforms (:issue:`6808`)
314315

315316
pandas 0.13.1
316317
-------------

pandas/computation/expressions.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def _where_numexpr(cond, a, b, raise_on_error=False):
154154
set_use_numexpr(True)
155155

156156

157+
def _bool_arith_check(op_str, a, b, not_allowed=('+', '*', '-', '/',
158+
'//', '**')):
159+
if op_str in not_allowed and a.dtype == bool and b.dtype == bool:
160+
raise NotImplementedError("operator %r not implemented for bool "
161+
"dtypes" % op_str)
162+
163+
157164
def evaluate(op, op_str, a, b, raise_on_error=False, use_numexpr=True,
158165
**eval_kwargs):
159166
""" evaluate and return the expression of the op on a and b
@@ -170,7 +177,7 @@ def evaluate(op, op_str, a, b, raise_on_error=False, use_numexpr=True,
170177
return the results
171178
use_numexpr : whether to try to use numexpr (default True)
172179
"""
173-
180+
_bool_arith_check(op_str, a, b)
174181
if use_numexpr:
175182
return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,
176183
**eval_kwargs)

pandas/tests/test_frame.py

+16
Original file line numberDiff line numberDiff line change
@@ -4769,6 +4769,22 @@ def test_logical_typeerror(self):
47694769
self.assertRaises(TypeError, self.frame.__gt__, 'foo')
47704770
self.assertRaises(TypeError, self.frame.__ne__, 'foo')
47714771

4772+
def test_bool_ops_raise_on_arithmetic(self):
4773+
df = DataFrame({'a': np.random.rand(10) > 0.5,
4774+
'b': np.random.rand(10) > 0.5})
4775+
df2 = DataFrame({'a': np.random.rand(10) > 0.5,
4776+
'b': np.random.rand(10) > 0.5})
4777+
ops = 'add', 'mul', 'sub', 'div', 'truediv', 'floordiv', 'pow'
4778+
names = '+', '*', '-', '/', '/', '//', '**'
4779+
msg = 'operator %r not implemented for bool dtypes'
4780+
for op, name in zip(ops, names):
4781+
if not compat.PY3 or op != 'div':
4782+
with tm.assertRaisesRegexp(NotImplementedError,
4783+
re.escape(msg % name)):
4784+
f = getattr(operator, op)
4785+
f(df, df2)
4786+
f(df.a, df.b)
4787+
47724788
def test_constructor_lists_to_object_dtype(self):
47734789
# from #1074
47744790
d = DataFrame({'a': [np.nan, False]})

0 commit comments

Comments
 (0)