-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
Masking and overflow checks for datetimeindex and timedeltaindex ops #18020
Changes from 12 commits
e9dd35b
36aea69
dd1eddd
92afc80
de663a1
183e8d0
75a403c
ff72d98
ac246b1
05254e2
7214823
f048ffa
7be5984
95f801f
586d872
24dd75b
4dbdfd7
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 |
---|---|---|
|
@@ -447,6 +447,40 @@ def f(): | |
t - offset | ||
pytest.raises(OverflowError, f) | ||
|
||
def test_datetimeindex_sub_timestamp_overflow(self): | ||
dtimax = pd.to_datetime(['now', pd.Timestamp.max]) | ||
dtimin = pd.to_datetime(['now', pd.Timestamp.min]) | ||
|
||
tsneg = Timestamp('1950-01-01') | ||
ts_neg_variants = [tsneg, | ||
tsneg.to_pydatetime(), | ||
tsneg.to_datetime64().astype('datetime64[ns]'), | ||
tsneg.to_datetime64().astype('datetime64[D]')] | ||
|
||
tspos = Timestamp('1980-01-01') | ||
ts_pos_variants = [tspos, | ||
tspos.to_pydatetime(), | ||
tspos.to_datetime64().astype('datetime64[ns]'), | ||
tspos.to_datetime64().astype('datetime64[D]')] | ||
|
||
for variant in ts_neg_variants: | ||
with pytest.raises(OverflowError): | ||
dtimax - variant | ||
|
||
expected = pd.Timestamp.max.value - tspos.value | ||
for variant in ts_pos_variants: | ||
res = dtimax - variant | ||
assert res[1].value == expected | ||
|
||
expected = pd.Timestamp.min.value - tsneg.value | ||
for variant in ts_neg_variants: | ||
res = dtimin - variant | ||
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 comment as below, assert fully the result type. |
||
assert res[1].value == expected | ||
|
||
for variant in ts_pos_variants: | ||
with pytest.raises(OverflowError): | ||
dtimin - variant | ||
|
||
def test_get_duplicates(self): | ||
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-02', | ||
'2000-01-03', '2000-01-03', '2000-01-04']) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1282,3 +1282,23 @@ def test_add_overflow(self): | |
result = (to_timedelta([pd.NaT, '5 days', '1 hours']) + | ||
to_timedelta(['7 seconds', pd.NaT, '4 hours'])) | ||
tm.assert_index_equal(result, exp) | ||
|
||
def test_timedeltaindex_add_timestamp_nat_masking(self): | ||
# GH17991 checking for overflow-masking with NaT | ||
tdinat = pd.to_timedelta(['24658 days 11:15:00', 'NaT']) | ||
|
||
tsneg = Timestamp('1950-01-01') | ||
ts_neg_variants = [tsneg, | ||
tsneg.to_pydatetime(), | ||
tsneg.to_datetime64().astype('datetime64[ns]'), | ||
tsneg.to_datetime64().astype('datetime64[D]')] | ||
|
||
tspos = Timestamp('1980-01-01') | ||
ts_pos_variants = [tspos, | ||
tspos.to_pydatetime(), | ||
tspos.to_datetime64().astype('datetime64[ns]'), | ||
tspos.to_datetime64().astype('datetime64[D]')] | ||
|
||
for variant in ts_neg_variants + ts_pos_variants: | ||
res = tdinat + variant | ||
assert res[1] is pd.NaT | ||
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. check sub as well 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. also would check both add/sub for the reverse, e.g. variant + tdinat (and -) 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. might as well assert fully, e.g.
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. Happy to do the full add/sub/radd/rsub matrix... that was kind of what I started out with. The question becomes where to put it, since arithmetic tests are scattered about. My preference would be new test modules See discussion in #18026, #18036. But for now I'll just edit the contents of the tests already introduced. 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 first entry is not 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. test_arithmetic sounds like a good name. key is to share code as much as possible, via fixtures / parametrization / inheritence. We ideally want these objects to act as similar as possible, so keeping special cases to a minimum is important. note before we do this, I think splitting out the tz-aware tests to its own hierarchy should be done. 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.
Actually as I look at this, I'd much rather close out this bug fix and follow up with the Do It Right approach. 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. certainly. bug fixes are good. separate, self-contained refactorings to make things more readable are better! 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. Great. After the current deluge of clears up, I'll circle back to this. |
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.
add the issue for these
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.
There is no issue for this; I noticed it when tracking down the TimedeltaIndex bug.
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.
sure there is, the PR number!