Skip to content
11 changes: 8 additions & 3 deletions python/cudf/cudf/core/column/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def length(self) -> ColumnBase:
@functools.cached_property
def left(self) -> ColumnBase:
return ColumnBase.create(
self.plc_column.children()[0],
self.plc_column.struct_view().get_sliced_child(0),
self.subtype,
)

Expand All @@ -98,7 +98,7 @@ def mid(self) -> ColumnBase:
@functools.cached_property
def right(self) -> ColumnBase:
return ColumnBase.create(
self.plc_column.children()[1],
self.plc_column.struct_view().get_sliced_child(1),
self.subtype,
)

Expand Down Expand Up @@ -193,7 +193,12 @@ def to_pandas(
f"pandas does not have a native nullable type for {self.dtype}."
)
pd_type = cast("IntervalDtype", self.dtype).to_pandas()
return pd.Index(pd_type.__from_arrow__(self.to_arrow()), dtype=pd_type)
return pd.IntervalIndex.from_arrays(
self.left.to_pandas(),
self.right.to_pandas(),
Comment on lines +197 to +198

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these calls needs to pass though nullable and arrow_type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both flags are false here. Arrow conversion returns earlier, and native intervals raise for nullable=True. I simplified the guard and verified all four flag combinations.

closed=self.closed,
dtype=pd_type,
)

