Can __repr__ return a dataframe?

Is it possible to make __repr__ in the code below return a dataframe?

The code below will throw

TypeError: __str__ returned non-string (type DataFrame)

because – I guess – __repr__ has to return a string.

Or should I replace __repr__ with some other magic method?

import pandas as pd

class Query():
    def __init__(self, filename):
        self.df = pd.read_excel(filename)
    def __repr__(self):
        return self.df
if __name__ == '__main__':
    filename = 'some_filename'
    query = Query(filename)


You want __repr__ to return a string, which would be the representation of your dataframe. So, just do:

def __repr__(self):
        return repr(self.df)