Skip to content

Commit

Permalink
Deprecate non-integer periods in date_range and interval_range (#…
Browse files Browse the repository at this point in the history
…14976)

This PR deprecates non-integer `periods` in `date_range` and `interval_range` to match pandas-2.2 deprecations.

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

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #14976
  • Loading branch information
galipremsagar authored Feb 6, 2024
1 parent 72ecbe9 commit 506d575
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
6 changes: 6 additions & 0 deletions python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,12 @@ def interval_range(

start = cudf.Scalar(start) if start is not None else start
end = cudf.Scalar(end) if end is not None else end
if periods is not None and not cudf.api.types.is_integer(periods):
warnings.warn(
"Non-integer 'periods' in cudf.date_range, and cudf.interval_range"
" are deprecated and will raise in a future version.",
FutureWarning,
)
periods = cudf.Scalar(int(periods)) if periods is not None else periods
freq = cudf.Scalar(freq) if freq is not None else freq

Expand Down
7 changes: 7 additions & 0 deletions python/cudf/cudf/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,13 @@ def date_range(
"three must be specified"
)

if periods is not None and not cudf.api.types.is_integer(periods):
warnings.warn(
"Non-integer 'periods' in cudf.date_range, and cudf.interval_range"
" are deprecated and will raise in a future version.",
FutureWarning,
)

dtype = np.dtype("<M8[ns]")

if freq is None:
Expand Down
38 changes: 31 additions & 7 deletions python/cudf/cudf/tests/indexes/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import pytest

import cudf
from cudf.core._compat import PANDAS_GE_210
from cudf.core._compat import PANDAS_GE_210, PANDAS_GE_220
from cudf.core.index import IntervalIndex, interval_range
from cudf.testing._utils import assert_eq
from cudf.testing._utils import assert_eq, expect_warning_if


def test_interval_constructor_default_closed():
Expand All @@ -33,6 +33,15 @@ def test_interval_to_arrow():
cudf.Scalar,
]

PERIODS_TYPES = [
int,
np.int8,
np.int16,
np.int32,
np.int64,
cudf.Scalar,
]


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize("start", [0, 1, 2, 3])
Expand Down Expand Up @@ -96,7 +105,7 @@ def test_interval_range_freq_basic_dtype(start_t, end_t, freq_t):


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize("periods", [1, 1.0, 2, 2.0, 3.0, 3])
@pytest.mark.parametrize("periods", [1, 2, 3])
@pytest.mark.parametrize("start", [0, 0.0, 1.0, 1, 2, 2.0, 3.0, 3])
@pytest.mark.parametrize("end", [4, 4.0, 5.0, 5, 6, 6.0, 7.0, 7])
def test_interval_range_periods_basic(start, end, periods, closed):
Expand All @@ -112,9 +121,9 @@ def test_interval_range_periods_basic(start, end, periods, closed):

@pytest.mark.parametrize("start_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("end_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("periods_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("periods_t", PERIODS_TYPES)
def test_interval_range_periods_basic_dtype(start_t, end_t, periods_t):
start, end, periods = start_t(0), end_t(4), periods_t(1.0)
start, end, periods = start_t(0), end_t(4), periods_t(1)
start_val = start.value if isinstance(start, cudf.Scalar) else start
end_val = end.value if isinstance(end, cudf.Scalar) else end
periods_val = (
Expand All @@ -130,6 +139,21 @@ def test_interval_range_periods_basic_dtype(start_t, end_t, periods_t):
assert_eq(pindex, gindex)


def test_interval_range_periods_warnings():
start_val, end_val, periods_val = 0, 4, 1.0

with expect_warning_if(PANDAS_GE_220):
pindex = pd.interval_range(
start=start_val, end=end_val, periods=periods_val, closed="left"
)
with pytest.warns(FutureWarning):
gindex = cudf.interval_range(
start=start_val, end=end_val, periods=periods_val, closed="left"
)

assert_eq(pindex, gindex)


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize("periods", [1, 2, 3])
@pytest.mark.parametrize("freq", [1, 2, 3, 4])
Expand All @@ -145,7 +169,7 @@ def test_interval_range_periods_freq_end(end, freq, periods, closed):
assert_eq(pindex, gindex)


@pytest.mark.parametrize("periods_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("periods_t", PERIODS_TYPES)
@pytest.mark.parametrize("freq_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("end_t", INTERVAL_BOUNDARY_TYPES)
def test_interval_range_periods_freq_end_dtype(periods_t, freq_t, end_t):
Expand Down Expand Up @@ -180,7 +204,7 @@ def test_interval_range_periods_freq_start(start, freq, periods, closed):
assert_eq(pindex, gindex)


@pytest.mark.parametrize("periods_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("periods_t", PERIODS_TYPES)
@pytest.mark.parametrize("freq_t", INTERVAL_BOUNDARY_TYPES)
@pytest.mark.parametrize("start_t", INTERVAL_BOUNDARY_TYPES)
def test_interval_range_periods_freq_start_dtype(periods_t, freq_t, start_t):
Expand Down

0 comments on commit 506d575

Please sign in to comment.