Skip to content

BUG: Printing of MultiIndex fails for longer strings #53537

@hagenw

Description

@hagenw

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# Short string, works when printing
index = pd.MultiIndex.from_arrays((['a' * 10], [0.1], [0.9]), names=['file', 'start', 'end'])
print(index)

# Long string, fails when printing
index = pd.MultiIndex.from_arrays((['a' * 58], [0.1], [0.9]), names=['file', 'start', 'end'])
print(index)

Issue Description

The last print statement fails with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/audeering.local/hwierstorf/.envs/test/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 1233, in __repr__
    data = self._format_data()
  File "/home/audeering.local/hwierstorf/.envs/test/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 1275, in _format_data
    return format_object_summary(
  File "/home/audeering.local/hwierstorf/.envs/test/lib/python3.8/site-packages/pandas/io/formats/printing.py", line 440, in format_object_summary
    summary, line = _extend_line(summary, line, tail[-1], display_width - 2, space2)
  File "/home/audeering.local/hwierstorf/.envs/test/lib/python3.8/site-packages/pandas/io/formats/printing.py", line 355, in _extend_line
    if adj.len(line.rstrip()) + adj.len(value.rstrip()) >= display_width:
AttributeError: 'tuple' object has no attribute 'rstrip'

Expected Behavior

It should have returned something like:

MultiIndex([('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', ...)],
           names=['file', 'start', 'end'])

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-149-generic
Version : #166~18.04.1-Ubuntu SMP Fri Apr 21 16:42:44 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 2.0.2
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions