Skip to content

ENH: Add Rich formatting support to Pandas object displays with __rich_console__ method #57572

@dwreeves

Description

@dwreeves

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Rich is a very popular and, at this point, a standard tool in the Python ecosystem for beautiful rendering of text in terminals. Pandas's rich rendering via _repr_html_ is very optimized for the Jupyter ecoystem, but lags behind in the terminal space. As someone who primarily codes in a terminal with a Rich patched REPL, I find this unfortunate and unnecessary. Hence I am adding this feature request in hopes that the situation can be improved. I can work on this if it is agreed that this feature would be handy and useful.

Feature Description

This is a very rough solution that does not cover all the appropriate challenges, requirements and complexities that would need to be met. It is just to give you an idea that it is possible:

import pandas as pd

class DataFrame(pd.DataFrame):

    def __rich_console__(self, console: "rich.console.Console", console_options: "rich.console.Console"):
        from rich.table import Table
        table = Table()
        table.add_column(self.index.name if self.index.name is not None else "")
        for c in self.columns:
            table.add_column(str(c))
        for r in self.itertuples():
            table.add_row(*map(str, r))
        yield from table.__rich_console__(console, console_options)

from rich.console import Console

console = Console()

df = DataFrame([
    {"name": "John Doe", "phone": "555-555-5555", "last_purchase_amount": 1.23},
    {"name": "Mary Sue", "phone": "800-123-4567", "last_purchase_amount": 100.00}
])

console.print(df)

A few things are missing from this solution. Notably, it needs to hook into the Pandas options; it needs to be able to truncate the rows and columns based on the relevant display options. Series objects also need to have a __rich_console__. The Styler object should, as well, and this should hook into the Styler object configuration. There should probably be a way to set additional display options for this somewhere (e.g. display.rich.[...] and styler.rich.[...]). You'd need to hook into the try-except import stuff that Pandas does on the backend for optional dependencies. Lastly, of course, this would all need to be documented.

Alternative Solutions

  • Users can just do it themselves, I suppose.
  • You can use the __rich__ method, but I think you'd want to have access to the console parameters when fleshing out the code.

Additional Context

I could not find any related issues.

Metadata

Metadata

Assignees

Labels

EnhancementNeeds DiscussionRequires discussion from core team before further actionOutput-Formatting__repr__ of pandas objects, to_string

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions