From 8dcc3305b5af346f21c9aec60d41c896f971ee82 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 13 Sep 2019 09:28:33 -0700 Subject: [PATCH] CLN: Exception in nanops --- pandas/core/nanops.py | 6 ++++-- pandas/tests/test_nanops.py | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 4db0f75586ead..fe88622a04bb4 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -1300,10 +1300,12 @@ def _ensure_numeric(x): elif not (is_float(x) or is_integer(x) or is_complex(x)): try: x = float(x) - except Exception: + except ValueError: + # e.g. "1+1j" or "foo" try: x = complex(x) - except Exception: + except ValueError: + # e.g. "foo" raise TypeError( "Could not convert {value!s} to numeric".format(value=x) ) diff --git a/pandas/tests/test_nanops.py b/pandas/tests/test_nanops.py index 21ab28c94c978..eb39f01657b90 100644 --- a/pandas/tests/test_nanops.py +++ b/pandas/tests/test_nanops.py @@ -911,10 +911,11 @@ def test_non_convertable_values(self): msg = "Could not convert foo to numeric" with pytest.raises(TypeError, match=msg): nanops._ensure_numeric("foo") - msg = "Could not convert {} to numeric" + + # with the wrong type, python raises TypeError for us + msg = "argument must be a string or a number" with pytest.raises(TypeError, match=msg): nanops._ensure_numeric({}) - msg = r"Could not convert \[\] to numeric" with pytest.raises(TypeError, match=msg): nanops._ensure_numeric([])