Skip to content

to_timedelta('1', unit='h') does not work. #16791

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
jtf621 opened this issue Jun 28, 2017 · 6 comments
Closed

to_timedelta('1', unit='h') does not work. #16791

jtf621 opened this issue Jun 28, 2017 · 6 comments
Labels
Duplicate Report Duplicate issue or pull request Error Reporting Incorrect or improved errors from pandas Timedelta Timedelta data type

Comments

@jtf621
Copy link

jtf621 commented Jun 28, 2017

Code Sample, a copy-pastable example if possible

# Your code here

In [8]: pd.to_timedelta('1', unit='h')
Out[8]: Timedelta('0 days 00:00:00.000000')

Problem description

I would expect all the following to return Timedelta('0 days 01:00:00'). pd.to_timedelta(1, unit='h') does not. I must first cast the string '1' to a int or float then to a timedelta.

In [6]: pd.to_timedelta(1.0, unit='h')
Out[6]: Timedelta('0 days 01:00:00')

In [7]: pd.to_timedelta(1, unit='h')
Out[7]: Timedelta('0 days 01:00:00')

In [8]: pd.to_timedelta('1', unit='h')
Out[8]: Timedelta('0 days 00:00:00.000000')

In [9]: pd.to_timedelta('1 H')
Out[9]: Timedelta('0 days 01:00:00')

Expected Output

Output of pd.show_versions()

# Paste the output here pd.show_versions() here

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.20.2
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 6.0.0
sphinx: None
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.0
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
s3fs: None
pandas_gbq: None
pandas_datareader: None

@jorisvandenbossche
Copy link
Member

IMO, we should raise an error in such a case (combination of specifying a unit and parsing a string). I recall we had a similar discussion for to_datetime.

If you have such a string, you should indeed first parse it to a numeric value (you can use to_numeric), and then to a timedelta. I personally don't think we should relax this requirement.

@jorisvandenbossche jorisvandenbossche added Timedelta Timedelta data type Error Reporting Incorrect or improved errors from pandas labels Jun 28, 2017
@jorisvandenbossche
Copy link
Member

For to_datetime, issue is here: #15836 (and open PR: #15896), other related issue: #14350

@jorisvandenbossche jorisvandenbossche added this to the Next Major Release milestone Jun 28, 2017
@chris-b1
Copy link
Contributor

I wouldn't have a problem raising, although it's worth noting that in to_datetime we do the "right" thing:

In [22]: pd.to_datetime(1, unit='D')
Out[22]: Timestamp('1970-01-02 00:00:00')

In [23]: pd.to_datetime('1', unit='D')
Out[23]: Timestamp('1970-01-02 00:00:00')

@jorisvandenbossche
Copy link
Member

Hmm, OK, didn't check that :-)

As long as we raise on invalid ones, I am OK with that as well.
Eg the following works now, but should raise I think:

In [11]: pd.to_timedelta("1 min", unit='h')
Out[11]: Timedelta('0 days 00:01:00')

(similar case with to_timedelta does raise)

@jreback
Copy link
Contributor

jreback commented Jun 28, 2017

we need to raise if ANY unit is passed if its a string. The string contains or implies the unit.

A string is parsed against the timedelta spec e.g. .. days HH:MM:SS.FFFFFFFFF it will take partials so 1 is valid and since no unit is specified it IS ns by definition. e.g. equiv to 1 ns. Actually we should maybe disallow that as well (but in new issue).

@jorisvandenbossche jorisvandenbossche modified the milestones: Contributions Welcome, No action Nov 21, 2018
@jorisvandenbossche jorisvandenbossche added the Duplicate Report Duplicate issue or pull request label Nov 21, 2018
@jorisvandenbossche
Copy link
Member

Closing this as a duplicate of #12136

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Error Reporting Incorrect or improved errors from pandas Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

4 participants