Skip to content

add operators for Index #504

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 4 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions pandas-stubs/core/arraylike.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ from pandas import DataFrame

class OpsMixinProtocol(Protocol): ...

# We would like to use ->Self here so this could be used by Index, but mypy 0.991
# doesn't support Self. So we will have to add these operators directly to Index

class OpsMixin:
def __eq__(self: OpsMixinProtocol, other: object) -> DataFrame: ... # type: ignore[override]
def __ne__(self: OpsMixinProtocol, other: object) -> DataFrame: ... # type: ignore[override]
Expand Down
22 changes: 14 additions & 8 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,20 @@ class Index(IndexOpsMixin, PandasObject):
def duplicated(
self, keep: Literal["first", "last", False] = ...
) -> np_ndarray_bool: ...
def __add__(self, other) -> Index: ...
def __radd__(self, other) -> Index: ...
def __iadd__(self, other) -> Index: ...
def __sub__(self, other) -> Index: ...
def __rsub__(self, other) -> Index: ...
def __and__(self, other) -> Index: ...
def __or__(self, other) -> Index: ...
def __xor__(self, other) -> Index: ...
def __add__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __radd__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __sub__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __rsub__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __mul__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __rmul__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __truediv__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __rtruediv__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __floordiv__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __rfloordiv__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __pow__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __rpow__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __mod__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __rmod__(self: IndexT, other: Index | Scalar) -> IndexT: ...
def __neg__(self: IndexT) -> IndexT: ...
def __nonzero__(self) -> None: ...
__bool__ = ...
Expand Down
20 changes: 10 additions & 10 deletions pandas-stubs/core/indexes/numeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ class NumericIndex(Index):
@property
def is_all_dates(self) -> bool: ...
def insert(self, loc, item): ...
def __add__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __radd__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __sub__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __rsub__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __mul__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __rmul__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __truediv__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __rtruediv__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __floordiv__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __rfloordiv__(self, other: NumericIndex | complex) -> NumericIndex: ...
def __add__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __radd__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __sub__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __rsub__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __mul__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __rmul__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __truediv__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __rtruediv__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __floordiv__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]
def __rfloordiv__(self, other: NumericIndex | complex) -> NumericIndex: ... # type: ignore[override]

class IntegerIndex(NumericIndex):
def __contains__(self, key) -> bool: ...
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ poethepoet = ">=0.16.5"
loguru = ">=0.6.0"
pandas = "1.5.2"
numpy = "<=1.23.5"
typing-extensions = ">=4.2.0"
typing-extensions = ">=4.4.0"
matplotlib = ">=3.5.1"
pre-commit = ">=2.19.0"
black = ">=22.12.0"
Expand Down
28 changes: 28 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,31 @@ def test_sorted_and_list() -> None:
),
list,
)


def test_index_operators() -> None:
# GH 405
i1 = pd.Index([1, 2, 3])
i2 = pd.Index([4, 5, 6])

check(assert_type(i1 + i2, pd.Index), pd.Index)
check(assert_type(i1 + 10, pd.Index), pd.Index)
check(assert_type(10 + i1, pd.Index), pd.Index)
check(assert_type(i1 - i2, pd.Index), pd.Index)
check(assert_type(i1 - 10, pd.Index), pd.Index)
check(assert_type(10 - i1, pd.Index), pd.Index)
check(assert_type(i1 * i2, pd.Index), pd.Index)
check(assert_type(i1 * 10, pd.Index), pd.Index)
check(assert_type(10 * i1, pd.Index), pd.Index)
check(assert_type(i1 / i2, pd.Index), pd.Index)
check(assert_type(i1 / 10, pd.Index), pd.Index)
check(assert_type(10 / i1, pd.Index), pd.Index)
check(assert_type(i1 // i2, pd.Index), pd.Index)
check(assert_type(i1 // 10, pd.Index), pd.Index)
check(assert_type(10 // i1, pd.Index), pd.Index)
check(assert_type(i1**i2, pd.Index), pd.Index)
check(assert_type(i1**2, pd.Index), pd.Index)
check(assert_type(2**i1, pd.Index), pd.Index)
check(assert_type(i1 % i2, pd.Index), pd.Index)
check(assert_type(i1 % 10, pd.Index), pd.Index)
check(assert_type(10 % i1, pd.Index), pd.Index)