Skip to content

Commit 052ad27

Browse files
mroeschkeYi Wei
authored and
Yi Wei
committed
BUG: dt.round with equal/higher freq resolution would not noop (pandas-dev#52841)
1 parent b4899fa commit 052ad27

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Bug fixes
2727
~~~~~~~~~
2828
- Bug in :attr:`Series.dt.days` that would overflow ``int32`` number of days (:issue:`52391`)
2929
- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`)
30+
- Bug in :func:`Series.dt.round` when passing a ``freq`` of equal or higher resolution compared to the :class:`Series` would raise a ``ZeroDivisionError`` (:issue:`52761`)
3031
- Bug in :func:`Series.median` with :class:`ArrowDtype` returning an approximate median (:issue:`52679`)
3132
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on categorical dtypes (:issue:`49889`)
3233
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on large string dtypes (:issue:`52795`)

pandas/core/arrays/datetimelike.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2050,8 +2050,12 @@ def _round(self, freq, mode, ambiguous, nonexistent):
20502050

20512051
values = self.view("i8")
20522052
values = cast(np.ndarray, values)
2053-
nanos = to_offset(freq).nanos # raises on non-fixed frequencies
2054-
nanos = delta_to_nanoseconds(to_offset(freq), self._creso)
2053+
offset = to_offset(freq)
2054+
offset.nanos # raises on non-fixed frequencies
2055+
nanos = delta_to_nanoseconds(offset, self._creso)
2056+
if nanos == 0:
2057+
# GH 52761
2058+
return self
20552059
result_i8 = round_nsint64(values, mode, nanos)
20562060
result = self._maybe_mask_results(result_i8, fill_value=iNaT)
20572061
result = result.view(self._ndarray.dtype)

pandas/tests/series/accessors/test_dt_accessor.py

+11
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ def test_dt_round_tz_nonexistent(self, method, ts_str, freq):
385385
with pytest.raises(pytz.NonExistentTimeError, match="2018-03-11 02:00:00"):
386386
getattr(ser.dt, method)(freq, nonexistent="raise")
387387

388+
@pytest.mark.parametrize("freq", ["ns", "U", "1000U"])
389+
def test_dt_round_nonnano_higher_resolution_no_op(self, freq):
390+
# GH 52761
391+
ser = Series(
392+
["2020-05-31 08:00:00", "2000-12-31 04:00:05", "1800-03-14 07:30:20"],
393+
dtype="datetime64[ms]",
394+
)
395+
expected = ser.copy()
396+
result = ser.dt.round(freq)
397+
tm.assert_series_equal(result, expected)
398+
388399
def test_dt_namespace_accessor_categorical(self):
389400
# GH 19468
390401
dti = DatetimeIndex(["20171111", "20181212"]).repeat(2)

0 commit comments

Comments
 (0)