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: 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 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],