diff --git a/docs/cudf/source/api_docs/index_objects.rst b/docs/cudf/source/api_docs/index_objects.rst index 30269bb2a72..2a4dd5ff9c8 100644 --- a/docs/cudf/source/api_docs/index_objects.rst +++ b/docs/cudf/source/api_docs/index_objects.rst @@ -280,6 +280,8 @@ Time-specific operations :toctree: api/ DatetimeIndex.round + DatetimeIndex.ceil + DatetimeIndex.floor Conversion ~~~~~~~~~~ diff --git a/python/cudf/cudf/core/frame.py b/python/cudf/cudf/core/frame.py index c0858398492..d6b6a19a610 100644 --- a/python/cudf/cudf/core/frame.py +++ b/python/cudf/cudf/core/frame.py @@ -3673,6 +3673,13 @@ def ceil(self): 3 5.0 dtype: float64 """ + + warnings.warn( + "Series.ceil and DataFrame.ceil are deprecated and will be \ + removed in the future", + DeprecationWarning, + ) + return self._unaryop("ceil") def floor(self): @@ -3705,6 +3712,13 @@ def floor(self): 5 3.0 dtype: float64 """ + + warnings.warn( + "Series.ceil and DataFrame.ceil are deprecated and will be \ + removed in the future", + DeprecationWarning, + ) + return self._unaryop("floor") def scale(self): diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 35b80715cca..63fda21152d 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -1898,6 +1898,68 @@ def _get_dt_field(self, field): def is_boolean(self): return False + def ceil(self, field): + """ + Perform ceil operation on the data to the specified freq. + + Parameters + ---------- + field : str + One of ["D", "H", "T", "min", "S", "L", "ms", "U", "us", "N"]. + Must be a fixed frequency like 'S' (second) not 'ME' (month end). + See `frequency aliases `__ + for more details on these aliases. + + Returns + ------- + DatetimeIndex + Index of the same type for a DatetimeIndex + + Examples + -------- + >>> import cudf + >>> gIndex = cudf.DatetimeIndex(["2020-05-31 08:00:00", + ... "1999-12-31 18:40:00"]) + >>> gIndex.ceil("T") + DatetimeIndex(['2020-05-31 08:00:00', '1999-12-31 18:40:00'], + dtype='datetime64[ns]', freq=None) + """ + out_column = self._values.ceil(field) + + return self.__class__._from_data({self.name: out_column}) + + def floor(self, field): + """ + Perform floor operation on the data to the specified freq. + + Parameters + ---------- + field : str + One of ["D", "H", "T", "min", "S", "L", "ms", "U", "us", "N"]. + Must be a fixed frequency like 'S' (second) not 'ME' (month end). + See `frequency aliases `__ + for more details on these aliases. + + Returns + ------- + DatetimeIndex + Index of the same type for a DatetimeIndex + + Examples + -------- + >>> import cudf + >>> gIndex = cudf.DatetimeIndex(["2020-05-31 08:59:59" + ... ,"1999-12-31 18:44:59"]) + >>> gIndex.floor("T") + DatetimeIndex(['2020-05-31 08:59:00', '1999-12-31 18:44:00'], + dtype='datetime64[ns]', freq=None) + """ + out_column = self._values.floor(field) + + return self.__class__._from_data({self.name: out_column}) + class TimedeltaIndex(GenericIndex): """ diff --git a/python/cudf/cudf/tests/test_index.py b/python/cudf/cudf/tests/test_index.py index c6cf7c4e6f5..ab211616a02 100644 --- a/python/cudf/cudf/tests/test_index.py +++ b/python/cudf/cudf/tests/test_index.py @@ -2470,3 +2470,29 @@ def test_index_type_methods(data, func): assert_eq(False, actual) else: assert_eq(expected, actual) + + +@pytest.mark.parametrize( + "resolution", ["D", "H", "T", "min", "S", "L", "ms", "U", "us", "N"] +) +def test_index_datetime_ceil(resolution): + cuidx = cudf.DatetimeIndex([1000000, 2000000, 3000000, 4000000, 5000000]) + pidx = cuidx.to_pandas() + + pidx_ceil = pidx.ceil(resolution) + cuidx_ceil = cuidx.ceil(resolution) + + assert_eq(pidx_ceil, cuidx_ceil) + + +@pytest.mark.parametrize( + "resolution", ["D", "H", "T", "min", "S", "L", "ms", "U", "us", "N"] +) +def test_index_datetime_floor(resolution): + cuidx = cudf.DatetimeIndex([1000000, 2000000, 3000000, 4000000, 5000000]) + pidx = cuidx.to_pandas() + + pidx_floor = pidx.floor(resolution) + cuidx_floor = cuidx.floor(resolution) + + assert_eq(pidx_floor, cuidx_floor)