Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: change tuples param for MultiIndex.from_tuples from sequence to iterable #61357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
yangdanny97 opened this issue Apr 25, 2025 · 2 comments
Open
1 task done
Labels
Docs MultiIndex Needs Discussion Requires discussion from core team before further action

Comments

@yangdanny97
Copy link

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/reference/api/pandas.MultiIndex.from_tuples.html#pandas.MultiIndex.from_tuples

The docs currently say this for the tuples parameter:

list / sequence of tuple-likes

Documentation problem

Pandas-stubs annotates the parameter as sequence: https://github.com/pandas-dev/pandas-stubs/blob/main/pandas-stubs/core/indexes/multi.pyi#L49

Pandas source code annotates the parameter as iterable: https://github.com/pandas-dev/pandas/blob/main/pandas/core/indexes/multi.py#L521

Typing the parameter as sequence prevents this pattern from typechecking, even if it works at runtime:

MultiIndex.from_tuples(zip(['a'], ['b']))

This was raised in pandas-dev/pandas-stubs#1158

Suggested fix for documentation

Could we loosen the type annotation in the docs to say iterable? Then I can update pandas-stubs to match.

@rhshadrach
Copy link
Member

rhshadrach commented Apr 27, 2025

Could we loosen the type annotation in the docs to say iterable?

I think no - the code raises on inputs that aren't list-like (pandas considers generators as list-like). Currently the code does not raise on sets, but I believe it should. I think the docs should be something like list-like excluding sets and the behavior of pandas changed to match.

Also this is the only occurrence in pandas of tuple-likes and I'm not sure what that means. MultiIndex.from_tuples([[1, 2]]) does not raise, is a list "tuple-like"?

cc @Dr-Irv

@rhshadrach rhshadrach added MultiIndex Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 27, 2025
@Dr-Irv
Copy link
Contributor

Dr-Irv commented Apr 27, 2025

This relates to a comment I made here: #55425 (comment)

I agree with the idea of removing tuple-like and using list-like in the docs, which means we could include a generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs MultiIndex Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

3 participants