Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 15 additions & 2 deletions chainladder/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy as np
import warnings

from abc import ABC, abstractmethod

from chainladder import options

from chainladder.core.common import Common
Expand Down Expand Up @@ -44,7 +46,8 @@ class TriangleBase(
TriangleSlicer,
TriangleDunders,
TrianglePandas,
Common
Common,
ABC
):
"""This class handles the initialization of a triangle"""

Expand Down Expand Up @@ -309,6 +312,16 @@ def _get_date_axes(
)

return c[c["__development__"] > c["__origin__"]]

@property
@abstractmethod
def is_pattern(self) -> bool:
raise NotImplementedError

@property
@abstractmethod
def is_ultimate(self) -> bool:
raise NotImplementedError

@property
def nan_triangle(self):
Expand All @@ -317,7 +330,7 @@ def nan_triangle(self):
This becomes useful when managing array arithmetic.
"""
xp = self.get_array_module()
if min(self.values.shape[2:]) == 1:
if self.is_pattern or self.is_ultimate:
return xp.ones(self.values.shape[2:], dtype="float16")
val_array = np.array(self.valuation).reshape(self.shape[-2:], order="f")
nan_triangle = np.array(pd.DataFrame(val_array) > self.valuation_date)
Expand Down
20 changes: 19 additions & 1 deletion chainladder/core/tests/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,22 @@ def test_fillzero():
raa = cl.load_sample('raa')
zero = raa - raa[raa.origin=='1982']
filled = zero.fillzero()
assert (filled[filled.origin == '1982'][filled.development == 24].values.flatten()[0]) == 0
assert (filled[filled.origin == '1982'][filled.development == 24].values.flatten()[0]) == 0

def test_2x2_triangle():

df = pd.DataFrame(data={
'origin': [2022, 2022, 2023],
'development': [2022, 2023, 2023],
'reported': [78000, 222000, 78000]}
)
tri_from_df = cl.Triangle(
data=df,
origin='origin',
development='development',
columns=['reported'],
cumulative=True
)
tri_from_df
assert np.array_equal(tri_from_df.cum_to_incr().values,np.array([[[[ 78000., 144000.],
[ 78000., np.float64(np.nan)]]]]), equal_nan=True)
13 changes: 11 additions & 2 deletions chainladder/core/triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def __init__(

self.is_cumulative: bool = cumulative
self.virtual_columns = VirtualColumns(self)
self.is_pattern: bool = pattern
self._pattern: bool = pattern

split: list[str] = self.origin_grain.split("-")
self.origin_grain: str = {"A": "Y", "2Q": "S"}.get(split[0], split[0])
Expand Down Expand Up @@ -484,8 +484,17 @@ def is_full(self) -> bool:

return self.nan_triangle.sum().sum() == np.prod(self.shape[-2:])


@property
def is_pattern(self) -> bool:
return self._pattern

@is_pattern.setter
def is_pattern(self, pattern: bool):
self._pattern = pattern

@property
def is_ultimate(self):
def is_ultimate(self) -> np.bool:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewer requested -> bool but got -> np.bool

Low Severity

The is_ultimate property uses -> np.bool as the return type annotation. The PR reviewer explicitly requested -> bool type hints for the getter and setter. Additionally, np.bool was removed in numpy 1.24 and only restored in numpy 2.0, making it an unreliable alias. The function actually returns a Python bool (from int > 0), so the annotation is also technically inaccurate. This violates the reviewer's explicit request: "Add type hints to the getter and setter (-> bool)".

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2efef59. Configure here.

return sum(self.valuation >= options.ULT_VAL[:4]) > 0

@property
Expand Down
Loading