-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
implement truediv, rtruediv directly in TimedeltaArray; tests #23829
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 4 commits
6ec1f08
4c2cc59
bd2ee96
3275dd9
79901f5
adea273
da9f743
ba9e490
10bb49b
8f276ae
7d56da9
6097789
2037be8
ffedf35
2fc44aa
cd4ff57
641ad20
e0d696f
7d9e677
dfc7af4
55cad6b
d21ae78
d72bf90
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 |
---|---|---|
|
@@ -329,6 +329,7 @@ def _evaluate_with_timedelta_like(self, other, op): | |
__rfloordiv__ = _wrap_tdi_op(ops.rfloordiv) | ||
|
||
def __truediv__(self, other): | ||
# timedelta / X is well-defined for timedelta-like or numeric X | ||
other = lib.item_from_zerodim(other) | ||
|
||
if isinstance(other, (ABCSeries, ABCDataFrame, ABCIndexClass)): | ||
|
@@ -368,18 +369,14 @@ def __truediv__(self, other): | |
elif is_object_dtype(other): | ||
result = [self[n] / other[n] for n in range(len(self))] | ||
result = np.array(result) | ||
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 this is really close to what soft_convert_objects does. 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. That isn't clear to me. 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. you are essentially re-implementing it. i would rather not do that. 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'm not clear on what you have in mind. Something like:
? 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 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. Fair enough, will change 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. changed. this ends up changing the behavior of the 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 is changed here? shouldn't is_object_type result in a TypeError or a NotImplemented? 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. There are two independent questions that have been asked about the object-dtype case:
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 this is fine, you are returning object dtype (which is consistent with how we do for Series now) |
||
if lib.infer_dtype(result) == 'timedelta': | ||
# TODO: keep inferred freq? | ||
result, _ = sequence_to_td64ns(result) | ||
return type(self)(result) | ||
|
||
return np.array(result) | ||
return result | ||
|
||
else: | ||
result = self._data / other | ||
return type(self)(result) | ||
|
||
def __rtruediv__(self, other): | ||
# X / timedelta is defined only for timedelta-like X | ||
other = lib.item_from_zerodim(other) | ||
|
||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isinstance(other, (ABCSeries, ABCDataFrame, ABCIndexClass)): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1453,9 +1453,13 @@ def test_td64arr_div_numeric_array(self, box_with_array, vector, dtype): | |
with pytest.raises(TypeError, match=pattern): | ||
vector / tdser | ||
|
||
if not isinstance(vector, pd.Index): | ||
if (not isinstance(vector, pd.Index) and | ||
box_with_array is not pd.DataFrame): | ||
# Index.__rdiv__ won't try to operate elementwise, just raises | ||
# DataFrame casts just-NaT object column to datetime64 | ||
result = tdser / vector.astype(object) | ||
expected = [tdser[n] / vector[n] for n in range(len(tdser))] | ||
expected = tm.box_expected(expected, xbox) | ||
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. Wouldn't this converted the expected list to an DatetimeArray, while the expected result is on object ndarray? (so it's not really testing that?) 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. No. In the case where 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. Ah, OK, all a bit opaque .. (I checked what 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, I'm hoping to simplify some of it, and ideally even get rid of box_expected, but it'll be a while before thats feasible. |
||
tm.assert_equal(result, expected) | ||
|
||
with pytest.raises(TypeError, match=pattern): | ||
|
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.
Do we need to allow this?
I would be fine with raising a TypeError here.
(I first wanted to say: can't we dispatch that to numpy, thinking that numpy object dtype would handle that, but they raise a TypeError)
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 can see why this would be first on the chopping block if we had to support fewer cases. Is there a compelling reason not to handle this case?
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 can also turn around the question :) Is there a compelling reason to do handle this case?
It's just an extra case to support. And eg, we could discuss whether this should return object dtype data or timedelta, as you are inferring now? Looking at Series behaviour with int64 and object integers, it actually returns object. For datetimes it now raises. So at least, our support is at the moment not very consistent.
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.
Because a selling point of pandas is that things Just Work? Because the code and tests are already written, so the marginal cost is \approx zero?
Fair enough. If a goal is to make things more consistent (which I'm +1 on BTW) then we're probably not going to go around and start breaking the places where it currently is supported.
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 agree with @jbrockmendel here