Skip to content

BUG: timedelta64 does not support boolean (floor-)division #18445

Closed

Description

@BvB93

Despite booleans being castable to timedelta, the np.true_divide and np.floor_divide ufuncs
currently do not support divisions between np.timedelta64 and np.bool_.
Note that timedelta / integer operations do work as expected.

This is inconsistent w.r.t. the rest of the ufuncs, which support either no timedelta casting or the full range of casts:

  • np.add and np.multiply support the full range of timedelta casts,
    i.e. both timedelta + timedelta, timedelta + integer and timedelta + bool operations are accepted.
  • np.remainder (ENH: timedelta64 mod and floordiv #16195) and np.nat do not support timedelta casting at all.

Reproducing code example:

Division:

In [1]: import numpy as np

In [2]: b = np.array([True])
   ...: u = np.array([1], dtype=np.uint)
   ...: i = np.array([1])
   ...: m = np.array([1], dtype="timedelta64[s]")

In [3]: m / i
Out[3]: array([1], dtype='timedelta64[s]')

In [4]: m / u
Out[4]: array([1], dtype='timedelta64[s]')

In [5]: m / b
---------------------------------------------------------------------------
UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('<m8[s]') and dtype('bool')

Floor division:

In [6]: m // i
Out[6]: array([1], dtype='timedelta64[s]')

In [7]: m // u
Out[7]: array([1], dtype='timedelta64[s]')

In [8]: m // b
---------------------------------------------------------------------------
UFuncTypeError: ufunc 'floor_divide' cannot use operands with types dtype('<m8[s]') and dtype('bool')

NumPy/Python version information:

In [9]: import sys, numpy; print(numpy.__version__, sys.version)
1.21.0.dev0+807.g121b6097a 3.8.5 (default, Sep  4 2020, 02:22:02) 
[Clang 10.0.0 ]

Activity

  1. seberg commented on Feb 19, 2021

    @seberg
    Member

    @BvB93 do we really want that? Note that they can cast "safely", but they do not promote in general (or at least I started deprecating that 😈. In my world view, that would mean, I would rather make that + ufunc here use "promotion" (not casting), which would effectively deprecate the above.
    But, I admit that timedelta64 + integer is probably intentionally allowed, even if dubious.

    Saying all of that, timedelta64 / boolean is actually like timedelta64 / integer which is valid (and not the same as timdelta64 / timedelta64 due to the unit!). Since we allow upcasts of boolean to integers everywhere, division specifically should probably be just allowed.

  2. BvB93 commented on Feb 19, 2021

    @BvB93
    MemberAuthor

    @BvB93 do we really want that?

    Well, what I'm mostly interested in is consistency, and np.floor_divide and np.true_divide are currently (as far as timedeltas + bool ops are concerned) inconsistent w.r.t. to all other ufuncs, an inconsistency that, in my opinion, appears to be unjustified.

    I'm a bit less concerned with how this could be resolved, be it either via allowing or dissallowing mixed timdelta/boolean operations in general. Though as you've noted in the division example, I don't see any harm in allowing it.

  3. mhafizazh commented on Sep 10, 2026

    @mhafizazh
    Contributor

    @ngoldbaum should we close this?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions