Pandas version checks

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

  • [X] 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

from typing import NamedTuple

import pandas as pd


class Person(NamedTuple):
    name: str
    age: int

    def __repr__(self):
        return self.name


a = Person("Alice", 20)
print(a)

df = pd.DataFrame([[a]])
print(df)

Issue Description

When printing a DataFrame containing NamedTuples, the default string representation for tuples is used rather than a custom string representation defined by the __repr__ method of the NamedTuple class.

Expected Behavior

I would expect the __repr__ method of the object to be used.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 8dab54d6573f7186ff0c3b6364d5e4dd635ff3e7 python : 3.11.1.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22621 machine : AMD64 processor : Intel64 Family 6 Model 126 Stepping 5, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United Kingdom.1252 pandas : 1.5.2 numpy : 1.24.1 pytz : 2022.7 dateutil : 2.8.2 setuptools : 65.5.0 pip : 22.3.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 : None IPython : 8.8.0 pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.6.2 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : 1.10.0 snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : None

Comment From: dicristina

This is a feature that can be controlled with the display.pprint_nest_depth option. Use pd.set_option("display.pprint_nest_depth", 0) (or with pd.option_context(...)) to force the use of str(element). Otherwise the pretty printer will descend into the DataFrame elements (depending on type and up to the nest depth) and print their elements. For further insight check out the pprint_thing function in io/formats/printing.py.

Comment From: phofl

This is correct. Thx for this great explanation! Closing as Usage Question

Comment From: dasistkeintest

Thanks for helping me out!