Skip to content

[ENH] set partial triangle through loc - #1103

Merged
henrydingliu merged 13 commits into
mainfrom
loc_setter
Aug 12, 2026
Merged

[ENH] set partial triangle through loc#1103
henrydingliu merged 13 commits into
mainfrom
loc_setter

Conversation

@henrydingliu

@henrydingliu henrydingliu commented Jul 10, 2026

Copy link
Copy Markdown
Member

Summary of Changes

adding support to set partial triangles via loc and iloc
adding support to set non-contig index and columns via loc and iloc
fixing a bug where slice(none:none:2) would become slice(none:none:none) during setting.

Related GitHub Issue(s)

closes #1069

Additional Context for Reviewers

the heavy lifting is done by the existing _contig_slice function, which is already used in the getter. not sure why it was never applied in the setter

  • I passed tests locally for both code (uv run pytest) and documentation changes (uv run jb build docs --builder=custom --custom-builder=doctest)

Note

Medium Risk
Changes core Triangle indexing write paths and array mutation semantics; scope is bounded to numpy backend with new tests, but incorrect np.ix_ alignment could corrupt triangle data.

Overview
Extends numpy-backend loc/iloc assignment so you can write partial triangles (origin/development ranges and labels) and copy non-contiguous index/column selections—behavior that previously worked on read but not on write.

The setter now runs keys through _contig_slice (same as the getter), uses np.ix_ when both index and column axes are fancy arrays (matching the getter’s Cartesian product), and rejects fancy indexing on origin/development with a clear error. _normalize_index no longer drops explicit slice steps (e.g. ::2), which had broken strided assignments.

Minor cleanups: is not for type checks, np.where(check) without == True, and docstrings. Tests cover partial triangle sets, non-contiguous loc/iloc sets, and set-side fancy origin/development errors (numpy backend only; sparse still requires .at/.iat).

Reviewed by Cursor Bugbot for commit 38765f1. Bugbot is set up for automated code reviews on this repo. Configure here.

henrydingliu and others added 4 commits July 2, 2026 07:10
* enable loc setting on partial triangles

* fixing