def element_indexing(self, index: int) -> pd.Interval | None:
result = super().element_indexing(index)
Expand Down
14 changes: 0 additions & 14 deletions python/cudf/cudf/pandas/scripts/pandas-testing-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2045,25 +2045,14 @@ def pytest_unconfigure(config):
"tests/indexes/interval/test_astype.py::TestFloatSubtype::test_subtype_integer[uint64]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_astype.py::TestFloatSubtype::test_subtype_integer_errors": "TODO: Add a reason for failure",
"tests/indexes/interval/test_constructors.py::TestFromBreaks::test_generic_errors": "TODO: Add a reason for failure",
"tests/indexes/interval/test_constructors.py::test_ea_dtype[Float64]": "https://github.com/pandas-dev/pandas/issues/64297",
"tests/indexes/interval/test_constructors.py::test_ea_dtype[float64[pyarrow]]": "https://github.com/pandas-dev/pandas/issues/64297",
"tests/indexes/interval/test_indexing.py::TestGetIndexer::test_get_indexer_categorical_with_nans": "TODO: Add a reason for failure",
"tests/indexes/interval/test_indexing.py::TestGetIndexer::test_get_indexer_datetime": "TODO: Add a reason for failure",
"tests/indexes/interval/test_indexing.py::TestGetLoc::test_get_loc_datetimelike_nonoverlapping[datetime64[us, US/Eastern]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_indexing.py::TestPutmask::test_putmask_dt64[US/Pacific]": "TypeError: Cannot interpret 'datetime64[us, US/Pacific]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_comparison": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_ensure_copied_data[both]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_ensure_copied_data[left]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_ensure_copied_data[neither]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_ensure_copied_data[right]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_insert[data0]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8[datetime64[us, US/Eastern]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[Interval-datetime64[us, US/Eastern]-datetime64[us]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[Interval-datetime64[us, US/Eastern]-timedelta64[us]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[IntervalIndex-datetime64[us, US/Eastern]-datetime64[us]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[IntervalIndex-datetime64[us, US/Eastern]-timedelta64[us]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[IntervalIndex-datetime64[us]-datetime64[us, US/Eastern]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[IntervalIndex-timedelta64[us]-datetime64[us, US/Eastern]]": "TypeError: Cannot interpret 'datetime64[us, US/Eastern]' as a data type",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_monotonic[both]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_monotonic[left]": "TODO: Add a reason for failure",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_monotonic[neither]": "TODO: Add a reason for failure",
Expand Down Expand Up @@ -5318,9 +5307,6 @@ def pytest_unconfigure(config):
"tests/groupby/methods/test_value_counts.py::test_against_frame_and_seriesgroupby[True-False-True-False-True-proportion-function]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/groupby/methods/test_value_counts.py::test_against_frame_and_seriesgroupby[True-False-True-True-False-count-function]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/groupby/methods/test_value_counts.py::test_against_frame_and_seriesgroupby[True-False-True-True-True-proportion-function]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[Index-datetime64[us, US/Eastern]-datetime64[us]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[scalar-datetime64[us, US/Eastern]-datetime64[us]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/indexes/interval/test_interval.py::TestIntervalIndex::test_maybe_convert_i8_errors[scalar-datetime64[us, US/Eastern]-timedelta64[us]]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/indexes/multi/test_formats.py::TestRepr::test_tuple_width": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/plotting/frame/test_frame.py::TestDataFramePlots::test_memory_leak[area]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
"tests/plotting/frame/test_frame.py::TestDataFramePlots::test_memory_leak[line]": "Skipped: failing in pandas-tests sharded CI (PR #22992, run 28204832469)",
Expand Down
52 changes: 52 additions & 0 deletions python/cudf/cudf/tests/indexes/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,58 @@ def test_interval_to_arrow():
assert_eq(expect, got)


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
@pytest.mark.parametrize("tz", [None, "US/Eastern", "Asia/Kolkata"])
@pytest.mark.parametrize("selection", [slice(None), slice(1, 3), slice(0, 0)])
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
def test_interval_to_pandas_datetime(closed, unit, tz, selection):
breaks = pd.date_range("2025-03-08", periods=5, tz=tz, unit=unit)
expected = pd.Series(
pd.IntervalIndex.from_breaks(breaks, closed=closed), name="intervals"
).iloc[selection]
intervals = cudf.IntervalIndex.from_breaks(
cudf.from_pandas(breaks), closed=closed, name="intervals"
)
result = cudf.Series(intervals, name="intervals").iloc[selection]

assert_eq(result.to_pandas(), expected)
assert_eq(
cudf.IntervalIndex(result, name="intervals").to_pandas(),
pd.IntervalIndex(expected, name="intervals"),
)


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize("subtype", ["Float64", "float64[pyarrow]"])
def test_interval_to_pandas_extension_subtype(closed, subtype):
breaks = pd.Series([0.0, 0.4, 0.6], dtype=subtype)
expected = pd.IntervalIndex.from_breaks(
breaks, closed=closed, dtype=pd.IntervalDtype(subtype, closed)
)
result = cudf.IntervalIndex.from_breaks(
cudf.from_pandas(breaks), closed=closed
)

assert_eq(result.to_pandas(), expected)


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize(
"subtype", ["float64", "datetime64[ns]", "timedelta64[ns]"]
)
def test_interval_to_pandas_nulls(closed, subtype):
expected = pd.Series(
pd.IntervalIndex.from_breaks(
np.arange(5).astype(subtype), closed=closed
)
)
expected.iloc[1] = None
result = cudf.from_pandas(expected)

assert_eq(result.to_pandas(), expected)
assert_eq(result.iloc[1:3].to_pandas(), expected.iloc[1:3])


INTERVAL_BOUNDARY_TYPES = [
int,
np.int8,
Expand Down
19 changes: 19 additions & 0 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,25 @@ def test_at_setitem_empty():
tm.assert_frame_equal(df, expected)


@pytest.mark.parametrize("closed", ["left", "right", "both", "neither"])
@pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Kolkata"])
def test_timezone_interval_fast_to_slow(monkeypatch, closed, tz):
breaks = pd.DatetimeIndex(
list(pd.date_range("2025-03-08", periods=4, tz=tz))
)
expected = pd.IntervalIndex.from_breaks(
breaks, closed=closed, name="intervals"
)
monkeypatch.setenv("CUDF_PANDAS_FAIL_ON_FALLBACK", "1")

result = xpd.IntervalIndex.from_breaks(
breaks, closed=closed, name="intervals"
)

assert isinstance(result._fsproxy_fast, cudf.IntervalIndex)
pd.testing.assert_index_equal(result._fsproxy_slow, expected)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this just use tm.assert_index_equal(result, expected)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tm.assert_index_equal(result, expected) raises FallbackError with a native pandas expected. I switched to result.as_cpu_object() so the test checks the conversion with fallback disabled.



@pytest.mark.parametrize(
"index",
[
Expand Down
Loading