Skip to content

Commit dc0ec0b

Browse files
authored
BUG: repr aligning left for string dtype columns (#54801)
1 parent 01c3e8b commit dc0ec0b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Diff for: doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ Conversion
717717
Strings
718718
^^^^^^^
719719
- Bug in :meth:`Series.str` that did not raise a ``TypeError`` when iterated (:issue:`54173`)
720+
- Bug in ``repr`` for :class:`DataFrame`` with string-dtype columns (:issue:`54797`)
720721

721722
Interval
722723
^^^^^^^^

Diff for: pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1397,8 +1397,8 @@ def _format_with_header(self, header: list[str_t], na_rep: str_t) -> list[str_t]
13971397

13981398
values = self._values
13991399

1400-
if is_object_dtype(values.dtype):
1401-
values = cast(np.ndarray, values)
1400+
if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
1401+
values = np.asarray(values)
14021402
values = lib.maybe_convert_objects(values, safe=True)
14031403

14041404
result = [pprint_thing(x, escape_chars=("\t", "\r", "\n")) for x in values]

Diff for: pandas/tests/frame/test_repr_info.py

+11
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,14 @@ def test_masked_ea_with_formatter(self):
455455
0 0.12 1.00
456456
1 1.12 2.00"""
457457
assert result == expected
458+
459+
def test_repr_ea_columns(self, any_string_dtype):
460+
# GH#54797
461+
pytest.importorskip("pyarrow")
462+
df = DataFrame({"long_column_name": [1, 2, 3], "col2": [4, 5, 6]})
463+
df.columns = df.columns.astype(any_string_dtype)
464+
expected = """ long_column_name col2
465+
0 1 4
466+
1 2 5
467+
2 3 6"""
468+
assert repr(df) == expected

0 commit comments

Comments
 (0)