Skip to content

Commit 2aeed3f

Browse files
jbrockmendeljreback
authored andcommitted
REF: separate bloated test (#28081)
1 parent 05cc959 commit 2aeed3f

File tree

2 files changed

+124
-63
lines changed

2 files changed

+124
-63
lines changed

pandas/core/ops/__init__.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -791,16 +791,21 @@ def wrapper(self, other):
791791
self, other = _align_method_SERIES(self, other, align_asobject=True)
792792
res_name = get_op_result_name(self, other)
793793

794+
# TODO: shouldn't we be applying finalize whenever
795+
# not isinstance(other, ABCSeries)?
796+
finalizer = (
797+
lambda x: x.__finalize__(self)
798+
if not isinstance(other, (ABCSeries, ABCIndexClass))
799+
else x
800+
)
801+
794802
if isinstance(other, ABCDataFrame):
795803
# Defer to DataFrame implementation; fail early
796804
return NotImplemented
797805

798806
elif isinstance(other, (ABCSeries, ABCIndexClass)):
799807
is_other_int_dtype = is_integer_dtype(other.dtype)
800-
other = fill_int(other) if is_other_int_dtype else fill_bool(other)
801-
802-
ovalues = other.values
803-
finalizer = lambda x: x
808+
other = other if is_other_int_dtype else fill_bool(other)
804809

805810
else:
806811
# scalars, list, tuple, np.array
@@ -811,8 +816,8 @@ def wrapper(self, other):
811816
# thing? e.g. other = [[0, 1], [2, 3], [4, 5]]?
812817
other = construct_1d_object_array_from_listlike(other)
813818

814-
ovalues = other
815-
finalizer = lambda x: x.__finalize__(self)
819+
# TODO: use extract_array once we handle EA correctly, see GH#27959
820+
ovalues = lib.values_from_object(other)
816821

817822
# For int vs int `^`, `|`, `&` are bitwise operators and return
818823
# integer dtypes. Otherwise these are boolean ops

pandas/tests/series/test_operators.py

+113-57
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,14 @@ def test_bool_operators_with_nas(self, bool_op):
3636
expected[mask] = False
3737
assert_series_equal(result, expected)
3838

39-
def test_operators_bitwise(self):
39+
def test_logical_operators_bool_dtype_with_empty(self):
4040
# GH#9016: support bitwise op for integer types
4141
index = list("bca")
4242

4343
s_tft = Series([True, False, True], index=index)
4444
s_fff = Series([False, False, False], index=index)
45-
s_tff = Series([True, False, False], index=index)
4645
s_empty = Series([])
4746

48-
# TODO: unused
49-
# s_0101 = Series([0, 1, 0, 1])
50-
51-
s_0123 = Series(range(4), dtype="int64")
52-
s_3333 = Series([3] * 4)
53-
s_4444 = Series([4] * 4)
54-
5547
res = s_tft & s_empty
5648
expected = s_fff
5749
assert_series_equal(res, expected)
@@ -60,6 +52,16 @@ def test_operators_bitwise(self):
6052
expected = s_tft
6153
assert_series_equal(res, expected)
6254

55+
def test_logical_operators_int_dtype_with_int_dtype(self):
56+
# GH#9016: support bitwise op for integer types
57+
58+
# TODO: unused
59+
# s_0101 = Series([0, 1, 0, 1])
60+
61+
s_0123 = Series(range(4), dtype="int64")
62+
s_3333 = Series([3] * 4)
63+
s_4444 = Series([4] * 4)
64+
6365
res = s_0123 & s_3333
6466
expected = Series(range(4), dtype="int64")
6567
assert_series_equal(res, expected)
@@ -68,76 +70,129 @@ def test_operators_bitwise(self):
6870
expected = Series(range(4, 8), dtype="int64")
6971
assert_series_equal(res, expected)
7072

71-
s_a0b1c0 = Series([1], list("b"))
72-
73-
res = s_tft & s_a0b1c0
74-
expected = s_tff.reindex(list("abc"))
73+
s_1111 = Series([1] * 4, dtype="int8")
74+
res = s_0123 & s_1111
75+
expected = Series([0, 1, 0, 1], dtype="int64")
7576
assert_series_equal(res, expected)
7677

77-
res = s_tft | s_a0b1c0
78-
expected = s_tft.reindex(list("abc"))
78+
res = s_0123.astype(np.int16) | s_1111.astype(np.int32)
79+
expected = Series([1, 1, 3, 3], dtype="int32")
7980
assert_series_equal(res, expected)
8081

81-
n0 = 0
82-
res = s_tft & n0
83-
expected = s_fff
84-
assert_series_equal(res, expected)
82+
def test_logical_operators_int_dtype_with_int_scalar(self):
83+
# GH#9016: support bitwise op for integer types
84+
s_0123 = Series(range(4), dtype="int64")
8585

86-
res = s_0123 & n0
86+
res = s_0123 & 0
8787
expected = Series([0] * 4)
8888
assert_series_equal(res, expected)
8989

90-
n1 = 1
91-
res = s_tft & n1
92-
expected = s_tft
93-
assert_series_equal(res, expected)
94-
95-
res = s_0123 & n1
90+
res = s_0123 & 1
9691
expected = Series([0, 1, 0, 1])
9792
assert_series_equal(res, expected)
9893

99-
s_1111 = Series([1] * 4, dtype="int8")
100-
res = s_0123 & s_1111
101-
expected = Series([0, 1, 0, 1], dtype="int64")
102-
assert_series_equal(res, expected)
103-
104-
res = s_0123.astype(np.int16) | s_1111.astype(np.int32)
105-
expected = Series([1, 1, 3, 3], dtype="int32")
106-
assert_series_equal(res, expected)
94+
def test_logical_operators_int_dtype_with_float(self):
95+
# GH#9016: support bitwise op for integer types
96+
s_0123 = Series(range(4), dtype="int64")
10797

108-
with pytest.raises(TypeError):
109-
s_1111 & "a"
110-
with pytest.raises(TypeError):
111-
s_1111 & ["a", "b", "c", "d"]
11298
with pytest.raises(TypeError):
11399
s_0123 & np.NaN
114100
with pytest.raises(TypeError):
115101
s_0123 & 3.14
116102
with pytest.raises(TypeError):
117103
s_0123 & [0.1, 4, 3.14, 2]
104+
with pytest.raises(TypeError):
105+
s_0123 & np.array([0.1, 4, 3.14, 2])
118106

119-
# s_0123 will be all false now because of reindexing like s_tft
120-
exp = Series([False] * 7, index=[0, 1, 2, 3, "a", "b", "c"])
121-
assert_series_equal(s_tft & s_0123, exp)
122-
123-
# s_tft will be all false now because of reindexing like s_0123
124-
exp = Series([False] * 7, index=[0, 1, 2, 3, "a", "b", "c"])
125-
assert_series_equal(s_0123 & s_tft, exp)
126-
127-
assert_series_equal(s_0123 & False, Series([False] * 4))
128-
assert_series_equal(s_0123 ^ False, Series([False, True, True, True]))
129-
assert_series_equal(s_0123 & [False], Series([False] * 4))
130-
assert_series_equal(s_0123 & (False), Series([False] * 4))
131-
assert_series_equal(
132-
s_0123 & Series([False, np.NaN, False, False]), Series([False] * 4)
133-
)
107+
# FIXME: this should be consistent with the list case above
108+
expected = Series([False, True, False, True])
109+
result = s_0123 & Series([0.1, 4, -3.14, 2])
110+
assert_series_equal(result, expected)
111+
112+
def test_logical_operators_int_dtype_with_str(self):
113+
s_1111 = Series([1] * 4, dtype="int8")
114+
115+
with pytest.raises(TypeError):
116+
s_1111 & "a"
117+
with pytest.raises(TypeError):
118+
s_1111 & ["a", "b", "c", "d"]
119+
120+
def test_logical_operators_int_dtype_with_bool(self):
121+
# GH#9016: support bitwise op for integer types
122+
s_0123 = Series(range(4), dtype="int64")
123+
124+
expected = Series([False] * 4)
125+
126+
result = s_0123 & False
127+
assert_series_equal(result, expected)
128+
129+
result = s_0123 & [False]
130+
assert_series_equal(result, expected)
131+
132+
result = s_0123 & (False,)
133+
assert_series_equal(result, expected)
134134

135-
s_ftft = Series([False, True, False, True])
136-
assert_series_equal(s_0123 & Series([0.1, 4, -3.14, 2]), s_ftft)
135+
result = s_0123 ^ False
136+
expected = Series([False, True, True, True])
137+
assert_series_equal(result, expected)
138+
139+
def test_logical_operators_int_dtype_with_object(self):
140+
# GH#9016: support bitwise op for integer types
141+
s_0123 = Series(range(4), dtype="int64")
142+
143+
result = s_0123 & Series([False, np.NaN, False, False])
144+
expected = Series([False] * 4)
145+
assert_series_equal(result, expected)
137146

138147
s_abNd = Series(["a", "b", np.NaN, "d"])
139-
res = s_0123 & s_abNd
140-
expected = s_ftft
148+
result = s_0123 & s_abNd
149+
expected = Series([False, True, False, True])
150+
assert_series_equal(result, expected)
151+
152+
def test_logical_operators_bool_dtype_with_int(self):
153+
index = list("bca")
154+
155+
s_tft = Series([True, False, True], index=index)
156+
s_fff = Series([False, False, False], index=index)
157+
158+
res = s_tft & 0
159+
expected = s_fff
160+
assert_series_equal(res, expected)
161+
162+
res = s_tft & 1
163+
expected = s_tft
164+
assert_series_equal(res, expected)
165+
166+
def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self):
167+
# GH#9016: support bitwise op for integer types
168+
169+
# with non-matching indexes, logical operators will cast to object
170+
# before operating
171+
index = list("bca")
172+
173+
s_tft = Series([True, False, True], index=index)
174+
s_tft = Series([True, False, True], index=index)
175+
s_tff = Series([True, False, False], index=index)
176+
177+
s_0123 = Series(range(4), dtype="int64")
178+
179+
# s_0123 will be all false now because of reindexing like s_tft
180+
expected = Series([False] * 7, index=[0, 1, 2, 3, "a", "b", "c"])
181+
result = s_tft & s_0123
182+
assert_series_equal(result, expected)
183+
184+
expected = Series([False] * 7, index=[0, 1, 2, 3, "a", "b", "c"])
185+
result = s_0123 & s_tft
186+
assert_series_equal(result, expected)
187+
188+
s_a0b1c0 = Series([1], list("b"))
189+
190+
res = s_tft & s_a0b1c0
191+
expected = s_tff.reindex(list("abc"))
192+
assert_series_equal(res, expected)
193+
194+
res = s_tft | s_a0b1c0
195+
expected = s_tft.reindex(list("abc"))
141196
assert_series_equal(res, expected)
142197

143198
def test_scalar_na_logical_ops_corners(self):
@@ -523,6 +578,7 @@ def test_comparison_operators_with_nas(self):
523578

524579
assert_series_equal(result, expected)
525580

581+
# FIXME: dont leave commented-out
526582
# fffffffuuuuuuuuuuuu
527583
# result = f(val, s)
528584
# expected = f(val, s.dropna()).reindex(s.index)

0 commit comments

Comments
 (0)