* adding test
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.26%. Comparing base (6ca44ed) to head (38765f1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1103      +/-   ##
==========================================
- Coverage   91.48%   91.26%   -0.22%     
==========================================
  Files          91       91              
  Lines        5552     5401     -151     
  Branches      736      691      -45     
==========================================
- Hits         5079     4929     -150     
  Misses        338      338              
+ Partials      135      134       -1     
Flag Coverage Δ
unittests 91.26% <100.00%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

Pyright Type Completeness

View the full pyright --verifytypes output for this commit

Project (full chainladder package, at this PR's head): 15.1% of exported symbols fully typed (201 / 1333)

Known Ambiguous Unknown Total
Project (head) 201 111 1021 1333

Other symbols referenced but not exported by chainladder: 13

Known Ambiguous Unknown Total
Other (head) 3 1 9 13

Symbols without documentation:

  • Functions without docstring: 321
  • Functions without default param: 0
  • Classes without docstring: 10

Patch (exported symbols added or changed by this PR): 0.0% fully typed (0 / 5)

Known Ambiguous Unknown Total
Patch 0 0 5 5
Patch symbol details
Symbol Status Change
chainladder.core.tests.test_slicing.test_iloc_setting_non_contiguous_index_and_columns ❌ unknown new
chainladder.core.tests.test_slicing.test_loc_setitem_partial_triangles ❌ unknown new
chainladder.core.tests.test_slicing.test_loc_setting_non_contiguous_index_and_columns ❌ unknown new
chainladder.core.tests.test_slicing.test_set_fancy_development_raises ❌ unknown new
chainladder.core.tests.test_slicing.test_set_fancy_origin_raises ❌ unknown new

Comment thread chainladder/core/tests/test_slicing.py Outdated
Comment thread chainladder/core/slice.py
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?

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.

done

Comment thread chainladder/core/slice.py Outdated
Comment thread chainladder/core/tests/test_slicing.py Outdated
@kennethshsu

Copy link
Copy Markdown
Member

How are we doing here with this PR? Can I help?

@genedan

genedan commented Jul 24, 2026

Copy link
Copy Markdown
Member

@kennethshsu that comment I made about the dense version of raa (and other fixtures, possibly) being run twice instead of alternating between dense/sparse as intended could definitely use assistance. It'll need it's own issue created and PR to remedy. I think it might reveal a lot more test failures too once we fix it.

@henrydingliu

Copy link
Copy Markdown
Member Author

@kennethshsu pausing till the sparse test fixture is fixed

@genedan i'd like your feedback on

  1. i'd like to continue to observe John's intent around no loc/iloc setting for sparse.
  2. I'd like to enable non-contig setting if the non-contig dims are index and column.

@henrydingliu
henrydingliu marked this pull request as draft July 29, 2026 22:58
@genedan

genedan commented Jul 30, 2026

Copy link
Copy Markdown
Member
  1. Agreed, while theoretically we could enable the feature there are other things more worth our time and this can remain as-is unless a bunch of users ask for it.
  2. Agreed.

Comment thread chainladder/core/slice.py

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 64329cc. Configure here.

Comment thread chainladder/core/slice.py Outdated
@henrydingliu
henrydingliu marked this pull request as ready for review August 1, 2026 22:56
Comment thread chainladder/core/slice.py Outdated
cast(np.ndarray, cast(object, self.obj.values)).__setitem__(norm_key, values)
else:
#the getter uses arr[idx,:][:,idx] to get the Cartesian product, using np.ix_ on the setter to match
cast(np.ndarray, cast(object, self.obj.values)).__setitem__(np.ix_(norm_key[0], norm_key[1])+(norm_key[2], norm_key[3]), 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.

Could you merge in the changes from main? I'd like to see if Ruff catches this line (should be a space before and after the +):

norm_key[1])+(norm_key[2],

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.

image

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.

lol, turns out we don't have the technology

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.

Hmmm looks like it caught other stuff, but not this specific spacing issue I pointed out. I'll need to add the E2 ruleset to the checks.

This Ruff stuff may take us some trial and error to work out. We may want to add/exclude certain rules as we go along. For example E2 is a very broad category of whitespace rules that includes the more granular E225. I think we want most of the things in the E2 category, so that's what I picked - I figure it's easier to go broad and exclude what we don't like than to add very granular rules piecemeal.

@henrydingliu

Copy link
Copy Markdown
Member Author

@kennethshsu can you please take a look at the failing rtd test? there's no config changes in this PR, so I don't know what could have triggered the rtd fail

@henrydingliu

Copy link
Copy Markdown
Member Author

@kennethshsu can you please take a look at the failing rtd test? there's no config changes in this PR, so I don't know what could have triggered the rtd fail

seems to be a bug from rtd. everything is working now. please disregard

@kennethshsu

Copy link
Copy Markdown
Member

@kennethshsu can you please take a look at the failing rtd test? there's no config changes in this PR, so I don't know what could have triggered the rtd fail

I saw something weird that I think you are not supposed to do. In #1201 you opened a PR from main into loc_setter and I think that caused some problems, because now head is on loc_setter, and it's failing. Can you do a reset and instead of using PR, pull directly? So that head is still on main?

@kennethshsu

Copy link
Copy Markdown
Member
Screenshot 2026-08-12 at 1 07 13 PM

@kennethshsu can you please take a look at the failing rtd test? there's no config changes in this PR, so I don't know what could have triggered the rtd fail

seems to be a bug from rtd. everything is working now. please disregard

checks are still failing because of #1201.

@kennethshsu

Copy link
Copy Markdown
Member

Why do you use PR when you need main into loc_setter instead of just pull?

@henrydingliu

Copy link
Copy Markdown
Member Author

Why do you use PR when you need main into loc_setter instead of just pull?

cuz i can do this from my phone while i'm eating lunch

@henrydingliu

Copy link
Copy Markdown
Member Author
Screenshot 2026-08-12 at 1 07 13 PM > > @kennethshsu can you please take a look at the failing rtd test? there's no config changes in this PR, so I don't know what could have triggered the rtd fail > > > seems to be a bug from rtd. everything is working now. please disregard

checks are still failing because of #1201.

everything was failing for a while. now it's working again

image

@kennethshsu

Copy link
Copy Markdown
Member

I think it's because loc_setter is now head instead of main, so things will pass when loc_setter passes, it will link to main.

Don't PR from main, just pull.

@henrydingliu

Copy link
Copy Markdown
Member Author

Don't PR from main, just pull.

What's the difference? isn't pr just approval for pull?

@kennethshsu

Copy link
Copy Markdown
Member

I think the main difference is the reappointment of "head".

If your dev feature has a check failure, that will show up on main if the head is reappointed (now pointing at the feature branch), if you just pull (not PR), the head will still stay on main until a PR is opened and merge into main, then that reappointment of head will happen (and now everything is in sync).

@kennethshsu

Copy link
Copy Markdown
Member

And also if you open a PR (from main into dev) all code owners get spammed lol

@henrydingliu

Copy link
Copy Markdown
Member Author

roger roger

@henrydingliu
henrydingliu merged commit 52e655b into main Aug 12, 2026
23 of 24 checks passed
@henrydingliu
henrydingliu deleted the loc_setter branch August 12, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Location setter erroring unexpectedly

3 participants