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
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!