Styler#

class chainladder.Styler(triangle: Triangle, *args: Any, **kwargs: Any)[source]#

Styles a Triangle according to the data with HTML and CSS.

This class provides methods for styling and formatting a Triangle. The styled output can be rendered as HTML or LaTeX, and it supports CSS-based styling, allowing users to control colors, font styles, and other visual aspects of triangular data. It is particularly useful for presenting Triangle objects in a Jupyter Notebook environment or when exporting styled triangles for reports.

Parameters:
triangle: Triangle

The Triangle to style.

*args: Any

Additional positional arguments passed to pandas.io.formats.style.Styler.

**kwargs: Any

Additional keyword arguments passed to pandas.io.formats.style.Styler.

Raises:
TypeError

If triangle is anything other than a Triangle.

ValueError

If triangle is multidimensional, i.e. holds more than a single index and column, or if it is empty, i.e. holds no values to style.

See also

Triangle.style

Returns a Styler for the Triangle.

Examples

import chainladder as cl

raa = cl.load_sample("raa")
raa.link_ratio.style.format(precision=1)

Please see: Triangle Visualization for more examples.

apply_from_triangle(mask: Triangle, color: str = 'blue', text_color: str | None = None, props: str | None = None) → Styler[source]#

Apply CSS styles to cells where the supplied mask Triangle has non-missing (or truthy) values.

Parameters:
mask: Triangle

A single (2-D) Triangle matching the shape of the styled Triangle. Cells where mask is not NaN (or True if boolean) will be styled.

color: str

Background color applied to selected cells. Ignored if props is given. Defaults to “blue”.

text_color: str | None

Text color applied to selected cells. Ignored if props is given. Left unstyled (inherited) if not given.

props: str | None

A full CSS properties string to apply instead of color and text_color. Optional.

Returns:
Styler
Raises:
TypeError

If mask is not a Triangle instance.

ValueError

If mask is multidimensional, empty, or does not match the 2-D shape of the styled Triangle.

Examples

import chainladder as cl

raa = cl.load_sample("raa")
# Highlight latest diagonal via valuation slicing
raa.style.apply_from_triangle(
    raa[raa.valuation == raa.valuation_date], color="#FFE599"
)
highlight_diagonal(color: str = '#FFE599', text_color: str | None = None, props: str | None = None, valuation: int | float | str | date | datetime | Timestamp | Literal['latest'] = 'latest', **kwargs: Any) → Styler[source]#

Highlight a diagonal – the cells corresponding to a specific valuation date – with a style.

Parameters:
color: str

Background color applied to diagonal cells. Ignored if props is given. Defaults to “#FFE599”.

text_color: str | None

Text color applied to diagonal cells. Ignored if props is given. Left unstyled (i.e. inherited) if not given.

props: str | None

A full CSS properties string to apply instead of color and text_color, e.g. "background-color: blue; opacity: 60%;". Optional.

valuation: ValuationDateLike | Literal[“latest”]

The valuation date of the diagonal to highlight. Can be "latest" to highlight the most recent observed diagonal, or a specific date (such as a year, date string, or datetime). Defaults to "latest".

**kwargs: Any

Additional keyword arguments. Supports valuation_date as an alias for valuation.

Returns:
Styler
Raises:
ValueError

If the wrapped Triangle is a valuation Triangle, or if the specified valuation date is not present in the Triangle.

Examples

import chainladder as cl

cl.load_sample("raa").style.highlight_diagonal(color="lightyellow")

Highlight a specific historical valuation diagonal:

cl.load_sample("raa").style.highlight_diagonal(
    valuation="1988", color="#FFE599"
)

Chaining diagonal and lower triangle highlighting:

(
    cl
    .load_sample("raa")
    .style.highlight_diagonal(color="#FFF2CC")
    .highlight_lower_triangle(color="#DDEBF7")
)
highlight_lower_triangle(color: str = 'blue', text_color: str | None = None, props: str | None = None, valuation_date: int | float | str | date | datetime | Timestamp | None = None) → Styler[source]#

