Skip to content

ENH: Styler.apply with axis=None can accept ndarray #39359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
attack68 opened this issue Jan 23, 2021 · 0 comments · Fixed by #39393
Closed

ENH: Styler.apply with axis=None can accept ndarray #39359

attack68 opened this issue Jan 23, 2021 · 0 comments · Fixed by #39393
Labels
Enhancement Styler conditional formatting using DataFrame.style
Milestone

Comments

@attack68
Copy link
Contributor

Currently Styler.apply with axis=0 and axis=1 permit functions which return array-like objects, provided they are of appropriate shape. With axis=None the return must be a DataFrame.

This means the following example:

def highlight_max(d):
    """Highlight the maximum in a Series or DataFrame bold-orange."""
    css = 'background-color: orange; font-weight: bold;'
    return np.where(d == np.nanmax(d.values), css, None)

df.style.apply(highlight_max, axis=0)  # works
df.style.apply(highlight_max, axis=1)  # works
df.style.apply(highlight_max, axis=None)  # raises TypeError since return is not DataFrame

Describe the solution you'd like

The solution is a relatively simple change to code as part of the Styler._apply method that checks for array-like (or specifically ndarray) result instead of DataFrame, and constructs a DataFrame from that data if needs be, with appropriate index and cols.

API breaking implications

None since all previous behaviour is replicable.

@attack68 attack68 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 23, 2021
@simonjayhawkins simonjayhawkins added Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 25, 2021
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jan 25, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.3 Feb 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants