-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Make Series[datetime64] - pd.NaT behave like DatetimeIndex - pd.NaT #18960
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
Changes from all commits
d4b799a
c07f49a
49ed387
782ea74
5540a4b
e99b5e1
f0da8ec
f275a6c
9ac970d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -407,8 +407,12 @@ def _validate_datetime(self, lvalues, rvalues, name): | |
|
||
# if tz's must be equal (same or None) | ||
if getattr(lvalues, 'tz', None) != getattr(rvalues, 'tz', None): | ||
raise ValueError("Incompatible tz's on datetime subtraction " | ||
"ops") | ||
if len(rvalues) == 1 and isna(rvalues).all(): | ||
# NaT gets a pass | ||
pass | ||
else: | ||
raise ValueError("Incompatible tz's on datetime " | ||
"subtraction ops", rvalues) | ||
|
||
else: | ||
raise TypeError('cannot operate on a series without a rhs ' | ||
|
@@ -505,11 +509,20 @@ def _convert_to_array(self, values, name=None, other=None): | |
inferred_type = lib.infer_dtype(values) | ||
if (inferred_type in ('datetime64', 'datetime', 'date', 'time') or | ||
is_datetimetz(inferred_type)): | ||
|
||
if ovalues is pd.NaT and name == '__sub__': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's wrong this this code that is existing? (obviously the dtype is incorrect if other is dt64 but otherwise this is fine)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For starters the other.dtype condition excludes datetime64s with timezones. More generally, this PR is specifically about scalar subtraction of pd.NaT and I would rather give that its own block than try to shoe-horn it into an existing block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simplify this like the above code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. But in my head I'm going to put sarcasm quotes around the word "simplify". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same issue. you are repeating code. you only need to select the dtype if other.dtype == 'timedelta64[ns]' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you're suggesting would catch |
||
# Note: This can only occur when `values` represents `right` | ||
# i.e. `other`. | ||
if other.dtype == 'timedelta64[ns]': | ||
values = np.array([iNaT], dtype='timedelta64[ns]') | ||
else: | ||
values = np.array([iNaT], dtype='datetime64[ns]') | ||
|
||
# if we have a other of timedelta, but use pd.NaT here we | ||
# we are in the wrong path | ||
if (supplied_dtype is None and other is not None and | ||
(other.dtype in ('timedelta64[ns]', 'datetime64[ns]')) and | ||
isna(values).all()): | ||
elif (supplied_dtype is None and other is not None and | ||
(other.dtype in ('timedelta64[ns]', 'datetime64[ns]')) and | ||
isna(values).all()): | ||
values = np.empty(values.shape, dtype='timedelta64[ns]') | ||
values[:] = iNaT | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does this matter if it is exactly len == 1, seems odd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue at hand is that
pd.NaT
is special because it can play the role of adatetime
or atimedelta
depending on context._TimeOp
wraps scalar inputs in an array that becomesrvalues
. But at a basic level here what we are interested in is "was the originalright
argumentpd.NaT
?" What this is doing (and the same you're asking me to do below) is to undo wrapping done by_TimeOp
. It is a much less clear way of checking "is the argumentpd.NaT
?"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you didn't answer the question, whether I have 1 NaT or all NaT is immaterial, I cann't use the rhs dtype as its ambiguous and must use the left.