diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index becbba703f92c..2d5fe7b8112ea 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1884,6 +1884,13 @@ def _validate(self): else: self._win_freq_i8 = freq.nanos + if self._on.dtype == "M8[us]": + self._win_freq_i8 /= 1e3 + elif self._on.dtype == "M8[ms]": + self._win_freq_i8 /= 1e6 + elif self._on.dtype == "M8[s]": + self._win_freq_i8 /= 1e9 + # min_periods must be an integer if self.min_periods is None: self.min_periods = 1 diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 3fe922539780d..9eb2d544392bb 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1950,3 +1950,25 @@ def test_numeric_only_corr_cov_series(kernel, use_arg, numeric_only, dtype): op2 = getattr(rolling2, kernel) expected = op2(*arg2, numeric_only=numeric_only) tm.assert_series_equal(result, expected) + + +@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"]) +def test_rolling_rolling_max_window_nanoseconds_confilict_timestamp(unit): + # GH#55026 + window = Timedelta(days=4) + + ref_dates = date_range("2023-01-01", "2023-01-10", unit="ns") + ref_series = Series(0, index=ref_dates) + ref_series.iloc[0] = 1 + ref_max_series = ref_series.rolling(window).max() + + dates = date_range("2023-01-01", "2023-01-10", unit=unit) + series = Series(0, index=dates) + series.iloc[0] = 1 + max_series = series.rolling(window).max() + + ref_df = DataFrame(ref_max_series) + df = DataFrame(max_series) + df.index = df.index.as_unit("ns") + + tm.assert_frame_equal(ref_df, df)