File tree 3 files changed +12
-2
lines changed
3 files changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -319,7 +319,7 @@ Datetimelike
319
319
320
320
Timedelta
321
321
^^^^^^^^^
322
- -
322
+ - Accuracy improvement in :meth: ` Timedelta.to_pytimedelta ` to round microseconds consistently for large nanosecond based Timedelta ( :issue: ` 57841 `)
323
323
-
324
324
325
325
Timezones
Original file line number Diff line number Diff line change @@ -1376,7 +1376,10 @@ cdef class _Timedelta(timedelta):
1376
1376
datetime.timedelta(days=3)
1377
1377
"""
1378
1378
if self ._creso == NPY_FR_ns:
1379
- return timedelta(microseconds = int (self ._value) / 1000 )
1379
+ us, remainder = divmod (self ._value, 1000 )
1380
+ if remainder >= 500 :
1381
+ us += 1
1382
+ return timedelta(microseconds = us)
1380
1383
1381
1384
# TODO(@WillAyd): is this the right way to use components?
1382
1385
self ._ensure_components()
Original file line number Diff line number Diff line change @@ -665,3 +665,10 @@ def test_timedelta_attribute_precision():
665
665
result += td .nanoseconds
666
666
expected = td ._value
667
667
assert result == expected
668
+
669
+
670
+ def test_to_pytimedelta_large_values ():
671
+ td = Timedelta (1152921504609987375 , unit = "ns" )
672
+ result = td .to_pytimedelta ()
673
+ expected = timedelta (days = 13343 , seconds = 86304 , microseconds = 609987 )
674
+ assert result == expected
You can’t perform that action at this time.
0 commit comments