Skip to content

Styler class should have a pipe() method, akin to DataFrame.pipe() #23229

Closed
@nmusolino

Description

@nmusolino

Problem description

Users may want to create functions that apply customized styling to a dataframe or styler object.

In order to make these functions composable, a convenient signature is f(styler) -> Styler, so that a user can chain multiple such functions together, in a "mix-and-match" fashion. (N.B. Such functions can obtain the underlying dataframe using the styler.data attribute.)

A method Styler.pipe() would provide an ergonomic way to apply these functions sequentially, after a series of Dataframe-related manipulations.

Usage example:

def right_align(styler):
    return styler.set_properties(**{'text-align': 'right'})

def highlight_rows_gt(styler, colname, threshold):
    return ...        # implementation omitted

>>> (df.groupby('A')[['B', 'C']].sum()
...        .assign(D=lambda x: x['B'] / x['C'])
...        .style
...        .format('{:.2f}')
...        .pipe(highlight_rows_gt, colname='D', threshold=0.95)
...        .pipe(right_align))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions