Skip to content

Commit e34745c

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Exception in nanops (#28433)
1 parent fae8177 commit e34745c

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pandas/core/nanops.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1300,10 +1300,12 @@ def _ensure_numeric(x):
13001300
elif not (is_float(x) or is_integer(x) or is_complex(x)):
13011301
try:
13021302
x = float(x)
1303-
except Exception:
1303+
except ValueError:
1304+
# e.g. "1+1j" or "foo"
13041305
try:
13051306
x = complex(x)
1306-
except Exception:
1307+
except ValueError:
1308+
# e.g. "foo"
13071309
raise TypeError(
13081310
"Could not convert {value!s} to numeric".format(value=x)
13091311
)

pandas/tests/test_nanops.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,11 @@ def test_non_convertable_values(self):
911911
msg = "Could not convert foo to numeric"
912912
with pytest.raises(TypeError, match=msg):
913913
nanops._ensure_numeric("foo")
914-
msg = "Could not convert {} to numeric"
914+
915+
# with the wrong type, python raises TypeError for us
916+
msg = "argument must be a string or a number"
915917
with pytest.raises(TypeError, match=msg):
916918
nanops._ensure_numeric({})
917-
msg = r"Could not convert \[\] to numeric"
918919
with pytest.raises(TypeError, match=msg):
919920
nanops._ensure_numeric([])
920921

0 commit comments

Comments
 (0)