Skip to content

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

Open
@BvB93

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 ]

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