Skip to content

Commit cae4616

Browse files
baidoosikjreback
authored andcommitted
.ne fails if comparing a list of columns containing column name 'dtype' #22383 (#22416)
1 parent 08640c3 commit cae4616

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@ Interval
14751475
Indexing
14761476
^^^^^^^^
14771477

1478+
- Bug in :meth:`DataFrame.ne` fails if columns contain column name "dtype" (:issue:`22383`)
14781479
- The traceback from a ``KeyError`` when asking ``.loc`` for a single missing label is now shorter and more clear (:issue:`21557`)
14791480
- :class:`PeriodIndex` now emits a ``KeyError`` when a malformed string is looked up, which is consistent with the behavior of :class:`DatetimeIndex` (:issue:`22803`)
14801481
- When ``.ix`` is asked for a missing integer label in a :class:`MultiIndex` with a first level of integer type, it now raises a ``KeyError``, consistently with the case of a flat :class:`Int64Index`, rather than falling back to positional indexing (:issue:`21593`)

pandas/core/computation/expressions.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import numpy as np
1212

13+
from pandas.core.dtypes.generic import ABCDataFrame
14+
1315
import pandas.core.common as com
1416
from pandas.core.computation.check import _NUMEXPR_INSTALLED
1517
from pandas.core.config import get_option
@@ -160,12 +162,12 @@ def _where_numexpr(cond, a, b):
160162

161163
def _has_bool_dtype(x):
162164
try:
163-
return x.dtype == bool
164-
except AttributeError:
165-
try:
165+
if isinstance(x, ABCDataFrame):
166166
return 'bool' in x.dtypes
167-
except AttributeError:
168-
return isinstance(x, (bool, np.bool_))
167+
else:
168+
return x.dtype == bool
169+
except AttributeError:
170+
return isinstance(x, (bool, np.bool_))
169171

170172

171173
def _bool_arith_check(op_str, a, b, not_allowed=frozenset(('/', '//', '**')),

pandas/tests/test_expressions.py

+16
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,19 @@ def test_bool_ops_warn_on_arithmetic(self):
443443
r = f(df, True)
444444
e = fe(df, True)
445445
tm.assert_frame_equal(r, e)
446+
447+
@pytest.mark.parametrize("test_input,expected", [
448+
(DataFrame([[0, 1, 2, 'aa'], [0, 1, 2, 'aa']],
449+
columns=['a', 'b', 'c', 'dtype']),
450+
DataFrame([[False, False], [False, False]],
451+
columns=['a', 'dtype'])),
452+
(DataFrame([[0, 3, 2, 'aa'], [0, 4, 2, 'aa'], [0, 1, 1, 'bb']],
453+
columns=['a', 'b', 'c', 'dtype']),
454+
DataFrame([[False, False], [False, False],
455+
[False, False]], columns=['a', 'dtype'])),
456+
])
457+
def test_bool_ops_column_name_dtype(self, test_input, expected):
458+
# GH 22383 - .ne fails if columns containing column name 'dtype'
459+
result = test_input.loc[:, ['a', 'dtype']].ne(
460+
test_input.loc[:, ['a', 'dtype']])
461+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)