Skip to content

Commit 1add02c

Browse files
sumanau7jorisvandenbossche
authored andcommitted
Add defensive check for argument errors keyword in to_numeric (#26466)
1 parent 44d5498 commit 1add02c

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Other Enhancements
4141
- :meth:`DataFrame.query` and :meth:`DataFrame.eval` now supports quoting column names with backticks to refer to names with spaces (:issue:`6508`)
4242
- :func:`merge_asof` now gives a more clear error message when merge keys are categoricals that are not equal (:issue:`26136`)
4343
- :meth:`pandas.core.window.Rolling` supports exponential (or Poisson) window type (:issue:`21303`)
44+
- :func:`to_numeric` now gives a error message when errors argument value is not in the tuple of accepted values. (:issue:`26466`)
4445

4546
.. _whatsnew_0250.api_breaking:
4647

pandas/core/tools/numeric.py

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def to_numeric(arg, errors='raise', downcast=None):
105105
if downcast not in (None, 'integer', 'signed', 'unsigned', 'float'):
106106
raise ValueError('invalid downcasting method provided')
107107

108+
if errors not in ('ignore', 'raise', 'coerce'):
109+
raise ValueError('invalid error value specified')
110+
108111
is_series = False
109112
is_index = False
110113
is_scalars = False

pandas/tests/tools/test_numeric.py

+10
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ def test_downcast_invalid_cast():
413413
to_numeric(data, downcast=invalid_downcast)
414414

415415

416+
def test_errors_invalid_value():
417+
# see gh-26466
418+
data = ["1", 2, 3]
419+
invalid_error_value = "invalid"
420+
msg = "invalid error value specified"
421+
422+
with pytest.raises(ValueError, match=msg):
423+
to_numeric(data, errors=invalid_error_value)
424+
425+
416426
@pytest.mark.parametrize("data", [
417427
["1", 2, 3],
418428
[1, 2, 3],

0 commit comments

Comments
 (0)