pandas apply errors='coerce': skip errors in apply (tested)

s.apply(float, errors="coerce") fails with TypeError: float() takes no keyword arguments: apply has no errors parameter and forwards the keyword to your function. Use pd.to_numeric(s, errors="coerce") for numbers, or wrap the function in a try/except that returns NaN. Tested on pandas 2.3.3 and 3.1.0.

1. apply has no errors= parameter

import pandas as pd
s = pd.Series(["1.5", "2", "n/a", "3,000", None])
try:
    s.apply(float, errors="coerce")
except TypeError as e:
    print("TypeError:", e)
try:
    s.apply(float)
except (ValueError, TypeError) as e:
    print(type(e).__name__ + ":", e)

Output, identical on pandas 2.3.3 and 3.1.0:

TypeError: float() takes no keyword arguments
ValueError: could not convert string to float: 'n/a'

Series.apply passes any keyword it does not recognise on to your function, so errors="coerce" ends up as float(x, errors="coerce"). That is why the message talks about float(), not pandas. Without it, the first bad value ("n/a") stops the whole apply.

2. For numbers: pd.to_numeric(errors="coerce")

import pandas as pd
s = pd.Series(["1.5", "2", "n/a", "3,000", None])
print(pd.to_numeric(s, errors="coerce").tolist())
print(pd.to_numeric(s.str.replace(",", ""), errors="coerce").tolist())
df = pd.DataFrame({"a": ["1", "x"], "b": ["2.5", "3"]})
print(df.apply(pd.to_numeric, errors="coerce").dtypes.to_dict())

Output, identical on pandas 2.3.3 and 3.1.0:

[1.5, 2.0, nan, nan, nan]
[1.5, 2.0, nan, 3000.0, nan]
{'a': dtype('float64'), 'b': dtype('float64')}

errors="coerce" belongs to pd.to_numeric (and pd.to_datetime); unparseable values and None become NaN. Clean known formats first: "3,000" was lost until the comma was removed. On a DataFrame, df.apply(pd.to_numeric, errors="coerce") works because here the keyword is passed through to to_numeric, which accepts it. Note that the result is float64 even for whole numbers, since NaN is a float. errors="ignore" is deprecated and pandas 3 rejects it; see errors='ignore' deprecated.

3. For any function: wrap it so failures return a default

import math
import pandas as pd
def safe(func, default=math.nan):
    def wrapper(x):
        try:
            return func(x)
        except (ValueError, TypeError, KeyError):
            return default
    return wrapper
s = pd.Series(["1.5", "2", "n/a", None])
print(s.apply(safe(float)).tolist())
lookup = {"a": 1, "b": 2}
print(pd.Series(["a", "z", "b"]).apply(safe(lambda k: lookup[k], default=-1)).tolist())
print(pd.Series(["a", "z", "b"]).map(lookup).tolist())

Output, identical on pandas 2.3.3 and 3.1.0:

[1.5, 2.0, nan, nan]
[1, -1, 2]
[1.0, nan, 2.0]

The wrapper catches only the exceptions you expect, so real bugs (a NameError in your function, say) still surface. For lookups, Series.map(dict) already gives NaN for missing keys with no error, but it turned the integers into floats (1.0); the wrapper with default=-1 kept them as ints.

Related: could not convert string to float · KeyError but the column exists · suppress pandas warnings.

Frequently asked questions

Does pandas apply have an errors='coerce' option?

No. apply forwards unknown keyword arguments to your function. errors='coerce' exists on pd.to_numeric and pd.to_datetime.

How do I skip errors in pandas apply?

Wrap the function in a try/except that returns a default such as NaN for the exceptions you expect, and apply the wrapper.

Why does df.apply(pd.to_numeric, errors='coerce') work?

The keyword is passed through to pd.to_numeric, which accepts errors='coerce', so each column is converted with bad values as NaN.

Written with AI assistance. Measured on 2026-10-23 in the python:3-alpine Docker image (Python 3.14.7) with pandas 2.3.3 and pandas 3.1.0. Outputs pasted unedited. PlainTables is our extension; Python and pandas are free.