From 3e107f3f9963ab8c0e7c180b7a6e6eee419bf646 Mon Sep 17 00:00:00 2001 From: Hadi Abdi Khojasteh Date: Sat, 16 Sep 2023 15:52:24 +0000 Subject: [PATCH 1/5] Fix rolling microseconds for sum --- doc/source/conf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index accbff596b12d..8b060e40aedd8 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -172,7 +172,9 @@ import pandas # isort:skip # version = '%s r%s' % (pandas.__version__, svn_version()) -version = str(pandas.__version__) +# version = str(pandas.__version__) + +version = str(pandas.__version__).split("+")[0] # The full version, including alpha/beta/rc tags. release = version From 15abeb528b0ab44a18348f6c6059d5f0c3f25791 Mon Sep 17 00:00:00 2001 From: Hadi Abdi Khojasteh Date: Sat, 16 Sep 2023 16:26:13 +0000 Subject: [PATCH 2/5] - adding a test for rounding sum --- pandas/core/window/rolling.py | 3 +++ pandas/tests/window/test_rolling.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index becbba703f92c..214d309956e51 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1884,6 +1884,9 @@ def _validate(self): else: self._win_freq_i8 = freq.nanos + if self._on.dtype == "M8[us]": + self._win_freq_i8 /= 1000 + # 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..f30427a57d62f 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1950,3 +1950,24 @@ 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) + + +def test_rolling_rolling_sum_window_microseconds_confilict_timestamp(): + # GH#55106 + df_time = DataFrame( + {"B": [0, 1, 2, 4, 5, 6]}, + index=[ + Timestamp("20130101 09:00:00"), + Timestamp("20130101 09:00:02"), + Timestamp("20130101 09:00:03"), + Timestamp("20130101 09:00:06"), + Timestamp("20130101 09:00:07"), + Timestamp("20130101 09:00:08"), + ], + ) + sum_in_nanosecs = df_time.rolling("1s").sum() + # micro seconds / milliseconds should not breaks the correct rolling + df_time.index = df_time.index.as_unit("us") + sum_in_microsecs = df_time.rolling("1s").sum() + sum_in_microsecs.index = sum_in_microsecs.index.as_unit("ns") + tm.assert_frame_equal(sum_in_nanosecs, sum_in_microsecs) From cc8a96a94cfef41136ffa9c5e310f8ca442ef817 Mon Sep 17 00:00:00 2001 From: Hadi Abdi Khojasteh Date: Thu, 21 Sep 2023 12:03:22 +0000 Subject: [PATCH 3/5] Revert "Fix rolling microseconds for sum" This reverts commit 3e107f3f9963ab8c0e7c180b7a6e6eee419bf646. --- doc/source/conf.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index 8b060e40aedd8..accbff596b12d 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -172,9 +172,7 @@ import pandas # isort:skip # version = '%s r%s' % (pandas.__version__, svn_version()) -# version = str(pandas.__version__) - -version = str(pandas.__version__).split("+")[0] +version = str(pandas.__version__) # The full version, including alpha/beta/rc tags. release = version From a95655d071812d09bb6f865a30448f288800c68a Mon Sep 17 00:00:00 2001 From: Hadi Abdi Khojasteh Date: Thu, 21 Sep 2023 12:03:41 +0000 Subject: [PATCH 4/5] Revert "- adding a test for rounding sum" This reverts commit 15abeb528b0ab44a18348f6c6059d5f0c3f25791. --- pandas/core/window/rolling.py | 3 --- pandas/tests/window/test_rolling.py | 21 --------------------- 2 files changed, 24 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 214d309956e51..becbba703f92c 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1884,9 +1884,6 @@ def _validate(self): else: self._win_freq_i8 = freq.nanos - if self._on.dtype == "M8[us]": - self._win_freq_i8 /= 1000 - # 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 f30427a57d62f..3fe922539780d 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -1950,24 +1950,3 @@ 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) - - -def test_rolling_rolling_sum_window_microseconds_confilict_timestamp(): - # GH#55106 - df_time = DataFrame( - {"B": [0, 1, 2, 4, 5, 6]}, - index=[ - Timestamp("20130101 09:00:00"), - Timestamp("20130101 09:00:02"), - Timestamp("20130101 09:00:03"), - Timestamp("20130101 09:00:06"), - Timestamp("20130101 09:00:07"), - Timestamp("20130101 09:00:08"), - ], - ) - sum_in_nanosecs = df_time.rolling("1s").sum() - # micro seconds / milliseconds should not breaks the correct rolling - df_time.index = df_time.index.as_unit("us") - sum_in_microsecs = df_time.rolling("1s").sum() - sum_in_microsecs.index = sum_in_microsecs.index.as_unit("ns") - tm.assert_frame_equal(sum_in_nanosecs, sum_in_microsecs) From 68297ac09767fd38224bc5396199df75fa9a158c Mon Sep 17 00:00:00 2001 From: Hadi Abdi Khojasteh Date: Fri, 22 Sep 2023 12:35:37 +0000 Subject: [PATCH 5/5] Rolling fixed by adding the validation on the unit Rolling max tests added --- pandas/core/window/rolling.py | 7 +++++++ pandas/tests/window/test_rolling.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) 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)