Skip to content

Commit

Permalink
Fix an issue with IntervalIndex.repr when null values are present (#…
Browse files Browse the repository at this point in the history
…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: #13958
  • Loading branch information
galipremsagar authored Aug 25, 2023
1 parent 4591dd3 commit ec1e73f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions python/cudf/cudf/tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit ec1e73f

Please sign in to comment.