Skip to content

Commit

Permalink
Disable creation of DatetimeIndex when freq is passed to `cudf.da…
Browse files Browse the repository at this point in the history
…te_range` (#13890)

In #13857, we disabled the construction of `DatetimeIndex` & `TimedeltaIndex` when their pandas counter-parts have `freq` set. However, when `freq` is passed to `date_range`, the expectation and pandas-behavior is to return a `DateTimeIndex` with `freq` set. This isn't possible with cudf currently, hence disabled the construction of `DatetimeIndex` in this case.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #13890
  • Loading branch information
galipremsagar authored Aug 16, 2023
1 parent b6dbe41 commit fdeabab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/cudf/cudf/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,10 @@ def date_range(
arr = cp.linspace(start=start, stop=end, num=periods)
result = cudf.core.column.as_column(arr).astype("datetime64[ns]")
return cudf.DatetimeIndex._from_data({name: result})
elif cudf.get_option("mode.pandas_compatible"):
raise NotImplementedError(
"`DatetimeIndex` with `freq` cannot be constructed."
)

# The code logic below assumes `freq` is defined. It is first normalized
# into `DateOffset` for further computation with timestamps.
Expand Down
13 changes: 13 additions & 0 deletions python/cudf/cudf/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,3 +2133,16 @@ def test_datetime_series_cmpops_pandas_compatibility(data1, data2, op):
def test_datetime_getitem_na():
s = cudf.Series([1, 2, None, 3], dtype="datetime64[ns]")
assert s[2] is cudf.NaT


def test_daterange_pandas_compatibility():
with cudf.option_context("mode.pandas_compatible", True):
with pytest.raises(NotImplementedError):
cudf.date_range("20010101", "20020215", freq="400h", name="times")
expected = pd.date_range(
"2010-01-01", "2010-02-01", periods=10, name="times"
)
actual = cudf.date_range(
"2010-01-01", "2010-02-01", periods=10, name="times"
)
assert_eq(expected, actual)

0 comments on commit fdeabab

Please sign in to comment.