Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wes-turner opened this issue Jun 22, 2017 · 2 comments · Fixed by #47995
Assignees
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas

Comments

@wes-turner
Copy link

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

@chris-b1 chris-b1 added Difficulty Intermediate Error Reporting Incorrect or improved errors from pandas labels Jun 23, 2017
@chris-b1 chris-b1 modified the milestones: No action, Next Major Release Jun 23, 2017
@chris-b1
Copy link
Contributor

Thanks this would be nice - PR welcome! It may be a little involved as to_datetime can take a few different paths depending on which parser is used, a couple of which are deep in the cython code.

@dannyi96
Copy link
Contributor

dannyi96 commented Jul 5, 2022

take

dannyi96 added a commit to dannyi96/pandas that referenced this issue Aug 6, 2022
dannyi96 added a commit to dannyi96/pandas that referenced this issue Aug 6, 2022
mroeschke pushed a commit that referenced this issue Aug 16, 2022
* #16757: improvement of to_datetime errors

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

* testcase updates

* testcase updates

* testcase updates

* testcase updates
YYYasin19 pushed a commit to YYYasin19/pandas that referenced this issue Aug 23, 2022
…#47995)

* pandas-dev#16757: improvement of to_datetime errors

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

* testcase updates

* testcase updates

* testcase updates

* testcase updates
noatamir pushed a commit to noatamir/pandas that referenced this issue Nov 9, 2022
…#47995)

* pandas-dev#16757: improvement of to_datetime errors

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

* testcase updates

* testcase updates

* testcase updates

* testcase updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants