From 85c52fc529769a20a429240a130761bbf3f00141 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 17 Jul 2025 21:08:09 +0200 Subject: [PATCH] BUG: fix padding for string categories in CategoricalIndex repr --- pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/categorical/test_formats.py | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index fb395f4f7bb1a..60288aedcba50 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1416,7 +1416,7 @@ def _format_data(self, name=None) -> str_t: is_justify = False elif isinstance(self.dtype, CategoricalDtype): self = cast("CategoricalIndex", self) - if is_object_dtype(self.categories.dtype): + if is_string_dtype(self.categories.dtype): is_justify = False elif isinstance(self, ABCRangeIndex): # We will do the relevant formatting via attrs diff --git a/pandas/tests/indexes/categorical/test_formats.py b/pandas/tests/indexes/categorical/test_formats.py index b100740b064ce..2308a62bc44a4 100644 --- a/pandas/tests/indexes/categorical/test_formats.py +++ b/pandas/tests/indexes/categorical/test_formats.py @@ -2,9 +2,6 @@ Tests for CategoricalIndex.__repr__ and related methods. """ -import pytest - -from pandas._config import using_string_dtype import pandas._config.config as cf from pandas import CategoricalIndex @@ -22,7 +19,6 @@ def test_string_categorical_index_repr(self, using_infer_string): ) assert repr(idx) == expected - @pytest.mark.xfail(using_string_dtype(), reason="Different padding on multi-line") def test_categorical_index_repr_multiline(self, using_infer_string): # multiple lines idx = CategoricalIndex(["a", "bb", "ccc"] * 10) @@ -37,7 +33,6 @@ def test_categorical_index_repr_multiline(self, using_infer_string): ) assert repr(idx) == expected - @pytest.mark.xfail(using_string_dtype(), reason="Different padding on multi-line") def test_categorical_index_repr_truncated(self, using_infer_string): # truncated idx = CategoricalIndex(["a", "bb", "ccc"] * 100) @@ -76,7 +71,6 @@ def test_categorical_index_repr_unicode(self, using_infer_string): ) assert repr(idx) == expected - @pytest.mark.xfail(using_string_dtype(), reason="Different padding on multi-line") def test_categorical_index_repr_unicode_multiline(self, using_infer_string): # multiple lines idx = CategoricalIndex(["あ", "いい", "ううう"] * 10) @@ -91,7 +85,6 @@ def test_categorical_index_repr_unicode_multiline(self, using_infer_string): ) assert repr(idx) == expected - @pytest.mark.xfail(using_string_dtype(), reason="Different padding on multi-line") def test_categorical_index_repr_unicode_truncated(self, using_infer_string): # truncated idx = CategoricalIndex(["あ", "いい", "ううう"] * 100) @@ -131,7 +124,6 @@ def test_categorical_index_repr_east_asian_width(self, using_infer_string): ) assert repr(idx) == expected - @pytest.mark.xfail(using_string_dtype(), reason="Different padding on multi-line") def test_categorical_index_repr_east_asian_width_multiline( self, using_infer_string ): @@ -151,7 +143,6 @@ def test_categorical_index_repr_east_asian_width_multiline( ) assert repr(idx) == expected - @pytest.mark.xfail(using_string_dtype(), reason="Different padding on multi-line") def test_categorical_index_repr_east_asian_width_truncated( self, using_infer_string ):