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
4 changes: 3 additions & 1 deletion chainladder/core/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ def key_to_slice(self, key: _LabelKey) -> tuple[_AxisKey, _AxisKey, _AxisKey, _A
return out

def __setitem__(self, key: _LabelKey, values: int | float | TriangleSlicer) -> None:
super().__setitem__(cast(tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey], self.key_to_slice(key)), values)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I missed filling out a docstring when I annotated the file. Could you fill it out?

raw_slice = self.key_to_slice(key)
contig_slice = [_LocBase._contig_slice(x) for x in raw_slice]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about self._contig_slice()? And a comment about why we need to pass a contiguous slice instead of just a raw one to super().__setitem__()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about self._contig_slice()?

i copied it from line 66.

And a comment about why we need to pass a contiguous slice instead of just a raw one to super().setitem()

while trying to write an explanation, i realized that the explanation wouldn't be completely true, i.e. the slice doesn't actually need to be contiguous for axis 0 and 1 (lines 70-76).

should we implement a setter that's as flexible (non-contig index and col) as the getter?

super().__setitem__(cast(tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey], contig_slice), values)

class Ilocation(_LocBase):
"""
Expand Down
22 changes: 22 additions & 0 deletions chainladder/core/tests/test_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,29 @@ def test_loc_setitem_triangle_value(clrd: Triangle) -> None:
tri.loc["Aegis Grp", "comauto"] = sub * 2
assert tri.loc["Aegis Grp", "comauto"] == sub * 2

def test_loc_setitem_partial_triangles(raa: Triangle) -> None:
"""
Use Triangle.loc to set a few origin or develop period via a TriangleSlicer.

Parameters
----------
raa: Triangle
The raa sample data set fixture.

Returns
-------
None

"""
raa2 = raa * 2
raa_new = raa.copy()
raa_new.loc[:,:,:,:60] = raa2.loc[:,:,:,:60]
raa_new.loc[:,:,:,72:] = raa2.loc[:,:,:,72:]
assert raa_new == raa2
raa_new = raa.copy()
raa_new.loc[:,:,:'1984',:] = raa2.loc[:,:,:'1984',:]
raa_new.loc[:,:,'1985':,:] = raa2.loc[:,:,'1985':,:]
assert raa_new == raa2

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse parametrization breaks new test

Medium Severity

test_loc_setitem_partial_triangles assigns through Triangle.loc without checking array_backend. The raa fixture is parametrized with sparse_only_run, where _LocBase.__setitem__ always raises when setting via .loc. The sibling test_loc_setitem_triangle_value guards sparse; this test does not, so half the parametrized runs fail.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit de661e2. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@genedan cursor's explanation makes sense. but why did all tests pass?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bug in the fixtures. Turns out raa is never sparse during the sparse_only_run, the backend gets set to numpy because of _auto_sparse(), so we've been doing 2 runs of numpy-backed raa triangles the whole time.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to fix that fixture issue??

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we do, I think once raa is properly set to sparse, we might catch all kinds of stuff. It could be sizeable issue to tackle.

If you do a manual run of the test code with the raa backend set to sparse and confirm that it's ok I can approve this. Then we can tackle the fixture issue separately.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it fails.

i'm debating whether we should extend loc/iloc setting to sparse (i believe you had mentioned it on another issue/PR). it is definitely theoretically possible. but i kinda get why john explicitly didn't implement it. setting via a slice defeats the whole purpose of sparse, i.e. each 2D triangle only having a couple of valid values.

so i'm torn between adding a sparse exception and move on or actually adding sparse support.


def test_invalid_iloc_sparse_assignment(prism) -> None:
"""
Expand Down
Loading