Skip to content

CLN: Exception in nanops #28433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a string an allowable value here? Not familiar but most of the other exceptions seem to require numeric

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"1" or "2.0" or "3+5j" are all acceptable

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([])

Expand Down