-
-
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 8 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,12 +509,18 @@ 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 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 | ||
(other.dtype in ('timedelta64[ns]', 'datetime64[ns]')) and | ||
isna(values).all()): | ||
values = np.empty(values.shape, dtype='timedelta64[ns]') | ||
if len(values) == 1 and 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. this is not simpler at all.
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. I agree, hence the sarcasm quotes and the simpler original implementation. 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. Yah, even then this is wrong because it isn't conditioning on the op being sub. Gonna revert back to how it was before this commit. |
||
values = np.empty(values.shape, dtype='timedelta64[ns]') | ||
elif len(values) == 1 and other.dtype == 'datetime64[ns]': | ||
values = np.empty(values.shape, dtype='datetime64[ns]') | ||
else: | ||
values = np.empty(values.shape, dtype='timedelta64[ns]') | ||
values[:] = iNaT | ||
|
||
# a datelike | ||
|
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.