From 948b1a87b85d9d4f93e086b0e96629a3c04b895c Mon Sep 17 00:00:00 2001 From: Kenneth Hsu Date: Thu, 4 Apr 2024 18:59:59 -0700 Subject: [PATCH] Addressed 420 and 491 --- chainladder/core/display.py | 26 +++++++++++++----- chainladder/core/tests/test_display.py | 13 +++++++++ chainladder/core/tests/test_triangle.py | 35 +++++++++++-------------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/chainladder/core/display.py b/chainladder/core/display.py index e0a135ee..282ad846 100644 --- a/chainladder/core/display.py +++ b/chainladder/core/display.py @@ -13,16 +13,22 @@ class TriangleDisplay: def __repr__(self): - # try: - # self.values - # except: - # print("Triangle is empty") - # return + # print("IN __repr__\n") + try: + self.values + except: + # print("Triangle is empty") + return "Empty Triangle." if (self.values.shape[0], self.values.shape[1]) == (1, 1): data = self._repr_format() + # print("printing triangle\n") + # print(data.to_string()) return data.to_string() + else: + # print("printing triangle summary") + # print(self._summary_frame().__repr__()) return self._summary_frame().__repr__() def _summary_frame(self): @@ -40,6 +46,13 @@ def _summary_frame(self): def _repr_html_(self): """Jupyter/Ipython HTML representation""" + # print("IN _repr_html_\n") + try: + self.values + except: + # print("Triangle is empty") + return "Triangle is empty." + if (self.values.shape[0], self.values.shape[1]) == (1, 1): data = self._repr_format() fmt_str = self._get_format_str(data) @@ -70,6 +83,7 @@ def _get_format_str(self, data): return "{:,.0f}" def _repr_format(self, origin_as_datetime=False): + # print("IN _repr_format") out = self.compute().set_backend("numpy").values[0, 0] if origin_as_datetime and not self.is_pattern: origin = self.origin.to_timestamp(how="s") @@ -150,7 +164,7 @@ def heatmap(self, cmap="coolwarm", low=0, high=0, axis=0, subset=None): ) output_xnan = re.sub("", "", default_output) else: - raise ValueError("heatmap only works with single triangles") + raise ValueError("heatmap() only works with a single triangle") if HTML: return HTML(output_xnan) elif HTML is None: diff --git a/chainladder/core/tests/test_display.py b/chainladder/core/tests/test_display.py index 0b11b1fb..b2cdebac 100644 --- a/chainladder/core/tests/test_display.py +++ b/chainladder/core/tests/test_display.py @@ -26,3 +26,16 @@ def test_to_frame(raa): except: assert False + + +def test_labels(xyz): + assert ( + xyz.valuation_date.strftime("%Y-%m-%d %H:%M:%S.%f") + == "2008-12-31 23:59:59.999999" + ) + assert xyz.origin_grain == "Y" + assert xyz.development_grain == "Y" + assert xyz.shape == (1, 5, 11, 11) + assert xyz.index_label == ["Total"] + assert xyz.columns_label == ["Incurred", "Paid", "Reported", "Closed", "Premium"] + assert xyz.origin_label == ["AccidentYear"] diff --git a/chainladder/core/tests/test_triangle.py b/chainladder/core/tests/test_triangle.py index c623ff0d..8fd585db 100644 --- a/chainladder/core/tests/test_triangle.py +++ b/chainladder/core/tests/test_triangle.py @@ -665,7 +665,7 @@ def test_halfyear_grain(): {"AccMo": [201409, 201503, 201603], "ValMo": [202203] * 3, "value": [100] * 3} ) assert cl.Triangle( - data=data, origin="AccMo", development="ValMo", columns="value" + data=data, origin="AccMo", development="ValMo", columns="value", cumulative=True ).shape == (1, 1, 16, 1) @@ -775,21 +775,18 @@ def test_halfyear_development(): ) ) == cl.Triangle - assert ( - cl.Triangle( - data=pd.DataFrame(data, columns=["origin", "val_date", "idx", "value"]), - index="idx", - columns="value", - origin="origin", - development="val_date", - cumulative=True, - ) == - cl.Triangle( - data=df_polars, - index="idx", - columns="value", - origin="origin", - development="val_date", - cumulative=True, - ) - ) \ No newline at end of file + assert cl.Triangle( + data=pd.DataFrame(data, columns=["origin", "val_date", "idx", "value"]), + index="idx", + columns="value", + origin="origin", + development="val_date", + cumulative=True, + ) == cl.Triangle( + data=df_polars, + index="idx", + columns="value", + origin="origin", + development="val_date", + cumulative=True, + )