-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix DTI comparison with None, datetime.date #19301
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 2 commits
b100f59
59ac32c
79d9a02
6720de9
c2c1da5
69f527d
f2dbb7e
0184b39
5dcc09c
78d4eb1
39ad04d
d578aba
6ba5d99
578a4a3
b57d15d
0e0886a
2ea1205
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 |
---|---|---|
|
@@ -119,8 +119,18 @@ def wrapper(self, other): | |
else: | ||
if isinstance(other, list): | ||
other = DatetimeIndex(other) | ||
elif not isinstance(other, (np.ndarray, Index, ABCSeries)): | ||
other = _ensure_datetime64(other) | ||
elif not isinstance(other, (np.datetime64, np.ndarray, | ||
Index, ABCSeries)): | ||
# Following Timestamp convention, __eq__ is all-False | ||
# and __ne__ is all True, others raise TypeError. | ||
if opname == '__eq__': | ||
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 think I prefer to return in the if/elif, then just raise instead of an else |
||
result = np.zeros(shape=self.shape, dtype=bool) | ||
elif opname == '__ne__': | ||
result = np.ones(shape=self.shape, dtype=bool) | ||
else: | ||
raise TypeError('%s type object %s' % | ||
(type(other), str(other))) | ||
return result | ||
|
||
if is_datetimelike(other): | ||
self._assert_tzawareness_compat(other) | ||
|
@@ -147,12 +157,6 @@ def wrapper(self, other): | |
return compat.set_function_name(wrapper, opname, cls) | ||
|
||
|
||
def _ensure_datetime64(other): | ||
if isinstance(other, np.datetime64): | ||
return other | ||
raise TypeError('%s type object %s' % (type(other), str(other))) | ||
|
||
|
||
_midnight = time(0, 0) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,25 @@ def addend(request): | |
return request.param | ||
|
||
|
||
class TestDatetimeIndexComparisons(object): | ||
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. can you lump the None with the NaT comparisons. Do we have the same testing of these comparisons for scalars? 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. AFAICT the comparison tests are scattered. I had planned to consolidate them here in a follow-up to keep narrow focus here.
They would need to be separate tests since the behavior is different, but I can put the tests adjacent to each other in this class. 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. Centralizing the DTI comparison tests is now a part of #19317. After that I'll make a pass to make sure all the relevant cases are covered. |
||
@pytest.mark.parametrize('other', [None, | ||
datetime(2016, 1, 1).date()]) | ||
def test_dti_cmp_invalid(self, tz, other): | ||
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. hmm, I think leave None and np.nan here is ok (rename this from invalid -> null or something more descriptive). put the date with a Timestamp / datetime test. |
||
# GH#19301 | ||
dti = pd.date_range('2016-01-01', periods=2, tz=tz) | ||
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. So is this saying that DatetimeIndex([None, '2016-01-01']) == [None, datetime.date(2016, 1, 1)]) is 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. @TomAugspurger I think part of the confusion is over Following this, #19288 can be de-kludged to make For the moment I'm taking
I recall a discussion of whether 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 find [5] a bit odd. 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.
The behavior is analogous to comparing |
||
|
||
assert not (dti == other).any() | ||
assert (dti != other).all() | ||
with pytest.raises(TypeError): | ||
dti < other | ||
with pytest.raises(TypeError): | ||
dti <= other | ||
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 should also test 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. Just added nan to the params for this test. pd.NaT test is immediately below this one. 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. the datetime.date part of this needs to move to where we test comparisions with Timestamp, datetime.datetime and np.datetime64 |
||
with pytest.raises(TypeError): | ||
dti > other | ||
with pytest.raises(TypeError): | ||
dti >= other | ||
|
||
|
||
class TestDatetimeIndexArithmetic(object): | ||
|
||
def test_dti_add_timestamp_raises(self): | ||
|
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.
rebase again, I just updated