Skip to content

DOC/ERR: update error message / doc-string for to_datetime with non-convertible object and unit kw #14350

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
julienvienne opened this issue Oct 5, 2016 · 3 comments · Fixed by #18380
Labels
Datetime Datetime data dtype Docs Error Reporting Incorrect or improved errors from pandas good first issue
Milestone

Comments

@julienvienne
Copy link

julienvienne commented Oct 5, 2016

A small, complete example of the issue

import pandas as pd
pd.to_datetime(datetime.datetime(2016,1,1), unit='s')
Traceback (most recent call last):
  File "/home/julienv/.pycharm_helpers/pydev/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/pandas/util/decorators.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/pandas/tseries/tools.py", line 424, in to_datetime
    return _convert_listlike(np.array([arg]), box, format)[0]
  File "/usr/local/lib/python3.4/dist-packages/pandas/tseries/tools.py", line 330, in _convert_listlike
    errors=errors)
  File "pandas/tslib.pyx", line 2144, in pandas.tslib.array_with_unit_to_datetime (pandas/tslib.c:39248)
  File "pandas/tslib.pyx", line 2255, in pandas.tslib.array_with_unit_to_datetime (pandas/tslib.c:38492)
ValueError: non convertible value 2016-01-01 00:00:00with the unit 's'

Expected Output

Timestamp('2016-01-01 00:00:00')

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-96-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: fr_FR.UTF-8
LOCALE: fr_FR.UTF-8

pandas: 0.19.0
nose: None
pip: 1.5.4
setuptools: 3.3
Cython: 0.20.1post0
numpy: 1.11.2
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: None
sphinx: 1.2.2
patsy: None
dateutil: 2.5.3
pytz: 2016.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: 2.5.3 (dt dec pq3 ext)
jinja2: 2.7.3
boto: None
pandas_datareader: None

@jorisvandenbossche
Copy link
Member

This may have changed, but is is actually in some way correct. As stated in the docstring (but maybe not clear enough), the unit keyword is to interpret correctly a integer or float, eg:

In [17]: pd.to_datetime(1000000000, unit='s')
Out[17]: Timestamp('2001-09-09 01:46:40')

It is not meant to the precision of the resulting datetime, as this is always 'ns' whathever the input. So since the keyword would not have any effect when parsing a datetime object, I think it is correct to raise an error.

@jorisvandenbossche jorisvandenbossche added the Datetime Datetime data dtype label Oct 5, 2016
@jreback
Copy link
Contributor

jreback commented Oct 5, 2016

if you'd like to submit a doc-string update would be ok. Furthermore the error message is missing a space (after the Timestamp), and I think could be more informative, e.g. print the type of the object (as well, maybe in lieu of the value).

@jreback jreback added Docs Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Oct 5, 2016
@jreback jreback added this to the Next Major Release milestone Oct 5, 2016
@jreback jreback changed the title to_datetime function produces ValueError when setting unit different from 'ns' DOC/ERR: update error message / doc-string for to_datetime with non-convertible object and unit kw Oct 5, 2016
@jorisvandenbossche
Copy link
Member

Yes, an error message that says something like "unit='s' is only valid with numerical input" would be a lot more informative

@mpenkov mpenkov mentioned this issue Nov 20, 2017
4 tasks
@jreback jreback modified the milestones: Next Major Release, 0.22.0 Nov 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Docs Error Reporting Incorrect or improved errors from pandas good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants