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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import (
Callable,
Hashable,
Iterable,
Sequence,
)
from typing import (
Expand All @@ -16,6 +17,7 @@ from pandas.core.indexes.base import Index
from typing_extensions import Self

from pandas._typing import (
AnyArrayLike,
Dtype,
DtypeArg,
HashableT,
Expand All @@ -27,31 +29,42 @@ from pandas._typing import (
class MultiIndex(Index[Any]):
def __new__(
cls,
levels=...,
codes=...,
sortorder=...,
levels: SequenceNotStr[SequenceNotStr[Hashable] | AnyArrayLike] = ...,
codes: SequenceNotStr[SequenceNotStr[int]] = ...,
sortorder: int | None = ...,
names: SequenceNotStr[Hashable] = ...,
dtype=...,
copy=...,
copy: bool = ...,
name: SequenceNotStr[Hashable] = ...,
verify_integrity: bool = ...,
_set_identity: bool = ...,
) -> Self: ...
@classmethod
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.

names: SequenceNotStr[Hashable] = ...,
) -> Self: ...
@classmethod
def from_tuples(
cls, tuples, sortorder=..., names: SequenceNotStr[Hashable] = ...
cls,
tuples: Iterable[tuple[Hashable, ...]],
sortorder: int | None = ...,
names: SequenceNotStr[Hashable] = ...,
) -> Self: ...
@classmethod
def from_product(
cls, iterables, sortorder=..., names: SequenceNotStr[Hashable] = ...
cls,
iterables: SequenceNotStr[Iterable[Hashable]],
sortorder: int | None = ...,
names: SequenceNotStr[Hashable] = ...,
) -> Self: ...
@classmethod
def from_frame(
cls, df, sortorder=..., names: SequenceNotStr[Hashable] = ...
cls,
df: pd.DataFrame,
sortorder: int | None = ...,
names: SequenceNotStr[Hashable] = ...,
) -> Self: ...
@property
def shape(self): ...
Expand Down
29 changes: 27 additions & 2 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_multiindex_get_level_values() -> None:
check(assert_type(i1, pd.Index), pd.Index)


def test_multiindex_constructor() -> None:
def test_multiindex_constructors() -> None:
check(
assert_type(
pd.MultiIndex([[1], [4]], codes=[[0], [0]], name=["a", "b"]), pd.MultiIndex
Expand All @@ -73,10 +73,35 @@ def test_multiindex_constructor() -> None:
)
check(
assert_type(
pd.MultiIndex([[1], [4]], codes=[[0], [0]], names=["a", "b"]), pd.MultiIndex
pd.MultiIndex(
[[1], [4]],
codes=[[0], [0]],
names=["a", "b"],
sortorder=0,
copy=True,
verify_integrity=True,
),
pd.MultiIndex,
),
pd.MultiIndex,
)
check(
assert_type(pd.MultiIndex.from_arrays([[1], [4]]), pd.MultiIndex), pd.MultiIndex
)
check(
assert_type(
pd.MultiIndex.from_arrays([np.arange(3), np.arange(3)]), pd.MultiIndex
),
pd.MultiIndex,
)
check(
assert_type(pd.MultiIndex.from_tuples([(1, 3), (2, 4)]), pd.MultiIndex),
pd.MultiIndex,
)
check(
assert_type(pd.MultiIndex.from_frame(pd.DataFrame({"a": [1]})), pd.MultiIndex),
pd.MultiIndex,
)


def test_index_tolist() -> None:
Expand Down