Skip to content

Commit 89ea60b

Browse files
Incorporate feedback
Updated code as per code review suggestions. Signed-off-by: Michael Tiemann <[email protected]>
1 parent b3edefa commit 89ea60b

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

pandas/core/arrays/_mixins.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,10 @@ def __setitem__(self, key, value) -> None:
262262
try:
263263
self._ndarray[key] = value
264264
except TypeError as exc:
265-
# Don't let Python's handling of `complex` make extra complexity for Pandas
266-
if self._ndarray.dtype.kind == "c":
267-
raise ValueError(
268-
*(x.replace("real", "complex") for x in exc.args)
269-
) from exc
270-
raise exc
265+
# Note: when `self._ndarray.dtype.kind == "c"`, Numpy incorrectly complains
266+
# that `must be real number, not ...` when in reality
267+
# a complex argument is more likely what's expected
268+
raise ValueError(exc.args) from exc
271269

272270
def _validate_setitem_value(self, value):
273271
return value

pandas/tests/arithmetic/test_numeric.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,67 +1122,67 @@ def test_ufunc_coercions(self, index_or_series, dtype):
11221122
box = index_or_series
11231123

11241124
result = np.sqrt(idx)
1125+
assert isinstance(result, box)
11251126
if result.dtype.kind == "c":
1126-
assert result.dtype == dtype and isinstance(result, box)
11271127
exp_dtype = dtype
11281128
else:
1129-
assert result.dtype == "f8" and isinstance(result, box)
1129+
# assert result.dtype == "f8"
11301130
exp_dtype = np.float64
11311131
exp = Index(np.sqrt(np.array([1, 2, 3, 4, 5], dtype=exp_dtype)), name="x")
11321132
exp = tm.box_expected(exp, box)
11331133
tm.assert_equal(result, exp)
11341134

11351135
result = np.divide(idx, 2.0)
1136+
assert isinstance(result, box)
11361137
if result.dtype.kind == "c":
1137-
assert result.dtype == dtype and isinstance(result, box)
11381138
exp_dtype = dtype
11391139
else:
1140-
assert result.dtype == "f8" and isinstance(result, box)
1140+
# assert result.dtype == "f8"
11411141
exp_dtype = np.float64
11421142
exp = Index([0.5, 1.0, 1.5, 2.0, 2.5], dtype=exp_dtype, name="x")
11431143
exp = tm.box_expected(exp, box)
11441144
tm.assert_equal(result, exp)
11451145

11461146
# _evaluate_numeric_binop
11471147
result = idx + 2.0
1148+
isinstance(result, box)
11481149
if result.dtype.kind == "c":
1149-
assert result.dtype == dtype and isinstance(result, box)
11501150
exp_dtype = dtype
11511151
else:
1152-
assert result.dtype == "f8" and isinstance(result, box)
1152+
# assert result.dtype == "f8"
11531153
exp_dtype = np.float64
11541154
exp = Index([3.0, 4.0, 5.0, 6.0, 7.0], dtype=exp_dtype, name="x")
11551155
exp = tm.box_expected(exp, box)
11561156
tm.assert_equal(result, exp)
11571157

11581158
result = idx - 2.0
1159+
isinstance(result, box)
11591160
if result.dtype.kind == "c":
1160-
assert result.dtype == dtype and isinstance(result, box)
11611161
exp_dtype = dtype
11621162
else:
1163-
assert result.dtype == "f8" and isinstance(result, box)
1163+
# assert result.dtype == "f8"
11641164
exp_dtype = np.float64
11651165
exp = Index([-1.0, 0.0, 1.0, 2.0, 3.0], dtype=exp_dtype, name="x")
11661166
exp = tm.box_expected(exp, box)
11671167
tm.assert_equal(result, exp)
11681168

11691169
result = idx * 1.0
1170+
isinstance(result, box)
11701171
if result.dtype.kind == "c":
1171-
assert result.dtype == dtype and isinstance(result, box)
11721172
exp_dtype = dtype
11731173
else:
1174-
assert result.dtype == "f8" and isinstance(result, box)
1174+
# assert result.dtype == "f8"
11751175
exp_dtype = np.float64
11761176
exp = Index([1.0, 2.0, 3.0, 4.0, 5.0], dtype=exp_dtype, name="x")
11771177
exp = tm.box_expected(exp, box)
11781178
tm.assert_equal(result, exp)
11791179

11801180
result = idx / 2.0
1181+
isinstance(result, box)
11811182
if result.dtype.kind == "c":
1182-
assert result.dtype == dtype and isinstance(result, box)
11831183
exp_dtype = dtype
11841184
else:
1185-
assert result.dtype == "f8" and isinstance(result, box)
1185+
# assert result.dtype == "f8"
11861186
exp_dtype = np.float64
11871187
exp = Index([0.5, 1.0, 1.5, 2.0, 2.5], dtype=exp_dtype, name="x")
11881188
exp = tm.box_expected(exp, box)

0 commit comments

Comments
 (0)