Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8d51763
Fix: proper return types for MultiIndex.swaplevel & MultiIndex.union
realzachbrownjohn Oct 21, 2025
4552867
Index of and List of tuples in MultiIndex overriding
realzachbrownjohn Oct 22, 2025
d3f5757
MultiIndex union tests
realzachbrownjohn Oct 22, 2025
d401e0c
MultiIndex-Index union overload simplified
realzachbrownjohn Oct 22, 2025
7739015
MultiIndex test amended and swaplevel test added
realzachbrownjohn Oct 22, 2025
2a77d79
Simplified MultiIndex-Index union overload
realzachbrownjohn Oct 22, 2025
2f5c338
Reverted S1 changes
realzachbrownjohn Oct 22, 2025
5e82ee5
amended MultiIndex union test and added swaplevel test
realzachbrownjohn Oct 22, 2025
a606e96
Removed runtime-failing test
realzachbrownjohn Oct 22, 2025
23b603f
Merge branch 'pandas-dev:main' into bugfix
realzachbrownjohn Oct 22, 2025
46e8050
Pre-commit refactoring
realzachbrownjohn Oct 22, 2025
092eec2
Removed a MultiIndex-Index test
realzachbrownjohn Oct 22, 2025
6b676bf
Removed MultiIndex union overloading
realzachbrownjohn Oct 22, 2025
5d8153f
Separated swaplevel and union tests
realzachbrownjohn Oct 23, 2025
3397d49
test for MultiIndex-Index union
realzachbrownjohn Oct 23, 2025
39877b2
MultiIndex.union overload replaced with change of base return type
realzachbrownjohn Oct 23, 2025
9c66299
removed MultiIndex.union overload
realzachbrownjohn Oct 23, 2025
0ffb099
Removed extra newline
realzachbrownjohn Oct 24, 2025
9024184
Added py.typed
realzachbrownjohn Oct 24, 2025
6e94402
existing tests fixed
realzachbrownjohn Oct 24, 2025
9fce599
tests fixed for runtime errors
realzachbrownjohn Oct 24, 2025
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
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
__bool__ = ...
def union(
self, other: list[HashableT] | Self, sort: bool | None = None
) -> Index: ...
) -> Self: ...
def intersection(
self, other: list[S1] | Self, sort: bool | None = False
) -> Self: ...
Expand Down
2 changes: 1 addition & 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
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/range.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class RangeIndex(_IndexSubclassBase[int, np.int64]):
def all(self, *args: Any, **kwargs: Any) -> bool: ...
def any(self, *args: Any, **kwargs: Any) -> bool: ...
@final
def union(
def union( # type: ignore[override]
self, other: list[HashableT] | Index, sort: bool | None = None
) -> Index | Index[int] | RangeIndex: ...
@overload # type: ignore[override]
Expand Down
25 changes: 23 additions & 2 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,18 @@ def test_range_index_union() -> None:
def test_index_union_sort() -> None:
"""Test sort argument in pd.Index.union GH1264."""
check(
assert_type(pd.Index(["e", "f"]).union(["a", "b", "c"], sort=True), pd.Index),
assert_type(
pd.Index(["e", "f"]).union(["a", "b", "c"], sort=True), "pd.Index[str]"
),
pd.Index,
str,
)
check(
assert_type(pd.Index(["e", "f"]).union(["a", "b", "c"], sort=False), pd.Index),
assert_type(
pd.Index(["e", "f"]).union(["a", "b", "c"], sort=False), "pd.Index[str]"
),
pd.Index,
str,
)


Expand Down Expand Up @@ -1601,3 +1607,18 @@ def test_to_series() -> None:
np.complexfloating,
)
check(assert_type(Index(["1"]).to_series(), "pd.Series[str]"), pd.Series, str)


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)