Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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