From 5bdd8a411ba5417e523bcd7086ea01c9d1b0cf72 Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Mon, 20 May 2019 12:26:33 +0530 Subject: [PATCH 1/3] Add defensive check for argument `errors` in func `to_numeric`. --- pandas/core/tools/numeric.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 08ce649d8602c..d7a1b1119ce4b 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -105,6 +105,9 @@ def to_numeric(arg, errors='raise', downcast=None): if downcast not in (None, 'integer', 'signed', 'unsigned', 'float'): raise ValueError('invalid downcasting method provided') + if errors not in ('ignore', 'raise', 'coerce'): + raise ValueError('invalid error value specified') + is_series = False is_index = False is_scalars = False From 8d96ef8b267740958f88c506f5d0d4ac978708e5 Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Mon, 20 May 2019 13:40:24 +0530 Subject: [PATCH 2/3] Added test for validating if right error message is raised. --- pandas/tests/tools/test_numeric.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/tools/test_numeric.py b/pandas/tests/tools/test_numeric.py index 5d3903cb93bd5..6e3e768f9360f 100644 --- a/pandas/tests/tools/test_numeric.py +++ b/pandas/tests/tools/test_numeric.py @@ -413,6 +413,16 @@ def test_downcast_invalid_cast(): to_numeric(data, downcast=invalid_downcast) +def test_errors_invalid_value(): + # see gh-26466 + data = ["1", 2, 3] + invalid_error_value = "invalid" + msg = "invalid error value specified" + + with pytest.raises(ValueError, match=msg): + to_numeric(data, errors=invalid_error_value) + + @pytest.mark.parametrize("data", [ ["1", 2, 3], [1, 2, 3], From 4de3f1a0c5a4c341c63e6c49835b2e6e77494ea5 Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Mon, 20 May 2019 15:18:01 +0530 Subject: [PATCH 3/3] Added change details, in v.0.25.0.rst file. --- doc/source/whatsnew/v0.25.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index b2d1aa299a45a..9e04dcaa41416 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -41,6 +41,7 @@ Other Enhancements - :meth:`DataFrame.query` and :meth:`DataFrame.eval` now supports quoting column names with backticks to refer to names with spaces (:issue:`6508`) - :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`) - :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`) +- :func:`to_numeric` now gives a error message when errors argument value is not in the tuple of accepted values. (:issue:`26466`) .. _whatsnew_0250.api_breaking: