Skip to content

Commit

Permalink
fix+test to_timedelta('NaT', box=False) (pandas-dev#24961)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and Pingviinituutti committed Feb 28, 2019
1 parent c9d8953 commit 04d38d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Bug Fixes
-

**Timedelta**

- Bug in :func:`to_timedelta` with `box=False` incorrectly returning a ``datetime64`` object instead of a ``timedelta64`` object (:issue:`24961`)
-
-
-
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 04d38d1

Please sign in to comment.