Repository navigation
How should pandas-stubs balance typing precision, overload complexity, and runtime behavior for MultiIndex indexing? #71944
Unanswered
Prem-gosavi45
asked this question in
Q&A
Replies: 1 comment 1 reply
|
A runtime edge case to include when evaluating an overload for a fully specified key: duplicate column labels can change the result. On pandas 3.0.3 I checked: import pandas as pd
cols = pd.MultiIndex.from_tuples([("A", "a"), ("A", "a")])
df = pd.DataFrame([[1, 2]], columns=cols)
print(type(df.loc[:, ("A", "a")]).__name__) # DataFrameSo even a complete tuple alone does not guarantee a Series unless uniqueness is also assumed. The duplicate-label guide discusses how duplicates affect dimensionality. A runtime/type-checker test matrix covering unique and duplicate full keys, partial keys, and slices could make the precision trade-off explicit. I can’t speak for the maintainers’ policy or planned direction. |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi pandas maintainers and contributors!
While working on pandas-stubs issue #1018 (MultiIndex column selection with
.loc), I encountered an interesting challenge around overload resolution.A seemingly simple indexing expression can have different return types depending on whether the MultiIndex key is fully specified, partially specified, or contains slices.
For example:
This raises a broader design question:
How should we model pandas indexing APIs in static typing when the return type depends on the structure and completeness of an index key, especially when Python's type system cannot easily express exclusions between overlapping tuple types?
I'm particularly interested in the following:
.loc,.iloc, MultiIndex, and GroupBy?I'd love to understand the maintainers' perspective on the long-term direction of pandas-stubs typing.
Thanks for maintaining this amazing project!
All reactions