Skip to content

1_Tables

safe_highlight_dataframe(df)

Safely apply highlight_max styling to a dataframe, handling NaN values in index. Falls back to unstyled display if styling fails.

Source code in src/nhssynth/modules/dashboard/pages/1_Tables.py
def safe_highlight_dataframe(df: pd.DataFrame):
    """
    Safely apply highlight_max styling to a dataframe, handling NaN values in index.
    Falls back to unstyled display if styling fails.
    """
    try:
        # Select only numeric columns for highlighting
        numeric_cols = df.select_dtypes(include=["number"]).columns
        if len(numeric_cols) > 0:
            return df.style.highlight_max(axis=0, subset=numeric_cols)
        return df
    except (KeyError, ValueError, TypeError):
        # Fall back to unstyled display if highlight_max fails
        return df