Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ SeriesDType: TypeAlias = (
| datetime.datetime # includes pd.Timestamp
| datetime.timedelta # includes pd.Timedelta
)

Copy link
Contributor

Choose a reason for hiding this comment

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

This extra empty line can also be removed

Copy link
Contributor

Choose a reason for hiding this comment

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

Would you?

S1 = TypeVar("S1", bound=SeriesDType, default=Any)
# Like S1, but without `default=Any`.
S2 = TypeVar("S2", bound=SeriesDType)
Expand Down
7 changes: 6 additions & 1 deletion pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class MultiIndex(Index):
def append(self, other): ...
def repeat(self, repeats, axis=...): ...
def drop(self, codes, level: Level | None = None, errors: str = "raise") -> Self: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def swaplevel(self, i: int = -2, j: int = -1): ...
def swaplevel(self, i: int = -2, j: int = -1) -> Self: ...
def reorder_levels(self, order): ...
def sortlevel(
self,
Expand Down Expand Up @@ -163,3 +163,8 @@ class MultiIndex(Index):
def insert(self, loc, item): ...
def delete(self, loc): ...
def isin(self, values, level=...) -> np_1darray[np.bool]: ...
def union(
self,
other: Self | Index | list[HashableT],
sort: bool | None = ...,
Comment on lines +167 to +169
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose Index should be removed. It is not tested, either.

Suggested change
self,
other: Self | Index | list[HashableT],
sort: bool | None = ...,
self, other: list[HashableT] | Self, sort: bool | None = ...

) -> Self: ...
Empty file removed pandas-stubs/py.typed
Empty file.
19 changes: 19 additions & 0 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,3 +1601,22 @@ def test_to_series() -> None:
np.complexfloating,
)
check(assert_type(Index(["1"]).to_series(), "pd.Series[str]"), pd.Series, str)


def test_multiindex_swaplevel_rettype() -> None:
"""Test that union returns MultiIndex on MultiIndex input and swaplevel returns Self"""
mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=["let", "num"])
mi2 = pd.MultiIndex.from_product([["a", "b"], [3, 4]], names=["let", "num"])

check(
assert_type(mi.swaplevel(0, 1), "pd.MultiIndex"),
pd.MultiIndex,
)
check(
assert_type(mi.union(mi2), "pd.MultiIndex"),
pd.MultiIndex,
)
check(
assert_type(mi.union([("c", 3), ("d", 4)]), "pd.MultiIndex"),
pd.MultiIndex,
)
Comment on lines +1606 to +1622
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make two tests, since swaplevel and union are two independent functionalities:

Suggested change
def test_multiindex_swaplevel_rettype() -> None:
"""Test that union returns MultiIndex on MultiIndex input and swaplevel returns Self"""
mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=["let", "num"])
mi2 = pd.MultiIndex.from_product([["a", "b"], [3, 4]], names=["let", "num"])
check(
assert_type(mi.swaplevel(0, 1), "pd.MultiIndex"),
pd.MultiIndex,
)
check(
assert_type(mi.union(mi2), "pd.MultiIndex"),
pd.MultiIndex,
)
check(
assert_type(mi.union([("c", 3), ("d", 4)]), "pd.MultiIndex"),
pd.MultiIndex,
)
def test_multiindex_union() -> None:
"""Test that MultiIndex.union returns MultiIndex"""
mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=["let", "num"])
mi2 = pd.MultiIndex.from_product([["a", "b"], [3, 4]], names=["let", "num"])
check(
assert_type(mi.union(mi2), "pd.MultiIndex"), pd.MultiIndex
)
check(
assert_type(mi.union([("c", 3), ("d", 4)]), "pd.MultiIndex"), pd.MultiIndex
)
def test_multiindex_swaplevel() -> None:
"""Test that MultiIndex.swaplevel returns MultiIndex"""
mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=["let", "num"])
check(
assert_type(mi.swaplevel(0, 1), "pd.MultiIndex"), pd.MultiIndex
)