Highlight the lower triangle – the cells beyond the Triangle’s latest diagonal – with a style.

Parameters:
color: str

Background color applied to lower-triangle cells. Ignored if props is given. Defaults to “blue”.

props: str | None

A full CSS properties string to apply instead of color and text_color, e.g. "background-color: blue; opacity: 60%;". Optional.

valuation_date: ValuationDateLike | None

The “as of” date used to determine the diagonal beyond which the cells are highlighted. If None, defaults to the lastest diagonal.

text_color: str | None

Text color applied to lower-triangle cells. Ignored if props is given. Left unstyled (i.e. inherited) if not given.

Returns:
Styler
Raises:
ValueError

If the wrapped Triangle is a valuation Triangle.

Examples

import chainladder as cl

cl.load_sample("raa").style.highlight_lower_triangle(color="lightgray")

A softer, higher-contrast pairing than the default:

cl.load_sample("raa").style.highlight_lower_triangle(
    color="#BDD7EE", text_color="#1F4E78"
)

A fully-predicted Triangle needs no valuation date, even though its cells are no longer NaN.

raa = cl.load_sample("raa")
full = cl.Chainladder().fit(raa).full_triangle_
full.style.highlight_lower_triangle(color="lightgray")

Inherited Methods

Styler.apply

Apply a CSS-styling function column-wise, row-wise, or table-wise.

Styler.apply_index

Apply a CSS-styling function to the index or column headers, level-wise.

Styler.applymap

Apply a CSS-styling function elementwise.

Styler.applymap_index

Apply a CSS-styling function to the index or column headers, elementwise.

Styler.background_gradient

Color the background in a gradient style.

Styler.bar

Draw bar chart in the cell backgrounds.

Styler.clear

Reset the Styler, removing any previously applied styles.

Styler.concat

Append another Styler to combine the output into a single table.

Styler.export

Export the styles applied to the current Styler.

Styler.format

Format the text display value of cells.

Styler.format_index

Format the text display value of index labels or column headers.

Styler.from_custom_template

Factory function for creating a subclass of Styler.

Styler.hide

Hide the entire index / column headers, or specific rows / columns from display.

Styler.highlight_between

Highlight a defined range with a style.

Styler.highlight_max

Highlight the maximum with a style.

Styler.highlight_min

Highlight the minimum with a style.

Styler.highlight_null

Highlight missing values with a style.

Styler.highlight_quantile

Highlight values defined by a quantile with a style.

Styler.map

Apply a CSS-styling function elementwise.

Styler.map_index

Apply a CSS-styling function to the index or column headers, elementwise.

Styler.pipe

Apply func(self, *args, **kwargs), and return the result.

Styler.relabel_index

Relabel the index, or column header, keys to display a set of specified values.

Styler.set_caption

Set the text added to a <caption> HTML element.

Styler.set_properties

Set defined CSS-properties to each <td> HTML element for the given subset.

Styler.set_sticky

Add CSS to permanently display the index or column headers in a scrolling frame.

Styler.set_table_attributes

Set the table attributes added to the <table> HTML element.

Styler.set_table_styles

Set the table styles included within the <style> HTML element.

Styler.set_td_classes

Set the class attribute of <td> HTML elements.

Styler.set_tooltips

Set the DataFrame of strings on Styler generating :hover tooltips.

Styler.set_uuid

Set the uuid applied to id attributes of HTML elements.

Styler.text_gradient

Color the text in a gradient style.

Styler.to_excel

Write Styler to an Excel sheet.

Styler.to_html

Write Styler to a file, buffer or string in HTML-CSS format.

Styler.to_latex

Write Styler to a file, buffer or string in LaTeX format.

Styler.to_string

Write Styler to a file, buffer or string in text format.

Styler.use

Set the styles on the current Styler.