Skip to content

Commit a377f03

Browse files
authored
REF: improve rendering of categories in CategoricalIndex (#45340)
1 parent 2026854 commit a377f03

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enhancement2
3030

3131
Other enhancements
3232
^^^^^^^^^^^^^^^^^^
33-
-
33+
- Improved the rendering of ``categories`` in :class:`CategoricalIndex` (:issue:`45218`)
3434
-
3535

3636
.. ---------------------------------------------------------------------------

pandas/core/indexes/category.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import numpy as np
1010

11-
from pandas._config import get_option
12-
1311
from pandas._libs import index as libindex
1412
from pandas._typing import (
1513
Dtype,
@@ -347,16 +345,12 @@ def _format_attrs(self):
347345
"""
348346
Return a list of tuples of the (attr,formatted_value)
349347
"""
350-
max_categories = (
351-
10
352-
if get_option("display.max_categories") == 0
353-
else get_option("display.max_categories")
354-
)
355348
attrs: list[tuple[str, str | int | bool | None]]
349+
356350
attrs = [
357351
(
358352
"categories",
359-
ibase.default_pprint(self.categories, max_seq_items=max_categories),
353+
"[" + ", ".join(self._data._repr_categories()) + "]",
360354
),
361355
("ordered", self.ordered),
362356
]

pandas/tests/arrays/categorical/test_repr.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def test_categorical_index_repr(self):
382382
assert repr(idx) == exp
383383

384384
i = CategoricalIndex(Categorical(np.arange(10)))
385-
exp = """CategoricalIndex([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], categories=[0, 1, 2, 3, 4, 5, 6, 7, ...], ordered=False, dtype='category')""" # noqa:E501
385+
exp = """CategoricalIndex([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], categories=[0, 1, 2, 3, ..., 6, 7, 8, 9], ordered=False, dtype='category')""" # noqa:E501
386386
assert repr(i) == exp
387387

388388
def test_categorical_index_repr_ordered(self):
@@ -391,7 +391,7 @@ def test_categorical_index_repr_ordered(self):
391391
assert repr(i) == exp
392392

393393
i = CategoricalIndex(Categorical(np.arange(10), ordered=True))
394-
exp = """CategoricalIndex([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], categories=[0, 1, 2, 3, 4, 5, 6, 7, ...], ordered=True, dtype='category')""" # noqa:E501
394+
exp = """CategoricalIndex([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], categories=[0, 1, 2, 3, ..., 6, 7, 8, 9], ordered=True, dtype='category')""" # noqa:E501
395395
assert repr(i) == exp
396396

397397
def test_categorical_index_repr_datetime(self):
@@ -498,7 +498,7 @@ def test_categorical_index_repr_period_ordered(self):
498498
def test_categorical_index_repr_timedelta(self):
499499
idx = timedelta_range("1 days", periods=5)
500500
i = CategoricalIndex(Categorical(idx))
501-
exp = """CategoricalIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], categories=[1 days 00:00:00, 2 days 00:00:00, 3 days 00:00:00, 4 days 00:00:00, 5 days 00:00:00], ordered=False, dtype='category')""" # noqa:E501
501+
exp = """CategoricalIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], categories=[1 days, 2 days, 3 days, 4 days, 5 days], ordered=False, dtype='category')""" # noqa:E501
502502
assert repr(i) == exp
503503

504504
idx = timedelta_range("1 hours", periods=10)
@@ -507,14 +507,14 @@ def test_categorical_index_repr_timedelta(self):
507507
'3 days 01:00:00', '4 days 01:00:00', '5 days 01:00:00',
508508
'6 days 01:00:00', '7 days 01:00:00', '8 days 01:00:00',
509509
'9 days 01:00:00'],
510-
categories=[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, 5 days 01:00:00, 6 days 01:00:00, 7 days 01:00:00, ...], ordered=False, dtype='category')""" # noqa:E501
510+
categories=[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, ..., 6 days 01:00:00, 7 days 01:00:00, 8 days 01:00:00, 9 days 01:00:00], ordered=False, dtype='category')""" # noqa:E501
511511

512512
assert repr(i) == exp
513513

514514
def test_categorical_index_repr_timedelta_ordered(self):
515515
idx = timedelta_range("1 days", periods=5)
516516
i = CategoricalIndex(Categorical(idx, ordered=True))
517-
exp = """CategoricalIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], categories=[1 days 00:00:00, 2 days 00:00:00, 3 days 00:00:00, 4 days 00:00:00, 5 days 00:00:00], ordered=True, dtype='category')""" # noqa:E501
517+
exp = """CategoricalIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], categories=[1 days, 2 days, 3 days, 4 days, 5 days], ordered=True, dtype='category')""" # noqa:E501
518518
assert repr(i) == exp
519519

520520
idx = timedelta_range("1 hours", periods=10)
@@ -523,7 +523,7 @@ def test_categorical_index_repr_timedelta_ordered(self):
523523
'3 days 01:00:00', '4 days 01:00:00', '5 days 01:00:00',
524524
'6 days 01:00:00', '7 days 01:00:00', '8 days 01:00:00',
525525
'9 days 01:00:00'],
526-
categories=[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, 4 days 01:00:00, 5 days 01:00:00, 6 days 01:00:00, 7 days 01:00:00, ...], ordered=True, dtype='category')""" # noqa:E501
526+
categories=[0 days 01:00:00, 1 days 01:00:00, 2 days 01:00:00, 3 days 01:00:00, ..., 6 days 01:00:00, 7 days 01:00:00, 8 days 01:00:00, 9 days 01:00:00], ordered=True, dtype='category')""" # noqa:E501
527527

528528
assert repr(i) == exp
529529

pandas/tests/indexes/categorical/test_formats.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_string_categorical_index_repr(self):
4141
idx = CategoricalIndex(list("abcdefghijklmmo"))
4242
expected = """CategoricalIndex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
4343
'm', 'm', 'o'],
44-
categories=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', ...], ordered=False, dtype='category')""" # noqa:E501
44+
categories=['a', 'b', 'c', 'd', ..., 'k', 'l', 'm', 'o'], ordered=False, dtype='category')""" # noqa:E501
4545

4646
assert repr(idx) == expected
4747

@@ -72,7 +72,7 @@ def test_string_categorical_index_repr(self):
7272
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
7373
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し',
7474
'す', 'せ', 'そ'],
75-
categories=['あ', 'い', 'う', 'え', 'お', '', '', '', ...], ordered=False, dtype='category')""" # noqa:E501
75+
categories=['あ', 'い', 'う', 'え', ..., '', '', '', 'そ'], ordered=False, dtype='category')""" # noqa:E501
7676

7777
assert repr(idx) == expected
7878

@@ -109,6 +109,6 @@ def test_string_categorical_index_repr(self):
109109
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
110110
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ',
111111
'さ', 'し', 'す', 'せ', 'そ'],
112-
categories=['あ', 'い', 'う', 'え', 'お', '', '', '', ...], ordered=False, dtype='category')""" # noqa:E501
112+
categories=['あ', 'い', 'う', 'え', ..., '', '', '', 'そ'], ordered=False, dtype='category')""" # noqa:E501
113113

114114
assert repr(idx) == expected

0 commit comments

Comments
 (0)