From ec1e73f8d04563c95fb5e0eb775c2e8c65ee0d64 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Fri, 25 Aug 2023 13:49:28 -0500 Subject: [PATCH] Fix an issue with `IntervalIndex.repr` when null values are present (#13958) closes #13954 This PR fixes an issue with `IntervalIndex.repr`, where there was a silent failure because of no dedicated `_clean_nulls_from_index` and the `GenericIndex._clean_nulls_from_index` wouldn't work because a type-cast to `str` isn't implemented. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Matthew Roeschke (https://github.com/mroeschke) URL: https://github.com/rapidsai/cudf/pull/13958 --- python/cudf/cudf/core/dtypes.py | 5 ++++- python/cudf/cudf/core/index.py | 5 ++++- python/cudf/cudf/tests/test_repr.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/python/cudf/cudf/core/dtypes.py b/python/cudf/cudf/core/dtypes.py index a83c1f7b3c9..5fb092c7cbc 100644 --- a/python/cudf/cudf/core/dtypes.py +++ b/python/cudf/cudf/core/dtypes.py @@ -905,9 +905,12 @@ def __init__(self, subtype, closed="right"): def subtype(self): return self.fields["left"] - def __repr__(self): + def __repr__(self) -> str: return f"interval[{self.subtype}, {self.closed}]" + def __str__(self) -> str: + return self.__repr__() + @classmethod def from_arrow(cls, typ): return IntervalDtype(typ.subtype.to_pandas_dtype(), typ.closed) diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 458ef2df02d..c7e25cdc430 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -3187,7 +3187,7 @@ def from_breaks(breaks, closed="right", name=None, copy=False, dtype=None): >>> import cudf >>> import pandas as pd >>> cudf.IntervalIndex.from_breaks([0, 1, 2, 3]) - IntervalIndex([(0, 1], (1, 2], (2, 3]], dtype='interval') + IntervalIndex([(0, 1], (1, 2], (2, 3]], dtype='interval[int64, right]') """ if copy: breaks = column.as_column(breaks, dtype=dtype).copy() @@ -3211,6 +3211,9 @@ def _is_interval(self): def _is_boolean(self): return False + def _clean_nulls_from_index(self): + return self + class StringIndex(GenericIndex): """String defined indices into another Column diff --git a/python/cudf/cudf/tests/test_repr.py b/python/cudf/cudf/tests/test_repr.py index b944e0483d0..a36cc1b3819 100644 --- a/python/cudf/cudf/tests/test_repr.py +++ b/python/cudf/cudf/tests/test_repr.py @@ -1469,3 +1469,16 @@ def test_repr_struct_after_concat(): pdf = df.to_pandas() assert repr(df) == repr(pdf) + + +def test_interval_index_repr(): + pi = pd.Index( + [ + np.nan, + pd.Interval(2.0, 3.0, closed="right"), + pd.Interval(3.0, 4.0, closed="right"), + ] + ) + gi = cudf.from_pandas(pi) + + assert repr(pi) == repr(gi)