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
triangleis anything other than a Triangle.- ValueError
If
triangleis 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.styleReturns 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
maskis not NaN (or True if boolean) will be styled.- color: str
Background color applied to selected cells. Ignored if
propsis given. Defaults to “blue”.- text_color: str | None
Text color applied to selected cells. Ignored if
propsis given. Left unstyled (inherited) if not given.- props: str | None
A full CSS properties string to apply instead of
colorandtext_color. Optional.
- Returns:
- Styler
- Raises:
- TypeError
If
maskis not a Triangle instance.- ValueError
If
maskis 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
propsis given. Defaults to “#FFE599”.- text_color: str | None
Text color applied to diagonal cells. Ignored if
propsis given. Left unstyled (i.e. inherited) if not given.- props: str | None
A full CSS properties string to apply instead of
colorandtext_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_dateas an alias forvaluation.
- 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
propsis given. Defaults to “blue”.- props: str | None
A full CSS properties string to apply instead of
colorandtext_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
propsis 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
|
Apply a CSS-styling function column-wise, row-wise, or table-wise. |
|
Apply a CSS-styling function to the index or column headers, level-wise. |
|
Apply a CSS-styling function elementwise. |
|
Apply a CSS-styling function to the index or column headers, elementwise. |
|
Color the background in a gradient style. |
|
Draw bar chart in the cell backgrounds. |
|
Reset the |
|
Append another Styler to combine the output into a single table. |
|
Export the styles applied to the current Styler. |
|
Format the text display value of cells. |
|
Format the text display value of index labels or column headers. |
|
Factory function for creating a subclass of |
|
Hide the entire index / column headers, or specific rows / columns from display. |
|
Highlight a defined range with a style. |
|
Highlight the maximum with a style. |
|
Highlight the minimum with a style. |
|
Highlight missing values with a style. |
|
Highlight values defined by a quantile with a style. |
|
Apply a CSS-styling function elementwise. |
|
Apply a CSS-styling function to the index or column headers, elementwise. |
|
Apply |
|
Relabel the index, or column header, keys to display a set of specified values. |
|
Set the text added to a |
|
Set defined CSS-properties to each |
|
Add CSS to permanently display the index or column headers in a scrolling frame. |
|
Set the table attributes added to the |
|
Set the table styles included within the |
|
Set the |
|
Set the DataFrame of strings on |
|
Set the uuid applied to |
|
Color the text in a gradient style. |
|
Write Styler to an Excel sheet. |
|
Write Styler to a file, buffer or string in HTML-CSS format. |
|
Write Styler to a file, buffer or string in LaTeX format. |
|
Write Styler to a file, buffer or string in text format. |
|
Set the styles on the current Styler. |