Repository navigation
ENH: add timedelta modulus operator support (mm) - #12120
Conversation
|
Shouldn't the ufunc machinery be able to handle this itself? How is the other arithmetic handled? |
c472685 to
b662819
Compare
|
The ufunc machinery for timedelta arithmetic is here in |
|
I think it would definitely be preferred to implement remainder as another ufunc loop. Hard coding it into |
|
It seems that writing in a I can disable a check in I'll keep trying! |
|
A nice followup would be divmod, which can probably reuse the same code. |
|
You probably need to tweak the type resolver function |
|
Does it need to be added to numpy/core/code_generators/generate_umath.py? |
Apparently yes, and I'm likely going to have to write a TypeResolver function for remainder too. Looks like there's a fair bit of copy-pasting with As far as I can tell this isn't that different from what I've done in this PR -- the Python C API is still used for type checking and making decisions, the mess is just being hidden in the resolver machinery. |
|
Needs a release note. |
b662819 to
9aaeafa
Compare
|
Refactored to use ufunc machinery as requested -- it appears remainder now works for all combinations of My decision to typecast as: Points for:
Points against:
Also good if we can decide on what @shoyer does this need a mailing list check first maybe? Might be nice if we could confine this PR to |
|
As far as I'm concerned, mm->m is the only reasonable signature for modulus. If in doubt, look at the behavior of the builtin timedelta. |
|
from built-in
So maybe supporting modulus with int and float is more controversial (was suggested as expected to work in the linked issue). |
|
The codecov checks are green, but it didn't actually do anything -- missing a report or something. |
|
If I may clarify #12092: from
it follows that all three quantities (dividend, divisor × quotient, remainder) must be homogeneous and have the same time units. Since for multiplication (divisor × quotient) we have
i.e a numeric divisor should be accepted but the result should still be The Therefore I find useful to define while the corresponding or make no sense to me. In other terms: in euclidean division the quotient should be an integer, and this makes sense for |
|
@miccoli Ok, so timedelta stuff can be confusing, but in short -- we're in agreement for the currently proposed One point for possible clarification from your analysis:
This PR currently proposes allowing: That is type homogenous, but the units are not--are you suggesting we don't want that? |
I think this is arguably a bug, especially on Python 3 -- you should need to write I think there's a case for sticking with
I think this is the correct behavior.
Reproducing the behavior of |
I agree that this is correct.
For reference: thus Therefore I would argue that In conclusion: I agree that |
I would argue that this is exactly what I would be in favor of deprecating |
There was a problem hiding this comment.
This indent is a little jarring - I'd put the brace on its own line
There was a problem hiding this comment.
This code is very exception-unsafe - you need to check for NULL from the result of PyObject_Repr, PyUString_ConcatAndDel, and PyUString_FromFormat
There was a problem hiding this comment.
Why not just write this:
if (type_num1 == NPY_TIMEDELTA && type_num2 == NPY_TIMEDELTA) {
// your code
}
else {
return PyUFunc_DefaultTypeResolver(...)
}
That saves you from having to produce an error message for datetime, making all my above comments moot
There was a problem hiding this comment.
It is all just copy-paste from other type resolvers. I was planning to leave room for implementing mq and md remainders, where there would be other switches to handle type_num2 on a case by case basis, so check type_num1 but multiple checks on type_num2.
There was a problem hiding this comment.
This function looks correct, thanks
9aaeafa to
17237f5
Compare
|
Updated with a small reference doc example and to reflect the error handling code changes merged in from Eric recently. Also added a release note |
17237f5 to
0a807fe
Compare
There was a problem hiding this comment.
maybe we should propagate NaT instead here to preserve the mm->m signature -- just noticing this as I try to round up the coverage % a little on the patch
0a807fe to
e07f7dc
Compare
|
Seems strange to me that Something for a later PR. |
There was a problem hiding this comment.
I would have thought something like np.timedelta64(1,'W') % np.timedelta64(10,'D') would be a slightly clearer example, but not really important
There was a problem hiding this comment.
Line wrapping here is a little weird, and doesn't match the other cases with only one TypeDescription
eric-wieser
left a comment
There was a problem hiding this comment.
Minor nits, looks otherwise good.
There was a problem hiding this comment.
Should check the result (0) too
e07f7dc to
6461602
Compare
There was a problem hiding this comment.
I means, strictly M % Y, M % M, Y % Y are all fine too. There is nothing special about how Y and M behave - there are just rules prohibiting mixing units larger than W with units smaller than or equal to W. In isolation, all the units behave the same.
6461602 to
abca780
Compare
|
Cleaned up the test comment a bit & rebased / force pushed so we get a Windows test on appveyor for the time being. |
There was a problem hiding this comment.
Wait, why does this raise a warning? Shouldn't it warn a warning?
There was a problem hiding this comment.
In CPython, ZeroDivisionError: integer division or modulo by zero is raised by timedelta(seconds=10) % timedelta(seconds=0)
There was a problem hiding this comment.
For development all warnings except a few are raised as errors in pytest.ini, but in the absence of that file it should just be a warning. Is it actually raised by the code?
There was a problem hiding this comment.
Eric is right -- in this feature branch it is just a warning when executed as plain code outside the test suite, so I should likely just check for a warning.
I assume we deviate from CPython timedelta because NumPy can gracefully handle division by 0 in scenarios where Python raises an exception.
There was a problem hiding this comment.
Hopefully assert_warns works then -- I'd normally use the pytest equivalent, but there's no precedent for that in NumPy IIRC.
There was a problem hiding this comment.
Coverage says this line is never hit, meaning the result in the array is never actually checked.
There was a problem hiding this comment.
Yeah, I wasn't sure why you asked me to check that the result is zero in a previous review comment, but now I'm seeing that you thought / think we should deviate from standard Python on this one and have warning instead of exception that break control flow?
There was a problem hiding this comment.
I think something weird is going on within pytest that's promoting the warning to an error. I can divide by zero just fine:
In [13]: np.float64(1) % np.float64(0)
C:\Program Files\Python 3.5\Scripts\ipython:1: RuntimeWarning: invalid value encountered in double_scalars
Out[13]: nan
In [16]: np.int64(1) % np.int64(0)
C:\Program Files\Python 3.5\Scripts\ipython:1: RuntimeWarning: divide by zero encountered in longlong_scalars
Out[16]: 0
I think you need to use assert_warns or something here, and then the warning will not escalate, and you can check the result too.
* added support for modulus operator with timedelta operands; type signature is mm->m
abca780 to
c9a6b02
Compare
|
Revised to switch a test from checking for exception to warning, as requested. |
Add support for modulus operator when both operands
are
timedelta64with seconds units, and no other cases.Related to #12092, though doesn't fully cover the modulus
scenarios requested there because I haven't added a branch
for modulus
timedelta64with a Python integer.I think this approach can be summarized as intercepting thearray
nb_remainderslot function before it dispatches tothe ufunc machinery.