-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Fix Series[timedelta64]+DatetimeIndex[tz] bugs #18884
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
8e856d1
2e824bc
5471d40
d5a0862
e142946
277b8cb
5a5d98c
76582ac
5c2e883
ae3b34d
e72bcf9
68727d4
dd2970c
26c9b51
a7a3d6f
3e71017
dead356
51234b3
7b831ff
c09ac0f
0fc30bb
ae1af46
190197c
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 |
---|---|---|
|
@@ -709,6 +709,17 @@ def wrapper(left, right, name=name, na_op=na_op): | |
|
||
left, right = _align_method_SERIES(left, right) | ||
|
||
if is_timedelta64_dtype(left) and isinstance(right, pd.DatetimeIndex): | ||
# GH#13905 | ||
# Defer to DatetimeIndex/TimedeltaIndex operations where timezones | ||
# are handled carefully. | ||
result = op(pd.TimedeltaIndex(left), right) | ||
name = _maybe_match_name(left, right) | ||
result.name = name # in case name is None, needs to be overridden | ||
return construct_result(left, result, | ||
index=left.index, name=name, | ||
dtype=result.dtype) | ||
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. should 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. yes |
||
|
||
converted = _Op.get_op(left, right, name, na_op) | ||
|
||
left, right = converted.left, converted.right | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -363,6 +363,26 @@ def test_datetimeindex_sub_timestamp_overflow(self): | |
with pytest.raises(OverflowError): | ||
dtimin - variant | ||
|
||
@pytest.mark.parametrize('names', [('foo', None, None), | ||
('baz', 'bar', None), | ||
('bar', 'bar', 'bar')]) | ||
@pytest.mark.parametrize('tz', [None, 'America/Chicago']) | ||
def test_dti_add_series(self, tz, names): | ||
# GH#13905 | ||
index = pd.DatetimeIndex(['2016-06-28 05:30', '2016-06-28 05:31'], | ||
tz=tz, name=names[0]) | ||
ser = pd.Series([pd.Timedelta(seconds=5)] * 2, | ||
index=index, name=names[1]) | ||
expected = pd.Series(index + pd.Timedelta(seconds=5), | ||
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. convention is not to use pd. 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. Will change. |
||
index=index, name=names[2]) | ||
# passing name arg isn't enough when names[2] is None | ||
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. pls add blank lines before comments |
||
expected.name = names[2] | ||
assert expected.dtype == index.dtype | ||
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 also test adding with |
||
res = ser + index | ||
tm.assert_series_equal(res, expected) | ||
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. pls use result= |
||
res2 = index + ser | ||
tm.assert_series_equal(res2, expected) | ||
|
||
|
||
# GH 10699 | ||
@pytest.mark.parametrize('klass,assert_func', zip([Series, DatetimeIndex], | ||
|
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.
don't special case things here, this is the point of the _TimeOp class.
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.
This is the appropriate place. Adding additional wrapping/unwrapping in _TimeOp obscures whats going on. Even if it wasn't a third(!) level of wrapping, b/c of namespacing/closure issues it is essentially impossible to handle overflow checks correctly with the current setup.
Eventually this is going to have to look like:
Everything _TimeOp does is done (better) in the index classes. Anything other than dispatching to those methods is unnecessary duplication and an invitation to inconsistency.
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.
so let's untangle that first. This bug fix just addsmore things to undo later.
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.
This won't need to be undone. Eventually the
and isinstance(right, pd.DatetimeIndex)
part ofif is_timedelta64_dtype(left) and isinstance(right, pd.DatetimeIndex):
is removed and the timedelta64_dtype case is complete.I've given the order of edits quite a bit of thought. Transitioning towards the dispatch approach case-by-case and implementing corresponding tests along the way is the way to go.
Untangle which first? The mess of closures that prevents overflow checks?
I guess if you want to untangle _TimeOp independently, #18832 is a step in that direction, is orthogonal to the other outstanding PRs.
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.
well, until #18832 this needs to change as I have indicated.
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.
#18832 is orthogonal.
What change have you indicated? Not clear on what "untangle this first" refers to.
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.
well I don't want this here. put it where the other conversions are. that can be reactored at some point if its worthile.
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.
I guess if I squint it kind of makes sense to avoid fixing the overflow-check bug in this PR and instead do it separately. Will change.