Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: incorrect floored division of negative timedeltas #32522

Description

@adienes

Describe the issue:

floored division // of negative np.timedelta disagrees with both int and datetime.timedelta. this is a ufunc contract violation causing correctness inconsistencies.

Reproduce the code example:

import datetime as dt

import numpy as np

delta = np.timedelta64(-7, "us")
actual = int((delta // 2) / np.timedelta64(1, "us"))      # timedelta64 // int
via_timedelta = int(delta // np.timedelta64(2, "us"))  # timedelta64 // timedelta64
python = dt.timedelta(microseconds=-7) // 2 // dt.timedelta(microseconds=1)

print("expected (floor):        ", -4)
print("timedelta64 // int:      ", actual)
print("timedelta64 // timedelta:", via_timedelta)
print("int64 // int:            ", int(np.int64(-7) // 2), " python timedelta // int:", python)
assert actual == -4, "floor_divide truncated toward zero for timedelta64 // int"

Error message:

Python and NumPy Versions:

import sys, numpy; print(numpy.version); print(sys.version)
2.5.2
3.14.7 (main, Sep 1 2026, 14:05:28) [Clang 22.1.3 ]

Runtime Environment:

No response

How does this issue affect you or how did you find it:

generative AI assisted identifying this bug (GPT-5.6 Sol)

Activity

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