Skip to content

nat division by timedelta #17955

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

Merged
merged 6 commits into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ Other API Changes
- Restricted DateOffset keyword arguments. Previously, ``DateOffset`` subclasses allowed arbitrary keyword arguments which could lead to unexpected behavior. Now, only valid arguments will be accepted. (:issue:`17176`).
- Pandas no longer registers matplotlib converters on import. The converters
will be registered and used when the first plot is draw (:issue:`17710`)
- ``NaT`` division with :class:`datetime.timedelta` will now return NaN instead of raising (:issue:`17876`)
Copy link
Member

Choose a reason for hiding this comment

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

Double-tick the NaN.

Copy link
Member

Choose a reason for hiding this comment

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

Also, what is the rationale for returning NaN ? Why not return NaT instead?

Copy link
Member

Choose a reason for hiding this comment

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

It also occurs to me that you might not need a whatsnew given that this is internal.

Copy link
Member Author

Choose a reason for hiding this comment

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

Double-tick the NaN.

Just pushed a fix.

Also, what is the rationale for returning NaN ? Why not return NaT instead?

This is just taking behavior that currently applies to Timedelta and applying the same to timedelta.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm...seems a little non-intuitive to me, but then again, I'm not going to push changing the norm without getting feedback from other maintainers:

@jreback @jorisvandenbossche ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the idea is that in this context NaT is ducking as a Timedelta, so Y = NaT / Timedelta(X) is solving for Timedelta(X) * Y = NaT --> Y = np.nan

Copy link
Member

@gfyoung gfyoung Oct 23, 2017

Choose a reason for hiding this comment

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

Ah, I see. Right, that makes sense. Leave it be then.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that seems to make sense to me, similar as:

In [5]: np.nan / 5
Out[5]: nan

And also this is how pd.NaT / pd.Timedelta(..) is already working? In that case I would rather call it a bug fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

And also this is how pd.NaT / pd.Timedelta(..) is already working?

Yes.


.. _whatsnew_0210.deprecations:

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1428,15 +1428,15 @@ _nat_scalar_rules[Py_GE] = False


cdef _nat_divide_op(self, other):
if (isinstance(other, Timedelta) or
if (PyDelta_Check(other) or
is_timedelta64_object(other) or other is NaT):
return np.nan
if is_integer_object(other) or is_float_object(other):
return NaT
return NotImplemented

cdef _nat_rdivide_op(self, other):
if isinstance(other, Timedelta):
if PyDelta_Check(other):
return np.nan
return NotImplemented

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ def test_nat_arithmetic():
assert left + right is NaT
assert right - left is NaT
assert left - right is NaT
assert np.isnan(left / right)
assert np.isnan(right / left)

# GH 11718
t_utc = Timestamp('2014-01-01', tz='UTC')
Expand Down