Skip to content

type multiindex constructors #1126

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

Merged
merged 5 commits into from
Feb 19, 2025

Conversation

MarcoGorelli
Copy link
Member

  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

Comment on lines 42 to 45
def from_arrays(
cls, arrays, sortorder=..., names: SequenceNotStr[Hashable] = ...
cls,
arrays: SequenceNotStr[SequenceNotStr[Hashable] | AnyArrayLike],
sortorder: int | None = ...,
Copy link
Member Author

Choose a reason for hiding this comment

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

This one's a bit odd

The name is from_arrays, and the pandas docstring says that this should be a sequence of array-like

But, the pandas docs (and tests) have several examples of passing lists, which are not ArrayLike

lists are not included in AnyArrayLike, so I've used SequenceNotStr[Hashable] | AnyArrayLike

Copy link
Member Author

Choose a reason for hiding this comment

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

🤔 maybe ListLike would be more suitable

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it should be Sequence[Axes] because this works:

>>> pd.MultiIndex.from_arrays([range(2), [3,4]], names=["a", "b"])
MultiIndex([(0, 3),
            (1, 4)],
           names=['a', 'b'])
>>> pd.MultiIndex.from_arrays([{1:"x", 2:"y"}, [3,4]], names=["a", "b"])
MultiIndex([(1, 3),
            (2, 4)],
           names=['a', 'b'])

Copy link
Collaborator

Choose a reason for hiding this comment

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

🤔 maybe ListLike would be more suitable

Problem is that ListLike allows a single string, so we can't use it. I think Axes is the right choice.

@MarcoGorelli MarcoGorelli marked this pull request as ready for review February 19, 2025 12:03
Comment on lines 42 to 45
def from_arrays(
cls, arrays, sortorder=..., names: SequenceNotStr[Hashable] = ...
cls,
arrays: SequenceNotStr[SequenceNotStr[Hashable] | AnyArrayLike],
sortorder: int | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it should be Sequence[Axes] because this works:

>>> pd.MultiIndex.from_arrays([range(2), [3,4]], names=["a", "b"])
MultiIndex([(0, 3),
            (1, 4)],
           names=['a', 'b'])
>>> pd.MultiIndex.from_arrays([{1:"x", 2:"y"}, [3,4]], names=["a", "b"])
MultiIndex([(1, 3),
            (2, 4)],
           names=['a', 'b'])

@MarcoGorelli MarcoGorelli marked this pull request as draft February 19, 2025 16:11
@MarcoGorelli MarcoGorelli marked this pull request as ready for review February 19, 2025 16:15
@MarcoGorelli
Copy link
Member Author

thanks for your review and explanations!

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

thanks @MarcoGorelli

@Dr-Irv Dr-Irv merged commit aa9844d into pandas-dev:main Feb 19, 2025
10 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.

2 participants