diff --git a/chainladder/core/tests/test_triangle.py b/chainladder/core/tests/test_triangle.py index 45c1e649b..c8653b328 100644 --- a/chainladder/core/tests/test_triangle.py +++ b/chainladder/core/tests/test_triangle.py @@ -3304,3 +3304,22 @@ def test_fit_and_predict_on_single_origin(raa: Triangle, atol) -> None: np.testing.assert_allclose( predicted.ultimate_.values.flatten(), [expected], atol=atol ) + + +def test_fill(clrd: Triangle) -> None: + """ + ``Fill`` method works as intended + """ + fill_tri = clrd.iloc[2:4, 4:6].fill(100) + assert np.nansum(fill_tri.values) == 100 * 11 * 10 / 2 * 2 * 2 + assert np.nanmax(fill_tri.values) == 100 + assert np.nanmin(fill_tri.values) == 100 + + +def test_full_fill(raa: Triangle) -> None: + """ + ``Fill`` method works as intended on full triangle + """ + full_tri = cl.Chainladder().fit(raa).full_triangle_ + fill_full_tri = full_tri.fill(200) + assert np.all(fill_full_tri.values == np.broadcast_to([200], (1, 1, 10, 12))) diff --git a/chainladder/core/triangle.py b/chainladder/core/triangle.py index a496d7edc..ee36d45f6 100644 --- a/chainladder/core/triangle.py +++ b/chainladder/core/triangle.py @@ -2357,3 +2357,28 @@ def reindex(self, columns=None, fill_value=np.nan): if column not in obj.columns: obj[column] = fill_value return obj + + def fill(self, value: float = 1.0, inplace: bool = False) -> Triangle: + """ + Fill the Triangle with a scalar value + + Parameters + ---------- + value : float, default 1.0 + All valid elements will be assigned this value + inplace : bool, default False + Whether to mutate the existing Triangle instance or return a new + one. + + Returns + ------- + Triangle + """ + if inplace: + xp = self.get_array_module() + new_value = self.nan_triangle[None, None, ...].astype(np.float64) + self.values = xp.broadcast_to(new_value * value, self.shape).copy() + return self + else: + obj = self.copy() + return obj.fill(value, True) diff --git a/chainladder/utils/data/friedland_autoprop.csv b/chainladder/utils/data/friedland_autoprop.csv index 2a34498c2..30e9d0ba8 100644 --- a/chainladder/utils/data/friedland_autoprop.csv +++ b/chainladder/utils/data/friedland_autoprop.csv @@ -30,9 +30,9 @@ Accident Year,Calendar Year,Reported ALAE,Paid ALAE,Reported Claims,Paid Claims 2000,2007,862,860,116825,116807 2000,2008,862,861,116472,116807 2001,2001,629,480,129430,91475 -2001,2002,894,802,139925,113761 +2001,2002,894,802,139925,133761 2001,2003,943,882,138161,136143 -2001,2004,982,836,137395,136552 +2001,2004,982,936,137395,136552 2001,2005,997,975,137269,136818 2001,2006,1002,987,137033,136838 2001,2007,1003,995,136998,136960