Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions chainladder/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@

@property
def shape(self):
"""The 4-D shape of the Triangle: ``(index, columns, origin, development)``.

Examples
--------
A single-triangle sample such as RAA has one index and one column.

.. testsetup::

import chainladder as cl

.. testcode::

print(cl.load_sample('raa').shape)
print(cl.load_sample('clrd').shape)

.. testoutput::

(1, 1, 10, 10)
(775, 6, 10, 10)
"""
return self.values.shape

@property
Expand All @@ -69,6 +89,24 @@
Returns ``'empty'`` for a Triangle instantiated without data
(e.g. ``cl.Triangle()``), ``'single'`` for a Triangle holding a
single triangle, and ``'multi'`` for a multidimensional Triangle.

Examples
--------
.. testsetup::

import chainladder as cl

.. testcode::

print(cl.Triangle().dimensionality)
print(cl.load_sample('raa').dimensionality)
print(cl.load_sample('clrd').dimensionality)

.. testoutput::

empty
single
multi
"""
return self._dimensionality

Expand All @@ -79,6 +117,22 @@
Mirrors ``pandas.DataFrame.empty``. Returns ``True`` for a Triangle
instantiated without data (e.g. ``cl.Triangle()``), whose ``values``
have not been populated, and ``False`` otherwise.

Examples
--------
.. testsetup::

import chainladder as cl

.. testcode::

print(cl.Triangle().empty)
print(cl.load_sample('raa').empty)

.. testoutput::

True
False
"""
return self._dimensionality == "empty"

Expand Down Expand Up @@ -166,7 +220,7 @@
columns: list
):
"""Summarize dataframe to the level specified in axes"""
if type(data) != pd.DataFrame:

Check failure on line 223 in chainladder/core/base.py

View workflow job for this annotation

GitHub Actions / ruff check

ruff (E721)

chainladder/core/base.py:223:12: E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
# A non-pandas input that reaches this branch is a Dask dataframe.
# Only the Dask backend is deprecated, so gate the warning on the
# data's module rather than warning for every pandas subclass that
Expand Down Expand Up @@ -367,6 +421,31 @@
"""Given the current triangle shape and valuation, it determines the
appropriate placement of NANs in the triangle for future valuations.
This becomes useful when managing array arithmetic.

Examples
--------
Observed cells are ``1`` and future valuations are missing.

.. testsetup::

import chainladder as cl

.. testcode::

print(cl.load_sample('raa').nan_triangle)

.. testoutput::

[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. nan]
[ 1. 1. 1. 1. 1. 1. 1. 1. nan nan]
[ 1. 1. 1. 1. 1. 1. 1. nan nan nan]
[ 1. 1. 1. 1. 1. 1. nan nan nan nan]
[ 1. 1. 1. 1. 1. nan nan nan nan nan]
[ 1. 1. 1. 1. nan nan nan nan nan nan]
[ 1. 1. 1. nan nan nan nan nan nan nan]
[ 1. 1. nan nan nan nan nan nan nan nan]
[ 1. nan nan nan nan nan nan nan nan nan]]
"""
xp = self.get_array_module()
if self.is_pattern or self.is_ultimate:
Expand Down Expand Up @@ -401,7 +480,7 @@
target: Series = target_field
# If the target field is a period, convert to timestamp. period_end is a boolean that if true,
# means that the timestamp should be the end of the period.
if type(target.iloc[0]) == pd.Period:

Check failure on line 483 in chainladder/core/base.py

View workflow job for this annotation

GitHub Actions / ruff check

ruff (E721)

chainladder/core/base.py:483:16: E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
return target.dt.to_timestamp(how={1: "e", 0: "s"}[period_end])
else:
datetime_arg: np.ndarray = target_field.unique()
Expand Down Expand Up @@ -527,6 +606,29 @@
-------
The backend module. For example, if the backend is numpy, it will return the "np" that you
would get if you ran the statement, "import numpy as np".

Examples
--------
The returned module is the same object as ``numpy`` or ``sparse``,
matching the Triangle's ``array_backend``.

.. testsetup::

import chainladder as cl

.. testcode::

import numpy as np
import sparse as sp

raa = cl.load_sample('raa')
print(raa.get_array_module() is np)
print(raa.set_backend('sparse').get_array_module() is sp)

.. testoutput::

True
True
"""

backend: str = (
Expand Down Expand Up @@ -666,6 +768,17 @@
Returns
-------
Triangle

Examples
--------
Numpy- and sparse-backed Triangles are already materialized.
``compute`` exists to realize a lazy dask array. The dask backend is
deprecated and optional, so that path is shown as a code sample:

.. code-block:: python

tri = cl.load_sample('raa').set_backend('dask')
tri = tri.compute()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you fake the output also?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a pycon sample that shows tri.array_backend as 'numpy' after compute().

"""
if hasattr(self.values, "chunks"):
obj = self.copy()
Expand Down
5 changes: 4 additions & 1 deletion docs/_templates/autosummary/class_inherited.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

.. currentmodule:: {{ module }}

{% set documented_attrs = ['shape', 'empty', 'dimensionality', 'nan_triangle'] %}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are the changes to this template compatible with #1212 and #1213?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were not: last merge would overwrite the file. #1211, #1212, #1213, and #1208 now share the same union: un-exclude loc/iloc/at/iat plus shape/empty/dimensionality/nan_triangle, and special-members for the arithmetic dunders. cdf_/ibnr_/pct_reported_ stay excluded on Triangle.

{% set hidden_attrs = attributes | reject('in', documented_attrs) | list %}

.. autoclass:: {{ objname }}
:members:
:inherited-members:
:undoc-members:
:exclude-members: set_fit_request, set_predict_request, set_score_request, set_transform_request, {{ attributes | join(', ') }}
:exclude-members: set_fit_request, set_predict_request, set_score_request, set_transform_request{% if hidden_attrs %}, {{ hidden_attrs | join(', ') }}{% endif %}
Loading