From ad5784bf75baff65a7eeb7f0aeb5883eb217dde0 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Sat, 29 Apr 2023 08:46:02 -0700 Subject: [PATCH] Backport PR #52982: BUG: Have non nano rounding noop return copy --- pandas/core/arrays/datetimelike.py | 2 +- pandas/tests/series/accessors/test_dt_accessor.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 843e9be6de14a..8545cd1499b5e 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -2010,7 +2010,7 @@ def _round(self, freq, mode, ambiguous, nonexistent): nanos = delta_to_nanoseconds(offset, self._creso) if nanos == 0: # GH 52761 - return self + return self.copy() result_i8 = round_nsint64(values, mode, nanos) result = self._maybe_mask_results(result_i8, fill_value=iNaT) result = result.view(self._ndarray.dtype) diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 74371830f3a19..e211bc233b9cf 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -392,6 +392,8 @@ def test_dt_round_nonnano_higher_resolution_no_op(self, freq): result = ser.dt.round(freq) tm.assert_series_equal(result, expected) + assert not np.shares_memory(ser.array._ndarray, result.array._ndarray) + def test_dt_namespace_accessor_categorical(self): # GH 19468 dti = DatetimeIndex(["20171111", "20181212"]).repeat(2)