Skip to content

Commit 7ea921e

Browse files
committed
Merge pull request #9040 from ahjulstad/rework-fix-9011
Return from to_timedelta is forced to dtype timedelta64[ns].
2 parents 4f526fe + 10e5ba8 commit 7ea921e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

doc/source/whatsnew/v0.15.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,5 @@ Bug Fixes
268268
- Fixed ValueError raised by cummin/cummax when datetime64 Series contains NaT. (:issue:`8965`)
269269
- Bug in Datareader returns object dtype if there are missing values (:issue:`8980`)
270270
- Bug in plotting if sharex was enabled and index was a timeseries, would show labels on multiple axes (:issue:`3964`).
271+
272+
- Bug where passing a unit to the TimedeltaIndex constructor applied the to nano-second conversion twice. (:issue:`9011`).

pandas/tseries/tests/test_timedeltas.py

+23
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,22 @@ def conv(v):
525525
expected = TimedeltaIndex([ np.timedelta64(1,'D') ]*5)
526526
tm.assert_index_equal(result, expected)
527527

528+
# Test with lists as input when box=false
529+
expected = np.array(np.arange(3)*1000000000, dtype='timedelta64[ns]')
530+
result = to_timedelta(range(3), unit='s', box=False)
531+
tm.assert_numpy_array_equal(expected, result)
532+
533+
result = to_timedelta(np.arange(3), unit='s', box=False)
534+
tm.assert_numpy_array_equal(expected, result)
535+
536+
result = to_timedelta([0, 1, 2], unit='s', box=False)
537+
tm.assert_numpy_array_equal(expected, result)
538+
539+
# Tests with fractional seconds as input:
540+
expected = np.array([0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')
541+
result = to_timedelta([0., 0.5, 0.8, 1.2], unit='s', box=False)
542+
tm.assert_numpy_array_equal(expected, result)
543+
528544
def testit(unit, transform):
529545

530546
# array
@@ -852,6 +868,13 @@ def test_constructor(self):
852868
pd.offsets.Second(3)])
853869
tm.assert_index_equal(result,expected)
854870

871+
expected = TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02'])
872+
tm.assert_index_equal(TimedeltaIndex(range(3), unit='s'), expected)
873+
expected = TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:05', '0 days 00:00:09'])
874+
tm.assert_index_equal(TimedeltaIndex([0, 5, 9], unit='s'), expected)
875+
expected = TimedeltaIndex(['0 days 00:00:00.400', '0 days 00:00:00.450', '0 days 00:00:01.200'])
876+
tm.assert_index_equal(TimedeltaIndex([400, 450, 1200], unit='ms'), expected)
877+
855878
def test_constructor_coverage(self):
856879
rng = timedelta_range('1 days', periods=10.5)
857880
exp = timedelta_range('1 days', periods=10)

pandas/tseries/timedeltas.py

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def _convert_listlike(arg, box, unit):
5252
value = np.array([ _get_string_converter(r, unit=unit)() for r in arg ],dtype='m8[ns]')
5353
except:
5454
value = np.array([ _coerce_scalar_to_timedelta_type(r, unit=unit, coerce=coerce) for r in arg ])
55+
value = value.astype('timedelta64[ns]', copy=False)
5556

5657
if box:
5758
from pandas import TimedeltaIndex

0 commit comments

Comments
 (0)