Skip to content
Open
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
24 changes: 19 additions & 5 deletions chainladder/core/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
from collections.abc import Callable
from numpy.typing import ArrayLike
from types import ModuleType
from typing import Type
from typing import (
Literal,
Self,
Type
)



Expand Down Expand Up @@ -308,7 +312,11 @@ def append(self, other):

return concat((self, other), 0)

def rename(self, axis, value):
def rename(
self,
axis: Literal['index', 'columns', 'origin', 'development'] | int,
value: list | str
) -> Self:
"""Alter axes labels.

Parameters
Expand All @@ -328,12 +336,18 @@ def rename(self, axis, value):
value = [value] if type(value) is str else value
if axis == "index" or axis == 0:
self.index = value
if axis == "columns" or axis == 1:
elif axis == "columns" or axis == 1:
self.columns = value
if axis == "origin" or axis == 2:
elif axis == "origin" or axis == 2:
self.origin = value
if axis == "development" or axis == 3:
elif axis == "development" or axis == 3:
self.development = value
else:
raise ValueError(
"Invalid value provided to the 'axis' parameter. Accepted values are a string of 'index', "
"'columns', 'origin', or 'development', or an integer in the interval [0, 4] specifying the"
" axis to be modified."
)
return self

def astype(self, dtype, inplace=True):
Expand Down
24 changes: 24 additions & 0 deletions chainladder/core/tests/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ def test_append(raa):
raa.append(raa2).sum() == raa * 2
assert raa.append(raa2).sum() == 2 * raa

def test_rename_columns(genins, clrd) -> None:
"""
Test the renaming of triangle columns.
"""
# Scalar case - single column triangle.
genins.rename('columns', 'foo')

assert genins.columns.to_list() == ['foo']

# Test the cascading of rename to triangle.columns_label.
assert genins.columns_label == ['foo']

def test_rename_exception(genins, clrd) -> None:
# Test incorrect axis argument - misspelling of string.
with pytest.raises(ValueError):
genins.rename('colunms', 'foo')

# Test incorrect axis integer.
with pytest.raises(ValueError):
genins.rename(4, 'foo')

# Test incorrect number of columns.
with pytest.raises(ValueError):
clrd.rename('columns', ['foo'])

def test_assign_existing_col(qtr):
out = qtr.copy()
Expand Down
5 changes: 4 additions & 1 deletion chainladder/core/triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def __init__(
)

# Store dimension metadata.
self.columns_label: list = columns
self.origin_label: list = origin

# Handle any ultimate vectors in triangles separately.
Expand Down Expand Up @@ -401,6 +400,10 @@ def columns(self, value):
self.vdims = np.array(self.vdims)
self._set_slicers()

@property
def columns_label(self) -> list:
return self.columns.to_list()

@property
def origin(self):
if self.is_pattern and len(self.odims) == 1:
Expand Down