diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 8dabaeb6c7bfe..f08ca72a17ae8 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -315,7 +315,7 @@ Interval ^^^^^^^^ - Construction of :class:`Interval` is restricted to numeric, :class:`Timestamp` and :class:`Timedelta` endpoints (:issue:`23013`) -- +- Fixed bug in :class:`Series`/:class:`DataFrame` not displaying ``NaN`` in :class:`IntervalIndex` with missing values (:issue:`25984`) - Indexing diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 077bfb6a88f0c..ffbed7ab2006d 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -1015,7 +1015,7 @@ def __getitem__(self, value): def _format_with_header(self, header, **kwargs): return header + list(self._format_native_types(**kwargs)) - def _format_native_types(self, na_rep='', quoting=None, **kwargs): + def _format_native_types(self, na_rep='NaN', quoting=None, **kwargs): """ actually format my specific types """ from pandas.io.formats.format import ExtensionArrayFormatter return ExtensionArrayFormatter(values=self, diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 82d88ba07f8e0..372968a3501a2 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -378,6 +378,23 @@ def test_frame_repr(self): ) assert result == expected + @pytest.mark.parametrize('constructor,expected', [ + (pd.Series, ('(0.0, 1.0] a\n' + 'NaN b\n' + '(2.0, 3.0] c\n' + 'dtype: object')), + (pd.DataFrame, (' 0\n' + '(0.0, 1.0] a\n' + 'NaN b\n' + '(2.0, 3.0] c')) + ]) + def test_repr_missing(self, constructor, expected): + # GH 25984 + index = IntervalIndex.from_tuples([(0, 1), np.nan, (2, 3)]) + obj = constructor(list('abc'), index=index) + result = repr(obj) + assert result == expected + # TODO: check this behavior is consistent with test_interval_new.py def test_get_item(self, closed): i = IntervalIndex.from_arrays((0, 1, np.nan), (1, 2, np.nan),