Skip to content

The exceptions from to_datetime(errors='raise') could include information about the exception #16757

Closed
@wes-turner

Description

@wes-turner

Code Sample, a copy-pastable example if possible

import pandas as pd

try:
    pd.to_numeric(pd.Series(['12', 'unparseable']), errors='raise')
except ValueError as e:
    print('to_numeric error (parsing "unparseable"):\n\t' + str(e))

try:
    pd.to_datetime(pd.Series(['2016-01-01', 'unparseable']), errors='raise')
except ValueError as e:
    print('to_datetime error (parsing "unparseable"):\n\t' + str(e))

try:
    pd.to_datetime(pd.Series(['2016-01-01', 'unparseable']), format='%Y-%m-%d', errors='raise')
except ValueError as e:
    print('to_datetime error (parsing "unparseable" given explicit format):\n\t' + str(e))

try:
    pd.to_datetime(pd.Series(['2016-01-01', '12']), errors='raise')
except ValueError as e:
    print('to_datetime error (parsing "12"):\n\t' + str(e))

Problem description

This gives:

to_numeric error (parsing "unparseable"):
        Unable to parse string "unparseable" at position 1
to_datetime error (parsing "unparseable"):
        Unknown string format
to_datetime error (parsing "unparseable" given explicit format):
        time data 'unparseable' doesn't match format specified
to_datetime error (parsing "12"):
        Given date string not likely a datetime.

The information in the to_numeric exception is great. It gives both the unparseable value and the line number that value is on. It'd be helpful for to_datetime to do something similar.

Expected Output

Like with to_numeric, something with information like the following:

Unable to parse string "unparseable" at position 1

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.4.3.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-79-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 36.0.1
Cython: 0.24.1
numpy: 1.13.0
scipy: 0.18.1
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: 2.4.0
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.3.3
bs4: 4.2.1
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions