From cbe277568c42c122a69fdad012e98580a0bb3d71 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Tue, 14 May 2024 12:20:26 -0500 Subject: [PATCH] Fix `Index.repeat` for `datetime64` types (#15722) Fixes: #15720 This PR fixes `Index.repeat` where the `freq` of `DatetimeIndex` needs to be reset. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Matthew Roeschke (https://github.com/mroeschke) URL: https://github.com/rapidsai/cudf/pull/15722 --- python/cudf/cudf/core/index.py | 5 +++++ python/cudf/cudf/core/indexed_frame.py | 5 ++++- python/cudf/cudf/tests/test_index.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 0710f0f5c42..209e582e5d6 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -2373,6 +2373,11 @@ def tz_convert(self, tz: str | None): result_col = self._column.tz_convert(tz) return DatetimeIndex._from_data({self.name: result_col}) + def repeat(self, repeats, axis=None): + res = super().repeat(repeats, axis=axis) + res._freq = None + return res + class TimedeltaIndex(Index): """ diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index dc261707867..904cd0c69c2 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -4871,13 +4871,16 @@ def repeat(self, repeats, axis=None): 1 2 dtype: int64 """ - return self._from_columns_like_self( + res = self._from_columns_like_self( Frame._repeat( [*self._index._data.columns, *self._columns], repeats, axis ), self._column_names, self._index_names, ) + if isinstance(res.index, cudf.DatetimeIndex): + res.index._freq = None + return res def astype( self, diff --git a/python/cudf/cudf/tests/test_index.py b/python/cudf/cudf/tests/test_index.py index 8b7ee1dccf8..8e7532d044d 100644 --- a/python/cudf/cudf/tests/test_index.py +++ b/python/cudf/cudf/tests/test_index.py @@ -3252,3 +3252,17 @@ def test_Index_init_with_nans(): assert gi.dtype == np.dtype("float64") pi = pd.Index([1, 2, 3, np.nan]) assert_eq(pi, gi) + + +def test_index_datetime_repeat(): + gidx = cudf.date_range("2021-01-01", periods=3, freq="D") + pidx = gidx.to_pandas() + + actual = gidx.repeat(5) + expected = pidx.repeat(5) + + assert_eq(actual, expected) + + actual = gidx.to_frame().repeat(5) + + assert_eq(actual.index, expected)