diff --git a/chainladder/core/io.py b/chainladder/core/io.py index e1588be3..12f637ec 100644 --- a/chainladder/core/io.py +++ b/chainladder/core/io.py @@ -26,7 +26,14 @@ def sparse_out(tri): return json.dumps(dict(zip([str(item) for item in zip(coo.row, coo.col)], coo.data))) json_dict = {} - attributes = ['kdims', 'vdims', 'odims', 'ddims'] + if self.is_val_tri: + ddims = self.ddims.to_timestamp(how='e') + json_dict['ddims'] = { + 'dtype': str(ddims.dtype), + 'array': ddims.values.tolist()} + attributes = ['kdims', 'vdims', 'odims'] + else: + attributes = ['kdims', 'vdims', 'odims', 'ddims'] for attribute in attributes: json_dict[attribute] = { 'dtype': str(getattr(self, attribute).dtype), @@ -46,6 +53,7 @@ def sparse_out(tri): json_dict['development_grain'] = self.development_grain json_dict['nan_override'] = self.nan_override json_dict['is_cumulative'] = self.is_cumulative + json_dict['is_val_tri'] = self.is_val_tri json_dict['valuation_date'] = self.valuation_date.strftime('%Y-%m-%d') return json.dumps(json_dict) diff --git a/chainladder/core/tests/test_triangle.py b/chainladder/core/tests/test_triangle.py index 40e811b3..1c55cd7e 100644 --- a/chainladder/core/tests/test_triangle.py +++ b/chainladder/core/tests/test_triangle.py @@ -293,3 +293,14 @@ def test_slicers_honor_order(): assert clrd.iloc[[1,0], [1, 0]].iloc[0, 0] == clrd.iloc[1, 1] #col assert clrd.loc[:,['CumPaidLoss','IncurLoss']].iloc[0, 0] == clrd.iloc[0,1] assert clrd.loc[['ppauto', 'medmal'],['CumPaidLoss','IncurLoss']].iloc[0,0] == clrd.iloc[3]['CumPaidLoss'] + +def test_exposure_tri(): + x = cl.load_dataset('auto') + x= x[x.development==12] + x = x['paid'].to_frame().T.unstack().reset_index() + x.columns=['LOB', 'origin', 'paid'] + x.origin = x.origin.astype(str) + y = cl.Triangle(x, origin='origin', index='LOB', columns='paid') + x = cl.load_dataset('auto')['paid'] + x = x[x.development==12] + assert x == y diff --git a/chainladder/utils/tests/test_utilities.py b/chainladder/utils/tests/test_utilities.py index 50e43a0d..39fe5675 100644 --- a/chainladder/utils/tests/test_utilities.py +++ b/chainladder/utils/tests/test_utilities.py @@ -24,6 +24,10 @@ def test_triangle_json_io(): xp.testing.assert_array_equal(clrd.ddims, clrd2.ddims) assert np.all(clrd.valuation == clrd2.valuation) +def test_json_for_val(): + x = cl.load_dataset('raa').dev_to_val().to_json() + cl.read_json(x) == cl.load_dataset('raa').dev_to_val() + def test_estimator_json_io(): assert cl.read_json(cl.Development().to_json()).get_params() == \ cl.Development().get_params() diff --git a/chainladder/utils/utility_functions.py b/chainladder/utils/utility_functions.py index 19b012c8..71746155 100644 --- a/chainladder/utils/utility_functions.py +++ b/chainladder/utils/utility_functions.py @@ -78,6 +78,8 @@ def sparse_in(json_str, dtype, shape): 'nan_override', 'is_cumulative'] for prop in properties: setattr(tri, prop, json_dict[prop]) + if json_dict.get('is_val_tri', False): + tri.ddims = pd.PeriodIndex(tri.ddims, freq=tri.development_grain) tri.valuation_date = pd.to_datetime( json_dict['valuation_date'], format='%Y-%m-%d') tri._set_slicers()