Skip to content

to_numeric fails to coerce on TypeError, only works on ValueError #14264

Closed
@JonathanMace

Description

@JonathanMace

Calling to_numeric with errors='coerce' should coerce all non-numeric values to NaN. However, it only seems to work when ValueError is thrown and not for TypeErrors.

Code Sample, a copy-pastable example if possible

import pandas as pd

print "Pandas Version %s\n" % pd.__version__
for badval in ["hello", [], {}]:
    series = pd.Series([badval])
    coerced = pd.to_numeric(series, errors='coerce')
    try:
        pd.to_numeric(series)
    except Exception as e:
        error = e
    print "Bad Value: %s  error=%s  coerced=%s" % (badval, type(error).__name__, coerced.dtype)
    print "DataFrame:"
    print coerced
    print ""

Expected Output

Pandas Version 0.18.1

Bad Value: hello  error=ValueError  coerced=float64
DataFrame:
0   NaN
dtype: float64

Bad Value: []  error=TypeError  coerced=object
DataFrame:
0    []
dtype: object

Bad Value: {}  error=TypeError  coerced=object
DataFrame:
0    {}
dtype: object

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Linux
OS-release: 4.0.9-63_fbk11_3989_g7d5ca22
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.4
pip: 8.1.2
setuptools: 25.1.6
Cython: 0.22
numpy: 1.11.1
scipy: 0.18.0
statsmodels: 0.6.1
xarray: None
IPython: 5.0.0
sphinx: 1.2.3
patsy: 0.3.0
dateutil: 2.4.1
pytz: 2015.2
blosc: None
bottleneck: None
tables: 3.1.1
numexpr: 2.6.1
matplotlib: 1.4.3
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.6.7
lxml: 3.4.2
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 0.9.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.36.0
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions