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()
Comment From: jorisvandenbossche
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.
Comment From: jorisvandenbossche
For to_datetime
, issue is here: https://github.com/pandas-dev/pandas/issues/15836 (and open PR: https://github.com/pandas-dev/pandas/pull/15896), other related issue: https://github.com/pandas-dev/pandas/issues/14350
Comment From: chris-b1
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')
Comment From: jorisvandenbossche
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)
Comment From: jreback
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).
Comment From: jorisvandenbossche
Closing this as a duplicate of https://github.com/pandas-dev/pandas/issues/12136