Skip to content
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

fix+test to_timedelta('NaT', box=False) #24961

Merged
merged 3 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize over box

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean over the input to to_timedelta? That would marginally reduce code, whereas parametrizing over box would mean completely re-writing this test and probably others.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for box, you are testing box=False, need to have equivalent for box=True (which we probably already have), but nicer to have in one place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at this a little more, I don't think parametrizing over box makes sense; the code for the relevant assertions isn't shareable. Makes more sense to collect the related tests to be adjacent in an upcoming test-refactoring pass.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok fair

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