Skip to content

Make relational operators work with all scalars #176

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 2 commits into from
Aug 1, 2022
Merged
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
16 changes: 4 additions & 12 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1143,12 +1143,8 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
def __div__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __eq__(self, other: object) -> Series[_bool]: ... # type: ignore[override]
def __floordiv__(self, other: num | _ListLike | Series[S1]) -> Series[int]: ...
def __ge__(
self, other: num | _ListLike | Series[S1] | Timestamp
) -> Series[_bool]: ...
def __gt__(
self, other: num | _ListLike | Series[S1] | Timestamp
) -> Series[_bool]: ...
def __ge__(self, other: S1 | _ListLike | Series[S1]) -> Series[_bool]: ...
def __gt__(self, other: S1 | _ListLike | Series[S1]) -> Series[_bool]: ...
# def __iadd__(self, other: S1) -> Series[S1]: ...
# def __iand__(self, other: S1) -> Series[_bool]: ...
# def __idiv__(self, other: S1) -> Series[S1]: ...
Expand All @@ -1161,12 +1157,8 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
# def __itruediv__(self, other: S1) -> Series[S1]: ...
# def __itruediv__(self, other) -> None: ...
# def __ixor__(self, other: S1) -> Series[_bool]: ...
def __le__(
self, other: num | _ListLike | Series[S1] | Timestamp
) -> Series[_bool]: ...
def __lt__(
self, other: num | _ListLike | Series[S1] | Timestamp
) -> Series[_bool]: ...
def __le__(self, other: S1 | _ListLike | Series[S1]) -> Series[_bool]: ...
def __lt__(self, other: S1 | _ListLike | Series[S1]) -> Series[_bool]: ...
@overload
def __mul__(self, other: Timedelta | TimedeltaSeries) -> TimedeltaSeries: ...
@overload
Expand Down
70 changes: 70 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import datetime
from pathlib import Path
import re
import tempfile
Expand Down Expand Up @@ -948,3 +949,72 @@ def test_series_overloads_extract():
assert_type(s.str.extract(r"[ab](\d)", re.IGNORECASE, False), pd.Series),
pd.Series,
)


def test_relops() -> None:
# GH 175
s: str = "abc"
check(assert_type(pd.Series([s]) > s, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([s]) < s, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([s]) <= s, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([s]) >= s, "pd.Series[bool]"), pd.Series, bool)

b: bytes = b"def"
check(assert_type(pd.Series([b]) > b, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([b]) < b, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([b]) <= b, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([b]) >= b, "pd.Series[bool]"), pd.Series, bool)

dtd = datetime.date(2022, 7, 31)
check(assert_type(pd.Series([dtd]) > dtd, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dtd]) < dtd, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dtd]) <= dtd, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dtd]) >= dtd, "pd.Series[bool]"), pd.Series, bool)

dtdt = datetime.datetime(2022, 7, 31, 8, 32, 21)
check(assert_type(pd.Series([dtdt]) > dtdt, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dtdt]) < dtdt, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dtdt]) <= dtdt, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dtdt]) >= dtdt, "pd.Series[bool]"), pd.Series, bool)

dttd = datetime.timedelta(seconds=10)
check(assert_type(pd.Series([dttd]) > dttd, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dttd]) < dttd, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dttd]) <= dttd, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([dttd]) >= dttd, "pd.Series[bool]"), pd.Series, bool)

bo: bool = True
check(assert_type(pd.Series([bo]) > bo, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([bo]) < bo, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([bo]) <= bo, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([bo]) >= bo, "pd.Series[bool]"), pd.Series, bool)

ai: int = 10
check(assert_type(pd.Series([ai]) > ai, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ai]) < ai, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ai]) <= ai, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ai]) >= ai, "pd.Series[bool]"), pd.Series, bool)

af: float = 3.14
check(assert_type(pd.Series([af]) > af, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([af]) < af, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([af]) <= af, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([af]) >= af, "pd.Series[bool]"), pd.Series, bool)

ac: complex = 1 + 2j
check(assert_type(pd.Series([ac]) > ac, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ac]) < ac, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ac]) <= ac, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ac]) >= ac, "pd.Series[bool]"), pd.Series, bool)

ts = pd.Timestamp("2022-07-31 08:35:12")
check(assert_type(pd.Series([ts]) > ts, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ts]) < ts, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ts]) <= ts, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([ts]) >= ts, "pd.Series[bool]"), pd.Series, bool)

td = pd.Timedelta(seconds=10)
check(assert_type(pd.Series([td]) > td, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([td]) < td, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([td]) <= td, "pd.Series[bool]"), pd.Series, bool)
check(assert_type(pd.Series([td]) >= td, "pd.Series[bool]"), pd.Series, bool)