Skip to content

Commit

Permalink
minor patch to multi-index broadcasting and vector initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogaardt committed Aug 27, 2020
1 parent 8a63536 commit c66debd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions chainladder/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ def __init__(self, data=None, origin=None, development=None,
format=development_format)
self.development_grain = TriangleBase._get_grain(development_date)
else:
development_date = origin_date + \
pd.tseries.offsets.MonthEnd(m_cnt[self.origin_grain])
development_date = pd.PeriodIndex(
origin_date + pd.tseries.offsets.MonthEnd(m_cnt[self.origin_grain]),
freq={'Y': 'A'}.get(self.origin_grain, self.origin_grain)
).to_timestamp(how='e')
self.development_grain = self.origin_grain
development_date.name = 'development'
# Summarize dataframe to the level specified in axes
Expand Down Expand Up @@ -116,7 +118,8 @@ def __init__(self, data=None, origin=None, development=None,
# Set all axis values
self.kdims = kdims.drop('index', 1).values
self.odims = orig_unique
self.ddims = dev_lag_unique*m_cnt[self.development_grain]
self.ddims = dev_lag_unique if development else dev_lag[0:1].values
self.ddims = self.ddims*(m_cnt[self.development_grain])
self.vdims = np.array(columns)

# Set remaining triangle properties
Expand Down
9 changes: 9 additions & 0 deletions chainladder/core/tests/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,12 @@ def test_4loc():
clrd = tri.groupby('LOB').sum()
assert clrd.iloc[:3, :2, 0,0] == clrd[clrd.origin==tri.origin.min()][clrd.development==clrd.development.min()].loc['comauto':'othliab', :'CumPaidLoss', :, :]
assert clrd.iloc[:3, :2, 0:1, -1] == clrd[clrd.development==tri.development.max()].loc['comauto':'othliab', :'CumPaidLoss', '1988', :]

def test_init_vector():
a = raa[raa.development==12]
b = pd.DataFrame(
{'AccYear':[item for item in range(1981, 1991)],
'premium': [3000000]*10})
b = cl.Triangle(b, origin='AccYear', columns='premium')
assert np.all(a.valuation==b.valuation)
assert a.valuation_date == b.valuation_date
2 changes: 1 addition & 1 deletion chainladder/methods/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _align_cdf(self, ultimate, sample_weight=None):
"""
xp = ultimate.get_array_module()
from chainladder.utils.utility_functions import num_to_nan
if self.cdf_.key_labels != ultimate.key_labels:
if self.cdf_.key_labels != ultimate.key_labels and len(self.ldf_.index) > 1:
level = list(set(self.cdf_.key_labels).intersection(ultimate.key_labels))
idx = ultimate.index[level].merge(
self.cdf_.index[level].reset_index(), how='left', on=level)['index'].values
Expand Down
8 changes: 8 additions & 0 deletions docs/modules/triangle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ of the object using pandas-style ``loc``/``iloc`` or boolean filtering.
need for ``reset_index()`` or trying to boolean-filter a ``MultiIndex``. This is
a divergence from the pandas API.

As of version ``0.7.6``, four-dimensional slicing is supported:
**Example:**
>>> import chainladder as cl
>>> clrd = cl.load_sample('clrd')
>>> clrd.iloc[[0, 10, 3], 1:8, :5, :]
>>> clrd.loc[:'Aegis Grp', 'CumPaidLoss':, '1990':'1994', :48]



Arithmetic
----------
Expand Down

0 comments on commit c66debd

Please sign in to comment.