From 184ad311c51c38eb6f60f6dcb8a578079fb36176 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 20 Sep 2025 03:08:06 +0530 Subject: [PATCH 01/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 11 ++++++++++- pandas/tests/arrays/test_datetimes.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 38be038efcaa5..ebcf5f228e4c4 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -817,7 +817,16 @@ def _add_offset(self, offset: BaseOffset) -> Self: result = type(self)._from_sequence(res_values, dtype=self.dtype) else: - result = type(self)._simple_new(res_values, dtype=res_values.dtype) + units = ["ns", "us", "ms", "s", "m", "h", "D"] + res_unit = self.unit + if hasattr(offset, "offset"): + offset_unit = Timedelta(offset.offset).unit + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] + dtype = tz_to_dtype(self.tz, unit=res_unit) + result = type(self)._simple_new(res_values, dtype=dtype) + if offset.normalize: result = result.normalize() result._freq = None diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 199e3572732a0..6cacc73ea3eda 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -844,3 +844,20 @@ def test_factorize_sort_without_freq(): tda = dta - dta[0] with pytest.raises(NotImplementedError, match=msg): tda.factorize(sort=True) + + +def test_dt64_non_nano_offset_no_rounding(): + # GH#56586 + dti = pd.date_range("2016-01-01", periods=3, unit="s") + offset = pd.offsets.CustomBusinessDay(offset=pd.Timedelta("1ms")) + result = dti + offset + + assert result.dtype == np.dtype("datetime64[ms]") + expected = pd.DatetimeIndex( + [ + pd.Timestamp("2016-01-02 00:00:00.001"), + pd.Timestamp("2016-01-03 00:00:00.001"), + pd.Timestamp("2016-01-04 00:00:00.001"), + ] + ) + assert all(result == expected) From 0f088f5109b0920ae6c537fe73004dd1daa0b061 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 4 Oct 2025 23:21:07 +0530 Subject: [PATCH 02/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 25 +++++++++++++++++++------ pandas/tests/arrays/test_datetimes.py | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index ebcf5f228e4c4..ffb2145ace390 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -817,14 +817,27 @@ def _add_offset(self, offset: BaseOffset) -> Self: result = type(self)._from_sequence(res_values, dtype=self.dtype) else: - units = ["ns", "us", "ms", "s", "m", "h", "D"] + units = [ + "ns", + "us", + "ms", + "s", + ] res_unit = self.unit - if hasattr(offset, "offset"): - offset_unit = Timedelta(offset.offset).unit - idx_self = units.index(self.unit) - idx_offset = units.index(offset_unit) - res_unit = units[min(idx_self, idx_offset)] + # Only try to adjust unit if both units are recognized + try: + if hasattr(offset, "offset"): + offset_td = Timedelta(offset.offset) + offset_unit = offset_td.unit + if self.unit in units and offset_unit in units: + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] + except Exception: + res_unit = self.unit dtype = tz_to_dtype(self.tz, unit=res_unit) + if res_values.dtype != f"datetime64[{res_unit}]": + res_values = res_values.astype(f"datetime64[{res_unit}]") result = type(self)._simple_new(res_values, dtype=dtype) if offset.normalize: diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 6cacc73ea3eda..505b49f7b7fb4 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -860,4 +860,4 @@ def test_dt64_non_nano_offset_no_rounding(): pd.Timestamp("2016-01-04 00:00:00.001"), ] ) - assert all(result == expected) + tm.assert_index_equal(result, expected) From f8e31d45e24f3545ef756f4be6c78fa21f405949 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Thu, 16 Oct 2025 20:13:26 +0530 Subject: [PATCH 03/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 34 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index ffb2145ace390..7dc4f74451c44 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -64,6 +64,7 @@ ) from pandas.core.dtypes.missing import isna +from pandas import Timedelta from pandas.core.arrays import datetimelike as dtl from pandas.core.arrays._ranges import generate_regular_range import pandas.core.common as com @@ -93,7 +94,6 @@ from pandas import ( DataFrame, - Timedelta, ) from pandas.core.arrays import PeriodArray @@ -824,27 +824,27 @@ def _add_offset(self, offset: BaseOffset) -> Self: "s", ] res_unit = self.unit - # Only try to adjust unit if both units are recognized - try: - if hasattr(offset, "offset"): - offset_td = Timedelta(offset.offset) - offset_unit = offset_td.unit - if self.unit in units and offset_unit in units: - idx_self = units.index(self.unit) - idx_offset = units.index(offset_unit) - res_unit = units[min(idx_self, idx_offset)] - except Exception: - res_unit = self.unit - dtype = tz_to_dtype(self.tz, unit=res_unit) - if res_values.dtype != f"datetime64[{res_unit}]": - res_values = res_values.astype(f"datetime64[{res_unit}]") - result = type(self)._simple_new(res_values, dtype=dtype) + if hasattr(offset, "offset"): + offset_td = Timedelta(offset.offset) + offset_unit = offset_td.unit + if self.unit in units and offset_unit in units: + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] + dtype_naive = np.dtype(f"datetime64[{res_unit}]") + if res_values.dtype != dtype_naive: + res_values = res_values.astype(dtype_naive) + result = type(self)._simple_new(res_values, dtype=dtype_naive) if offset.normalize: result = result.normalize() result._freq = None - if self.tz is not None: + if ( + self.tz is not None + and getattr(result.dtype, "tz", None) is None + and res_unit == "ns" + ): result = result.tz_localize(self.tz) return result From 259a5e4e34cddd42bc2b3e63a44b69d06adc9072 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 17 Oct 2025 04:52:36 +0530 Subject: [PATCH 04/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 7dc4f74451c44..23e3d2fc1836a 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -831,10 +831,8 @@ def _add_offset(self, offset: BaseOffset) -> Self: idx_self = units.index(self.unit) idx_offset = units.index(offset_unit) res_unit = units[min(idx_self, idx_offset)] - dtype_naive = np.dtype(f"datetime64[{res_unit}]") - if res_values.dtype != dtype_naive: - res_values = res_values.astype(dtype_naive) - result = type(self)._simple_new(res_values, dtype=dtype_naive) + result = type(self)._simple_new(res_values, dtype=res_values.dtype) + result = result.as_unit(res_unit) if offset.normalize: result = result.normalize() From 9c95525e43b1f9f1387247c84891401c571bd3b0 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Wed, 29 Oct 2025 14:47:12 +0530 Subject: [PATCH 05/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 23e3d2fc1836a..b0b4118aa467e 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -827,10 +827,9 @@ def _add_offset(self, offset: BaseOffset) -> Self: if hasattr(offset, "offset"): offset_td = Timedelta(offset.offset) offset_unit = offset_td.unit - if self.unit in units and offset_unit in units: - idx_self = units.index(self.unit) - idx_offset = units.index(offset_unit) - res_unit = units[min(idx_self, idx_offset)] + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] result = type(self)._simple_new(res_values, dtype=res_values.dtype) result = result.as_unit(res_unit) @@ -838,11 +837,7 @@ def _add_offset(self, offset: BaseOffset) -> Self: result = result.normalize() result._freq = None - if ( - self.tz is not None - and getattr(result.dtype, "tz", None) is None - and res_unit == "ns" - ): + if self.tz is not None: result = result.tz_localize(self.tz) return result From dcf4dfc956ea92f7624aa79759824ffb24b4d982 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Wed, 29 Oct 2025 15:17:57 +0530 Subject: [PATCH 06/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index b0b4118aa467e..a2ad694eb6674 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -28,6 +28,7 @@ NaT, NaTType, Resolution, + Timedelta, Timestamp, astype_overflowsafe, fields, @@ -64,7 +65,6 @@ ) from pandas.core.dtypes.missing import isna -from pandas import Timedelta from pandas.core.arrays import datetimelike as dtl from pandas.core.arrays._ranges import generate_regular_range import pandas.core.common as com From 4a4d9766869d00b7e3da8a4c8f73fcb43172888f Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Tue, 11 Nov 2025 20:00:14 +0530 Subject: [PATCH 07/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index a2ad694eb6674..35f774a0965c9 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -824,12 +824,13 @@ def _add_offset(self, offset: BaseOffset) -> Self: "s", ] res_unit = self.unit - if hasattr(offset, "offset"): + if hasattr(offset, "offset") and offset.offset is not None: offset_td = Timedelta(offset.offset) - offset_unit = offset_td.unit - idx_self = units.index(self.unit) - idx_offset = units.index(offset_unit) - res_unit = units[min(idx_self, idx_offset)] + if offset_td.value != 0: + offset_unit = offset_td.unit + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] result = type(self)._simple_new(res_values, dtype=res_values.dtype) result = result.as_unit(res_unit) From ba029d11d2cc1e9d4b88e8a576610940c3697454 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Thu, 13 Nov 2025 15:38:49 +0530 Subject: [PATCH 08/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 35f774a0965c9..c897ee717448e 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -71,6 +71,8 @@ from pandas.tseries.frequencies import get_period_alias from pandas.tseries.offsets import ( + BusinessHour, + CustomBusinessDay, Day, Tick, ) @@ -825,12 +827,13 @@ def _add_offset(self, offset: BaseOffset) -> Self: ] res_unit = self.unit if hasattr(offset, "offset") and offset.offset is not None: - offset_td = Timedelta(offset.offset) - if offset_td.value != 0: - offset_unit = offset_td.unit - idx_self = units.index(self.unit) - idx_offset = units.index(offset_unit) - res_unit = units[min(idx_self, idx_offset)] + if isinstance(offset, (CustomBusinessDay, BusinessHour)): + offset_td = Timedelta(offset.offset) + if offset_td.value != 0: + offset_unit = offset_td.unit + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] result = type(self)._simple_new(res_values, dtype=res_values.dtype) result = result.as_unit(res_unit) From e27110a10295b7fd3be9b96c2f551e9e3b60360e Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Thu, 13 Nov 2025 16:09:19 +0530 Subject: [PATCH 09/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index c897ee717448e..18673a2c837b5 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -46,6 +46,7 @@ tzconversion, ) from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit +from pandas._libs.tslibs.offsets import DateOffset from pandas.errors import PerformanceWarning from pandas.util._exceptions import find_stack_level from pandas.util._validators import validate_inclusive @@ -71,8 +72,6 @@ from pandas.tseries.frequencies import get_period_alias from pandas.tseries.offsets import ( - BusinessHour, - CustomBusinessDay, Day, Tick, ) @@ -827,7 +826,7 @@ def _add_offset(self, offset: BaseOffset) -> Self: ] res_unit = self.unit if hasattr(offset, "offset") and offset.offset is not None: - if isinstance(offset, (CustomBusinessDay, BusinessHour)): + if not isinstance(offset, DateOffset): offset_td = Timedelta(offset.offset) if offset_td.value != 0: offset_unit = offset_td.unit From 4f2dced5bf60874aba0e4a83302b0eec6f7f879e Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 14 Nov 2025 16:16:37 +0530 Subject: [PATCH 10/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 18673a2c837b5..2416cc0438ac8 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -46,7 +46,6 @@ tzconversion, ) from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit -from pandas._libs.tslibs.offsets import DateOffset from pandas.errors import PerformanceWarning from pandas.util._exceptions import find_stack_level from pandas.util._validators import validate_inclusive @@ -72,6 +71,7 @@ from pandas.tseries.frequencies import get_period_alias from pandas.tseries.offsets import ( + DateOffset as TSeriesDateOffset, Day, Tick, ) @@ -826,7 +826,7 @@ def _add_offset(self, offset: BaseOffset) -> Self: ] res_unit = self.unit if hasattr(offset, "offset") and offset.offset is not None: - if not isinstance(offset, DateOffset): + if not isinstance(offset, TSeriesDateOffset): offset_td = Timedelta(offset.offset) if offset_td.value != 0: offset_unit = offset_td.unit From c6c2fadc6fd93f5d2db912ac5a2ebb52c4204230 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 14 Nov 2025 17:01:39 +0530 Subject: [PATCH 11/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 2416cc0438ac8..6910fe172bdae 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -825,14 +825,17 @@ def _add_offset(self, offset: BaseOffset) -> Self: "s", ] res_unit = self.unit - if hasattr(offset, "offset") and offset.offset is not None: - if not isinstance(offset, TSeriesDateOffset): - offset_td = Timedelta(offset.offset) - if offset_td.value != 0: - offset_unit = offset_td.unit - idx_self = units.index(self.unit) - idx_offset = units.index(offset_unit) - res_unit = units[min(idx_self, idx_offset)] + if isinstance(offset, TSeriesDateOffset): + micro = offset.kwds.get("microseconds", 0) + if micro and self.unit != "ns": + res_unit = "us" + elif hasattr(offset, "offset") and offset.offset is not None: + offset_td = Timedelta(offset.offset) + if offset_td.value != 0: + offset_unit = offset_td.unit + idx_self = units.index(self.unit) + idx_offset = units.index(offset_unit) + res_unit = units[min(idx_self, idx_offset)] result = type(self)._simple_new(res_values, dtype=res_values.dtype) result = result.as_unit(res_unit) From 00ad36cf24a478fdaa3ccc0352850494d385bb79 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 21 Nov 2025 12:42:10 +0530 Subject: [PATCH 12/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 6910fe172bdae..04dff842165b6 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -829,7 +829,11 @@ def _add_offset(self, offset: BaseOffset) -> Self: micro = offset.kwds.get("microseconds", 0) if micro and self.unit != "ns": res_unit = "us" - elif hasattr(offset, "offset") and offset.offset is not None: + if ( + hasattr(offset, "offset") + and offset.offset is not None + and not isinstance(offset, Tick) + ): offset_td = Timedelta(offset.offset) if offset_td.value != 0: offset_unit = offset_td.unit From 87cbc4225b3da93d7f5cee2791c56f2432bdb0eb Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 09:21:23 +0530 Subject: [PATCH 13/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 04dff842165b6..ab8dd349a0033 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -71,7 +71,8 @@ from pandas.tseries.frequencies import get_period_alias from pandas.tseries.offsets import ( - DateOffset as TSeriesDateOffset, + BusinessDay, + DateOffset, Day, Tick, ) @@ -825,14 +826,18 @@ def _add_offset(self, offset: BaseOffset) -> Self: "s", ] res_unit = self.unit - if isinstance(offset, TSeriesDateOffset): + if type(offset) is DateOffset: + nano = offset.kwds.get("nanoseconds", 0) micro = offset.kwds.get("microseconds", 0) - if micro and self.unit != "ns": + if nano: + res_unit = "ns" + elif micro and self.unit != "ns": res_unit = "us" if ( hasattr(offset, "offset") and offset.offset is not None and not isinstance(offset, Tick) + and type(offset) is not BusinessDay ): offset_td = Timedelta(offset.offset) if offset_td.value != 0: From e4b91c441f9b61756e6121a927d00119977a59ba Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 09:55:24 +0530 Subject: [PATCH 14/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index ab8dd349a0033..fda122c39cacf 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -72,6 +72,8 @@ from pandas.tseries.frequencies import get_period_alias from pandas.tseries.offsets import ( BusinessDay, + BusinessHour, + CustomBusinessHour, DateOffset, Day, Tick, @@ -838,6 +840,8 @@ def _add_offset(self, offset: BaseOffset) -> Self: and offset.offset is not None and not isinstance(offset, Tick) and type(offset) is not BusinessDay + and type(offset) is not BusinessHour + and type(offset) is not CustomBusinessHour ): offset_td = Timedelta(offset.offset) if offset_td.value != 0: From b6f945dc370b5c59ab2cb2973a4f792310e98aeb Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 10:27:19 +0530 Subject: [PATCH 15/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index fda122c39cacf..101757071f865 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -71,9 +71,6 @@ from pandas.tseries.frequencies import get_period_alias from pandas.tseries.offsets import ( - BusinessDay, - BusinessHour, - CustomBusinessHour, DateOffset, Day, Tick, @@ -839,9 +836,6 @@ def _add_offset(self, offset: BaseOffset) -> Self: hasattr(offset, "offset") and offset.offset is not None and not isinstance(offset, Tick) - and type(offset) is not BusinessDay - and type(offset) is not BusinessHour - and type(offset) is not CustomBusinessHour ): offset_td = Timedelta(offset.offset) if offset_td.value != 0: From 1b571f7e6b1eab6ae17312e6c4f1abdb9141f16a Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 10:48:42 +0530 Subject: [PATCH 16/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/tests/arrays/test_datetimes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 505b49f7b7fb4..98aa1a5f9c245 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -846,6 +846,10 @@ def test_factorize_sort_without_freq(): tda.factorize(sort=True) +@pytest.mark.filterwarnings( + "ignore:Non-vectorized DateOffset being applied to Series or DatetimeIndex:" + "PerformanceWarning" +) def test_dt64_non_nano_offset_no_rounding(): # GH#56586 dti = pd.date_range("2016-01-01", periods=3, unit="s") From 67221527e8e5ab3dc04571eb02e47dee3462d9ff Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 11:06:02 +0530 Subject: [PATCH 17/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/tests/arrays/test_datetimes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/arrays/test_datetimes.py b/pandas/tests/arrays/test_datetimes.py index 98aa1a5f9c245..27d694e1d7813 100644 --- a/pandas/tests/arrays/test_datetimes.py +++ b/pandas/tests/arrays/test_datetimes.py @@ -847,8 +847,7 @@ def test_factorize_sort_without_freq(): @pytest.mark.filterwarnings( - "ignore:Non-vectorized DateOffset being applied to Series or DatetimeIndex:" - "PerformanceWarning" + "ignore:Non-vectorized DateOffset being applied to Series or DatetimeIndex" ) def test_dt64_non_nano_offset_no_rounding(): # GH#56586 From 3f5af7457f0771406c14f201b38343433bd749d3 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 12:01:41 +0530 Subject: [PATCH 18/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 101757071f865..df72337d67d48 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -815,6 +815,12 @@ def _add_offset(self, offset: BaseOffset) -> Self: stacklevel=find_stack_level(), ) res_values = self.astype("O") + offset + res_values = [ + Timestamp(x) + if isinstance(x, datetime) and not isinstance(x, Timestamp) + else x + for x in res_values + ] result = type(self)._from_sequence(res_values, dtype=self.dtype) else: From 9372a88d22b9f740aa3ce4b8e0d43dfe3d392cf6 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sat, 22 Nov 2025 12:21:40 +0530 Subject: [PATCH 19/19] BUG: Fix dt64[non_nano] + offset rounding --- pandas/core/arrays/datetimes.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index df72337d67d48..101757071f865 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -815,12 +815,6 @@ def _add_offset(self, offset: BaseOffset) -> Self: stacklevel=find_stack_level(), ) res_values = self.astype("O") + offset - res_values = [ - Timestamp(x) - if isinstance(x, datetime) and not isinstance(x, Timestamp) - else x - for x in res_values - ] result = type(self)._from_sequence(res_values, dtype=self.dtype) else: