From f08cd112677bbcedcf86105e06b45f26083813c9 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 27 Jan 2019 12:21:01 -0800 Subject: [PATCH 1/2] fix+test to_timedelta('NaT', box=False) --- pandas/core/tools/timedeltas.py | 3 ++- pandas/tests/scalar/timedelta/test_timedelta.py | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index e3428146b91d8..ddd21d0f62d08 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -120,7 +120,8 @@ def _coerce_scalar_to_timedelta_type(r, unit='ns', box=True, errors='raise'): try: result = Timedelta(r, unit) if not box: - result = result.asm8 + # explicitly view as timedelta64 for case when result is pd.NaT + result = result.asm8.view('timedelta64[ns]') except ValueError: if errors == 'raise': raise diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 9b5fdfb06a9fa..e1838e0160fec 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -309,8 +309,13 @@ def test_iso_conversion(self): assert to_timedelta('P0DT0H0M1S') == expected def test_nat_converters(self): - assert to_timedelta('nat', box=False).astype('int64') == iNaT - assert to_timedelta('nan', box=False).astype('int64') == iNaT + result = to_timedelta('nat', box=False) + assert result.dtype.kind == 'm' + assert result.astype('int64') == iNaT + + result = to_timedelta('nan', box=False) + assert result.dtype.kind == 'm' + assert result.astype('int64') == iNaT @pytest.mark.parametrize('units, np_unit', [(['Y', 'y'], 'Y'), From 1eecb87ca8b60b73fc28ebf621ef908b1b39561f Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 29 Jan 2019 16:39:12 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v0.24.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.1.rst b/doc/source/whatsnew/v0.24.1.rst index 3ac2ed73ea53f..7da99238acca1 100644 --- a/doc/source/whatsnew/v0.24.1.rst +++ b/doc/source/whatsnew/v0.24.1.rst @@ -58,7 +58,7 @@ Bug Fixes - **Timedelta** - +- Bug in :func:`to_timedelta` with `box=False` incorrectly returning a ``datetime64`` object instead of a ``timedelta64`` object (:issue:`24961`) - - -