pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.66k stars 17.91k forks source link

BUG: `pd.MultiIndex` is not able to display an index containing a particular tuple of string #54314

Open DSLituiev opened 1 year ago

DSLituiev commented 1 year ago

Pandas version checks

Reproducible Example

pd.MultiIndex.from_tuples(
    [('Legal, financial, employment and socioeconomic history detail (observable entity)',
 'Business and Financial Operation Occupation')]
)

Issue Description

pd.MultiIndex.from_tuples is not able to display a particular string

Out[3]: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/IPython/core/formatters.py:708, in PlainTextFormatter.__call__(self, obj) 701 stream = StringIO() 702 printer = pretty.RepresentationPrinter(stream, self.verbose, 703 self.max_width, self.newline, 704 max_seq_length=self.max_seq_length, 705 singleton_pprinters=self.singleton_printers, 706 type_pprinters=self.type_printers, 707 deferred_pprinters=self.deferred_printers) --> 708 printer.pretty(obj) 709 printer.flush() 710 return stream.getvalue() File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj) 407 return meth(obj, self, cycle) 408 if cls is not object \ 409 and callable(cls.__dict__.get('__repr__')): --> 410 return _repr_pprint(obj, self, cycle) 412 return _default_pprint(obj, self, cycle) 413 finally: File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/IPython/lib/pretty.py:778, in _repr_pprint(obj, p, cycle) 776 """A pprint that just redirects to the normal repr function.""" 777 # Find newlines and replace them with p.break_() --> 778 output = repr(obj) 779 lines = output.splitlines() 780 with p.group(): File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/pandas/core/indexes/base.py:1233, in Index.__repr__(self) 1229 """ 1230 Return a string representation for this object. 1231 """ 1232 klass_name = type(self).__name__ -> 1233 data = self._format_data() 1234 attrs = self._format_attrs() 1235 space = self._format_space() File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/pandas/core/indexes/base.py:1275, in Index._format_data(self, name) 1272 if is_object_dtype(self.categories): 1273 is_justify = False -> 1275 return format_object_summary( 1276 self, 1277 self._formatter_func, 1278 is_justify=is_justify, 1279 name=name, 1280 line_break_each_value=self._is_multi, 1281 ) File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/pandas/io/formats/printing.py:440, in format_object_summary(obj, formatter, is_justify, name, indent_for_name, line_break_each_value) 437 summary, line = _extend_line(summary, line, word, display_width, space2) 439 # last value: no sep added + 1 space of width used for trailing ',' --> 440 summary, line = _extend_line(summary, line, tail[-1], display_width - 2, space2) 441 summary += line 443 # right now close is either '' or ', ' 444 # Now we want to include the ']', but not the maybe space. File /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/pandas/io/formats/printing.py:355, in format_object_summary.._extend_line(s, line, value, display_width, next_line_prefix) 352 def _extend_line( 353 s: str, line: str, value: str, display_width: int, next_line_prefix: str 354 ) -> tuple[str, str]: --> 355 if adj.len(line.rstrip()) + adj.len(value.rstrip()) >= display_width: 356 s += line.rstrip() 357 line = next_line_prefix AttributeError: 'tuple' object has no attribute 'rstrip'

Expected Behavior

pd.MultiIndex displayed in Jupyter

Installed Versions

INSTALLED VERSIONS ------------------ commit : 0f437949513225922d851e9581723d82120684a6 python : 3.11.3.final.0 python-bits : 64 OS : Darwin OS-release : 21.6.0 Version : Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:28 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T8110 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.0.3 numpy : 1.24.3 pytz : 2023.3 dateutil : 2.8.2 setuptools : 66.0.0 pip : 23.0.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.13.2 pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : 2023.5.0 gcsfs : None matplotlib : 3.7.1 numba : None numexpr : 2.8.4 odfpy : None openpyxl : None pandas_gbq : None pyarrow : 12.0.1 pyreadstat : None pyxlsb : None s3fs : None scipy : 1.11.1 snappy : None sqlalchemy : 2.0.15 tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None
swesoc commented 2 weeks ago

I am using Python 3.11, pandas 2.2.3. Code is executing successfully. Attached is the result. Please let me know if I am missing something.

Thanks,

triage