Skip to content

Commit

Permalink
ceil/floor for DatetimeIndex (#9554)
Browse files Browse the repository at this point in the history
Follow-up to #9571  where we add `ceil` and `floor` support for `Series`.

Here we add `ceil` and `floor` support to `DatetimeIndex` class. This PR is dependent on #9571 getting merged first since it assumes the `libcudf` implementation for `floor` exists.

Authors:
  - Mayank Anand (https://github.com/mayankanand007)

Approvers:
  - Michael Wang (https://github.com/isVoid)
  - Ashwin Srinath (https://github.com/shwina)

URL: #9554
  • Loading branch information
mayankanand007 authored Nov 18, 2021
1 parent d4ff518 commit 406429a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/cudf/source/api_docs/index_objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ Time-specific operations
:toctree: api/

DatetimeIndex.round
DatetimeIndex.ceil
DatetimeIndex.floor

Conversion
~~~~~~~~~~
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
62 changes: 62 additions & 0 deletions python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://pandas.pydata.org/docs/\
user_guide/timeseries.html#timeseries-offset-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 <https://pandas.pydata.org/docs/\
user_guide/timeseries.html#timeseries-offset-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):
"""
Expand Down
26 changes: 26 additions & 0 deletions python/cudf/cudf/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 406429a

Please sign in to comment.