Skip to content

remainder is not implemented for timedelta64 #12092

Closed

Description

@miccoli

timedelta64 allows for multiplication and division, but not for remainder

Reproducing code example:

>>> a = np.timedelta64(7, 's')
>>> b = np.timedelta64(3, 's')
>>> c = np.timedelta64(1, 's')
>>> a == b*2 + c
True
>>> a / 2
numpy.timedelta64(3,'s')
>>> a / b
2.3333333333333335

I would expect that a % b == c and a % 2 == c
but

>>> a % b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ufunc 'remainder' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
>>> a % 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ufunc 'remainder' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Numpy/Python version information:

>>> import sys, numpy; print(numpy.__version__, sys.version)
1.15.2 3.6.5 (default, Apr  6 2018, 12:22:55) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]

Activity

  1. tylerjereddy commented on Oct 5, 2018

    @tylerjereddy
    Contributor

    There hasn't been much movement on datetime64 issues lately; I tried to fix a few things here and there.

    If this is something that should be fixed, people may need some convincing!

  2. miccoli commented on Oct 5, 2018

    @miccoli
    ContributorAuthor

    @tylerjereddy
    timedelta64 is the base for pandas Time Deltas.

    Lately I'm fighting with timestamped data, read via pandas IO. It was not a smooth journey: a lot of bumps on the road, some of which I contributed (at leas so I hope) to mitigate (see pandas-dev/pandas#22802).

    My point is that if timedelta64 is not deprecated it should be implemented in a consistent way. Continuously juggling between different representation of the same quantity depending of the operation you are trying to perform is unnerving.

    Just if you are interest in the problem that prompted this issue / request for enhancement. I have a signal sampled at 102.4 Hz with a jitter that causes deviations of about ±0.05 Hz. I have timestamps (stored as timedelta64) at 𝘪⋅𝛥𝘵 + 𝜀ᵢ ; the modulo arithmetic here is handy to compute the deviation 𝜀ᵢ of the sampling interval with respect to the nominal one. This is not possible directly: I have to convert first the timedelta64 to long integers and then back to timedelta64. Of course I can circumvent the problem in a lot of ways but it would be nice to this in the natural way:
    (𝘪⋅𝛥𝘵 + 𝜀ᵢ) mod 𝛥𝘵

  3. shoyer commented on Oct 6, 2018

    @shoyer
    Member

    A few of us (including myself) have been slowly improving/fixing datetime64, but it isn't as widely used as most other parts of NumPy. We don't need convincing to add this feature/fix -- rather someone like @tylerjereddy might need convincing to add this himself :).

  4. tylerjereddy commented on Oct 6, 2018

    @tylerjereddy
    Contributor

    Ok, I tried to help with #11890, #11873, #11852, #11851, and #11841, which have maybe lingered a bit.

    I'll take a look here then

  5. shoyer commented on Oct 6, 2018

    @shoyer
    Member

    @tylerjereddy feel free to ping me on review for datetime64 issues

  6. miccoli commented on Oct 6, 2018

    @miccoli
    ContributorAuthor

    a small note:

    Let me express my profound gratitude to all numpy contributors, for this invaluable tool. My intention opening this issue was of course only to signal a perceived incongruence in the current datetime64/timedelta64 semantics/arithmetic, not to overburden the maintainers. Let me know If I can help.

  7. miccoli commented on Oct 31, 2018

    @miccoli
    ContributorAuthor

    I think that #12120 completely addresses the present issue. Can I close the present issue, or there is a reason for keeping it open?

  8. tylerjereddy commented on Oct 31, 2018

    @tylerjereddy
    Contributor

    Yeah, I'll close this then. I think I may still try to follow up with remainder support for one or two other operand types mixed with timedelta64 and I think Eric suggested divmod would be a reasonable follow-up as well, but that can be pursued elsewhere.

  9. miccoli commented on Oct 31, 2018

    @miccoli
    ContributorAuthor

    as what regard mm operands, divmod and // should follow, so that

    divmod(a, b) == (a//b, a%b)
    

    is an invariant.

    thnx again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions