Skip to content

Commit 8e90830

Browse files
jbrockmendeljreback
authored andcommitted
BUG: same behavior for Series/Index vs ndarray/list in Series logical ops (pandas-dev#28364)
* treat listlikes symmetrically in logical ops * add test
1 parent 2fb91ed commit 8e90830

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

pandas/core/ops/__init__.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,19 @@ def wrapper(self, other):
829829
is_other_int_dtype = is_integer_dtype(other.dtype)
830830
other = other if is_other_int_dtype else fill_bool(other, self)
831831

832-
else:
833-
# scalars, list, tuple, np.array
834-
is_other_int_dtype = is_integer_dtype(np.asarray(other).dtype)
835-
if is_list_like(other) and not isinstance(other, np.ndarray):
836-
# TODO: Can we do this before the is_integer_dtype check?
837-
# could the is_integer_dtype check be checking the wrong
838-
# thing? e.g. other = [[0, 1], [2, 3], [4, 5]]?
832+
elif is_list_like(other):
833+
# list, tuple, np.ndarray
834+
if not isinstance(other, np.ndarray):
839835
other = construct_1d_object_array_from_listlike(other)
840836

837+
is_other_int_dtype = is_integer_dtype(other.dtype)
838+
other = type(self)(other)
839+
other = other if is_other_int_dtype else fill_bool(other, self)
840+
841+
else:
842+
# i.e. scalar
843+
is_other_int_dtype = lib.is_integer(other)
844+
841845
# TODO: use extract_array once we handle EA correctly, see GH#27959
842846
ovalues = lib.values_from_object(other)
843847

pandas/tests/series/test_operators.py

+35
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@ def test_logical_operators_bool_dtype_with_int(self):
159159
expected = s_tft
160160
assert_series_equal(res, expected)
161161

162+
def test_logical_ops_bool_dtype_with_ndarray(self):
163+
# make sure we operate on ndarray the same as Series
164+
left = pd.Series([True, True, True, False, True])
165+
right = [True, False, None, True, np.nan]
166+
167+
expected = pd.Series([True, False, False, False, False])
168+
result = left & right
169+
tm.assert_series_equal(result, expected)
170+
result = left & np.array(right)
171+
tm.assert_series_equal(result, expected)
172+
result = left & pd.Index(right)
173+
tm.assert_series_equal(result, expected)
174+
result = left & pd.Series(right)
175+
tm.assert_series_equal(result, expected)
176+
177+
expected = pd.Series([True, True, True, True, True])
178+
result = left | right
179+
tm.assert_series_equal(result, expected)
180+
result = left | np.array(right)
181+
tm.assert_series_equal(result, expected)
182+
result = left | pd.Index(right)
183+
tm.assert_series_equal(result, expected)
184+
result = left | pd.Series(right)
185+
tm.assert_series_equal(result, expected)
186+
187+
expected = pd.Series([False, True, True, True, True])
188+
result = left ^ right
189+
tm.assert_series_equal(result, expected)
190+
result = left ^ np.array(right)
191+
tm.assert_series_equal(result, expected)
192+
result = left ^ pd.Index(right)
193+
tm.assert_series_equal(result, expected)
194+
result = left ^ pd.Series(right)
195+
tm.assert_series_equal(result, expected)
196+
162197
def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self):
163198
# GH#9016: support bitwise op for integer types
164199

0 commit comments

Comments
 (0)