Skip to content

Conversation

@dnl-bsch
Copy link
Collaborator

@dnl-bsch dnl-bsch commented May 7, 2025

Description

Add a function to set one or more levels in a MultiIndex.

Checklist

Please confirm that this pull request has done the following:

  • Tests added
  • Documentation added (where applicable)
  • Changelog item added to changelog/

@codecov
Copy link

codecov bot commented May 7, 2025

Codecov Report

❌ Patch coverage is 97.61905% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.98%. Comparing base (e76eeb4) to head (002949e).
⚠️ Report is 124 commits behind head on main.

Files with missing lines Patch % Lines
src/pandas_openscm/index_manipulation.py 97.43% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #18      +/-   ##
==========================================
+ Coverage   96.96%   96.98%   +0.01%     
==========================================
  Files          27       27              
  Lines        1319     1358      +39     
  Branches      146      153       +7     
==========================================
+ Hits         1279     1317      +38     
  Misses         23       23              
- Partials       17       18       +1     

☔ View full report in Codecov by Sentry.
📢 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.

Comment on lines 747 to 750
# TODO mypy says this is unreachable
if not isinstance(ini, pd.MultiIndex):
msg = f"Expected MultiIndex, got {type(ini)}"
raise TypeError(msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes that's because the type hint for ini is pd.MultiIndex. You'll probably need to push this check one layer higher into the function that operates on DataFrames (where the index could be just index, rather than a multi-index)

Copy link
Contributor

Choose a reason for hiding this comment

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

Can be resolved I think?

msg = f"Expected MultiIndex, got {type(ini)}"
raise TypeError(msg)

df = ini.to_frame(index=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really know why, but calling to_frame is crazy expensive. Try and use https://github.com/openscm/gcages/blob/69452181c365c45f83d59edc3189727f651e5655/src/gcages/index_manipulation.py#L18 and

# Slow route: have to update the codes
as inspiration for a way to write this without needing to call to_frame. We can talk Monday afternoon if it's still not clear (which is very likely going to be the case as this multi-index stuff is super confusing).

Copy link
Contributor

Choose a reason for hiding this comment

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

Can be resolved I think?

Copy link
Contributor

@znichollscr znichollscr left a comment

Choose a reason for hiding this comment

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

Super nice. Some small comments and clean up then good to merge.

ValueError
If the length of the values is not equal to the length of the index
Examples
Copy link
Contributor

Choose a reason for hiding this comment

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

Super nice

Copy link
Contributor

Choose a reason for hiding this comment

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

This thread can be resolved I assume ?

dnl-bsch and others added 2 commits May 21, 2025 09:26
Co-authored-by: znichollscr <114576287+znichollscr@users.noreply.github.com>
@dnl-bsch
Copy link
Collaborator Author

Thank you for the comments.

I had to make some changes to make mypy happy (see last commit). It seems not ideal to convert a numpy array to a list only to satisfy the mypy checks. Or maybe that's the point of it? If you think that's a minor issue, we can also merge.

@znichollscr
Copy link
Contributor

It seems not ideal to convert a numpy array to a list only to satisfy the mypy checks

Good instinct, this isn't ideal. See #20 for the updates needed to fix this and some more explanation. If that all makes sense, then merge #20 and then also merge this MR. Nice job!

@znichollscr
Copy link
Contributor

Merge when you're happy @crdanielbusch

@dnl-bsch dnl-bsch merged commit 75dc2c8 into main May 23, 2025
23 checks passed
@znicholls znicholls deleted the MultiIndex-set-levels branch May 23, 2025 09:02
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.

4 participants