Skip to content
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

ERR: better error message on invalid timestamp parsing #11977

Closed
jreback opened this issue Jan 7, 2016 · 3 comments
Closed

ERR: better error message on invalid timestamp parsing #11977

jreback opened this issue Jan 7, 2016 · 3 comments
Labels
Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas

Comments

@jreback
Copy link
Contributor

jreback commented Jan 7, 2016

In [2]: pd.Timestamp('2010011')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-ccfd37d6ef5d> in <module>()
----> 1 pd.Timestamp('2010011')

/Users/jreback/pandas/pandas/tslib.pyx in pandas.tslib.Timestamp.__new__ (pandas/tslib.c:8958)()
    296         cdef _Timestamp ts_base
    297 
--> 298         ts = convert_to_tsobject(ts_input, tz, unit)
    299 
    300         if ts.value == NPY_NAT:

/Users/jreback/pandas/pandas/tslib.pyx in pandas.tslib.convert_to_tsobject (pandas/tslib.c:24122)()
   1232 
   1233     if util.is_string_object(ts):
-> 1234         return convert_str_to_tsobject(ts, tz, unit)
   1235 
   1236     if ts is None or ts is NaT:

/Users/jreback/pandas/pandas/tslib.pyx in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:26110)()
   1361                 ts = parse_datetime_string(ts, dayfirst=dayfirst, yearfirst=yearfirst)
   1362             except Exception:
-> 1363                 raise ValueError
   1364 
   1365     return convert_to_tsobject(ts, tz, unit)

ValueError: 

we are should have a nice error message here. This is normally caught for example in to_datetime, but if its only a single Timestamp no message.

@jreback jreback added Datetime Datetime data dtype Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Jan 7, 2016
@jreback jreback added this to the 0.18.0 milestone Jan 7, 2016
@jreback
Copy link
Contributor Author

jreback commented Jan 7, 2016

xref #11976

@jreback
Copy link
Contributor Author

jreback commented Jan 7, 2016

can start by seeing the caught Exception has a nice message which I think it does (this is what is called in cython)

from pandas.compat import parse_date
In [5]: parse_date('2010011')
ValueError: year is out of range

@jreback
Copy link
Contributor Author

jreback commented Nov 18, 2016

#14684 makes this a bit better.

>>> pd.Timestamp('2010011')
Traceback (most recent call last):
  File "pandas/tslib.pyx", line 1587, in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:29386)
    _string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
  File "pandas/src/datetime.pxd", line 138, in datetime._string_to_dts (pandas/tslib.c:95654)
    result = _cstring_to_dts(tmp, len(val), dts, out_local, out_tzoffset)
ValueError: Error parsing datetime string "2010011" at position 7

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pandas/tslib.pyx", line 1607, in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:29811)
    ts = parse_datetime_string(
  File "pandas/tslib.pyx", line 1973, in pandas.tslib.parse_datetime_string (pandas/tslib.c:35412)
    dt = parse_date(date_string, default=_DEFAULT_DATETIME,
  File "/Users/jreback/miniconda3/envs/py3.6/lib/python3.6/site-packages/dateutil/parser.py", line 1168, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Users/jreback/miniconda3/envs/py3.6/lib/python3.6/site-packages/dateutil/parser.py", line 578, in parse
    if cday > monthrange(cyear, cmonth)[1]:
  File "/Users/jreback/miniconda3/envs/py3.6/lib/python3.6/calendar.py", line 124, in monthrange
    day1 = weekday(year, month, 1)
  File "/Users/jreback/miniconda3/envs/py3.6/lib/python3.6/calendar.py", line 116, in weekday
    return datetime.date(year, month, day).weekday()
ValueError: year is out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/tslib.pyx", line 407, in pandas.tslib.Timestamp.__new__ (pandas/tslib.c:10434)
    ts = convert_to_tsobject(ts_input, tz, unit, 0, 0)
  File "pandas/tslib.pyx", line 1471, in pandas.tslib.convert_to_tsobject (pandas/tslib.c:27533)
    return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
  File "pandas/tslib.pyx", line 1610, in pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:29864)
    raise ValueError("could not convert string to Timestamp")
ValueError: could not convert string to Timestamp

@jreback jreback closed this as completed Nov 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas
Projects
None yet
1 participant