Skip to content

Conversation

@znicholls
Copy link
Contributor

@crdanielbusch this fixes up the typing issues for the codes. In short, we should be able to type codes as list[npt.NDArray[np.integer[Any]]] everywhere. I'll add some specific comments to explain more details. Once you're happy with this and have understood it, let's merge this and then we can merge #18


levels: list[pd.Index[Any]] = list(ini.levels)
codes: list[list[int] | npt.NDArray[np.integer[Any]]] = list(ini.codes)
codes: list[npt.NDArray[np.integer[Any]]] = list(ini.codes)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure why we had list[int] too, but removing it is correct I think (it gets cast to an array by pandas, so no need to support the list path too as that is slow because you're casting back and forth for now reason)

def create_level_from_collection(
level: str, value: Collection[Any]
) -> tuple[pandas.Index[Any], list[int]]:
) -> tuple[pandas.Index[Any], npt.NDArray[np.integer[Any]]]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The second return value is the codes, so should have the same type as we expect codes to have

if not new_level.has_duplicates:
# Fast route, can just return new level and codes from level we mapped from
return new_level, list(np.arange(len(value)))
return new_level, np.arange(len(value))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Once the type hint was correct, we could drop the cast to a list

else:
new_level = pd.Index([value], name=level)
new_codes = list(np.zeros(ini.shape[0]))
new_codes = np.zeros(ini.shape[0], dtype=int)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just dropping the cast to a list here as it isn't needed (the specification of the dtype wasn't something I expected, but it makes sense as the default is float, which isn't 100% correct)

@codecov
Copy link

codecov bot commented May 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.98%. Comparing base (0045e01) to head (9abd43b).

Additional details and impacted files
@@                  Coverage Diff                   @@
##           MultiIndex-set-levels      #20   +/-   ##
======================================================
  Coverage                  96.98%   96.98%           
======================================================
  Files                         27       27           
  Lines                       1358     1358           
  Branches                     153      153           
======================================================
  Hits                        1317     1317           
  Misses                        23       23           
  Partials                      18       18           

☔ 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.

@znichollscr znichollscr mentioned this pull request May 23, 2025
3 tasks
@dnl-bsch dnl-bsch merged commit 002949e into MultiIndex-set-levels May 23, 2025
41 of 42 checks passed
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.

3 participants