From 3419deb451fc10d292c0b0dca08df7ce9d722b0f Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Thu, 14 Mar 2024 13:11:50 -0400 Subject: [PATCH 1/5] improve accuracy of to_pytimedelta --- pandas/_libs/tslibs/timedeltas.pyx | 5 ++++- pandas/tests/scalar/timedelta/test_timedelta.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index f6c69cf6d3875..2bf1a28ed5a84 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1306,7 +1306,10 @@ cdef class _Timedelta(timedelta): datetime.timedelta(days=3) """ if self._creso == NPY_FR_ns: - return timedelta(microseconds=int(self._value) / 1000) + us, remainder = divmod(self._value, 1000) + if remainder > 500: + us += 1 + return timedelta(microseconds=us) # TODO(@WillAyd): is this the right way to use components? self._ensure_components() diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index d4398f66e6f89..a243e640df520 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -664,3 +664,10 @@ def test_timedelta_attribute_precision(): result += td.nanoseconds expected = td._value assert result == expected + + +def test_to_pytimedelta_large_values(): + td = Timedelta(1152921504609987414, unit="ns") + result = td.to_pytimedelta() + expected = timedelta(days=13343, seconds=86304, microseconds=609987) + assert result == expected From 779be5b42c6310a033363a89dee42757f574741b Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Thu, 14 Mar 2024 13:18:51 -0400 Subject: [PATCH 2/5] f --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 2bf1a28ed5a84..3855151a82e79 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1307,7 +1307,7 @@ cdef class _Timedelta(timedelta): """ if self._creso == NPY_FR_ns: us, remainder = divmod(self._value, 1000) - if remainder > 500: + if remainder >= 500: us += 1 return timedelta(microseconds=us) From 73af5fd4de229bbb874513fe6e36821c08711a85 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Thu, 14 Mar 2024 17:51:34 -0400 Subject: [PATCH 3/5] f --- pandas/tests/scalar/timedelta/test_timedelta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index a243e640df520..9c8cec264fe75 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -667,7 +667,7 @@ def test_timedelta_attribute_precision(): def test_to_pytimedelta_large_values(): - td = Timedelta(1152921504609987414, unit="ns") + td = Timedelta(1152921504609987375, unit="ns") result = td.to_pytimedelta() expected = timedelta(days=13343, seconds=86304, microseconds=609987) assert result == expected From 8680ee534895f91889d0df62b0c8df747bd9a6d6 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Wed, 20 Mar 2024 14:06:51 -0400 Subject: [PATCH 4/5] whatsnew --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 7263329d2e53b..d90b2cf333215 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -309,7 +309,7 @@ Datetimelike Timedelta ^^^^^^^^^ -- +- Accuracy improvement in :method:`Timedelta.to_pytimedelta` to round microseconds consistently for large nanosecond based Timedelta (:issue:`57841`) - Timezones From 1c83d027f9f1f46c124b2e962c8261968843168d Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Wed, 20 Mar 2024 14:57:05 -0400 Subject: [PATCH 5/5] f --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index ce71bc86dcbd2..a08c604965602 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -312,7 +312,7 @@ Datetimelike Timedelta ^^^^^^^^^ -- Accuracy improvement in :method:`Timedelta.to_pytimedelta` to round microseconds consistently for large nanosecond based Timedelta (:issue:`57841`) +- Accuracy improvement in :meth:`Timedelta.to_pytimedelta` to round microseconds consistently for large nanosecond based Timedelta (:issue:`57841`) - Timezones