Skip to content
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

GH1089 Migrate frame/series tests to new framework #1093

Closed
Closed
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
Prev Previous commit
Next Next commit
GH1089 Partial typehinting
loicdiridollou committed Jan 17, 2025
commit fcd829f5053e5e0c035f23e9b0d27483584457c4
146 changes: 133 additions & 13 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
@@ -1609,6 +1609,16 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def __add__(self, other: S1 | Self) -> Self: ...
@overload
def __add__(
self: Series[int] | Series[float],
other: float | Series[float],
) -> Series[float]: ...
@overload
def __add__(
self: Series[float],
other: int | Series[int],
) -> Series[float]: ...
@overload
def __add__(
self,
other: complex,
@@ -1629,6 +1639,9 @@ class Series(IndexOpsMixin[S1], NDFrame):
) -> Series[int]: ...
# def __array__(self, dtype: Optional[_bool] = ...) -> _np_ndarray
def __eq__(self, other: object) -> Series[_bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
@overload
def __floordiv__(self: Series[float], other: Series[float] | Series[int] | float) -> Series[float]: ... # type: ignore[overload-overlap]
@overload
def __floordiv__(self, other: num | _ListLike | Series[S1]) -> Series[int]: ...
def __ge__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
@@ -1657,11 +1670,29 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def __mul__(self: Series[int], other: Series[float] | float) -> Series[float]: ...
@overload
def __mul__(self: Series[float], other: Series[int] | int) -> Series[float]: ...
@overload
def __mul__(self: Series[Any], other: Series[Any]) -> Series: ...
@overload
def __mul__(self, other: num | _ListLike | Series) -> Series: ...
@overload
def __mod__( # pyright: ignore[reportOverlappingOverload]
self: Series[int], other: int | Series[int]
) -> Series[int]: ...
@overload
def __mod__(self: Series[int], other: float | Series[float]) -> Series[float]: ...
@overload
def __mod__(
self: Series[float], other: float | Series[int] | Series[float]
) -> Series[float]: ...
@overload
def __mod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __ne__(self, other: object) -> Series[_bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
@overload
def __pow__(self: Series[int], other: Series[int] | int) -> Series[int]: ...
@overload
def __pow__(self, other: Series[float] | float) -> Series[float]: ...
@overload
def __pow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
@@ -1686,7 +1717,12 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: int | np_ndarray_anyint | Series[int]
) -> Series[int]: ...
def __rdivmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __rfloordiv__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
@overload
def __rfloordiv__(self: Series[float], other: float | Series[int] | Series[float]) -> Series[float]: ... # type: ignore[misc]
@overload
def __rfloordiv__(
self, other: num | _ListLike | Series[float]
) -> Series[float]: ...
def __rmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
@overload
def __rmul__(
@@ -1731,25 +1767,35 @@ class Series(IndexOpsMixin[S1], NDFrame):
self, other: Timestamp | datetime | TimestampSeries
) -> TimedeltaSeries: ...
@overload
def __sub__(
def __sub__(self, other: S1 | Self) -> Self: ...
@overload
def __sub__( # pyright: ignore[reportOverlappingOverload]
self: Series[int],
other: int | Series[int],
) -> Series[int]: ...
@overload
def __sub__(
self: Series[int],
other: float | Series[float],
) -> Series[float]: ...
@overload
def __sub__(
self: Series[float],
other: int | Series[int],
) -> Series[float]: ...
@overload
def __sub__(
self,
other: complex,
) -> Series[complex]: ...
@overload
def __sub__(self, other: S1 | Self) -> Self: ...
@overload
def __sub__(self, other: num | _ListLike | Series) -> Series: ...
@overload
def __truediv__(self: Series[int], other: Series[int] | int) -> Series[float]: ...
@overload
def __truediv__(
self, other: num | _ListLike | Series[S1] | Path
) -> Series | Self: ...
def __truediv__(self, other: Path) -> Series: ...
@overload
def __truediv__(self, other: num | _ListLike | Series[S1]) -> Series[float]: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
def __xor__( # pyright: ignore[reportOverlappingOverload]
@@ -1773,7 +1819,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def iloc(self) -> _iLocIndexerSeries[S1]: ...
@property
def loc(self) -> _LocIndexerSeries[S1]: ...
# Methods
# Met @overload
@overload
def add(
self: Series[int],
@@ -1783,6 +1829,22 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: int = ...,
) -> Series[int]: ...
@overload
def add( # pyright: ignore[reportOverlappingOverload]
self,
other: float | Series[float],
level: Level | None = ...,
fill_value: float | None = ...,
axis: int = ...,
) -> Series[float]: ...
@overload
def add(
self: Series[float],
other: int | Series[int],
level: Level | None = ...,
fill_value: float | None = ...,
axis: int = ...,
) -> Series[float]: ...
@overload
def add(
self,
other: complex,
@@ -1798,6 +1860,14 @@ class Series(IndexOpsMixin[S1], NDFrame):
fill_value: float | None = ...,
axis: int = ...,
) -> Series[S1]: ...
@overload
def add(
self,
other: S1 | Self,
level: Level | None = ...,
fill_value: float | None = ...,
axis: int = ...,
) -> Series: ...
def all(
self,
axis: AxisIndex = ...,
@@ -1861,13 +1931,30 @@ class Series(IndexOpsMixin[S1], NDFrame):
min_periods: int = ...,
method: CalculationMethod = ...,
) -> Expanding[Series]: ...
@overload
def floordiv( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
self: Series[int],
other: Series[int] | int,
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[int]: ...
@overload
def floordiv(
self: Series[int] | Series[float],
other: num | _ListLike | Series[S1],
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[float]: ...
@overload
def floordiv(
self,
other: num | _ListLike | Series[S1],
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[int]: ...
) -> Series[S1]: ...
def ge(
self,
other: Scalar | Series[S1],
@@ -1945,6 +2032,31 @@ class Series(IndexOpsMixin[S1], NDFrame):
numeric_only: _bool = ...,
**kwargs,
) -> S1: ...
@overload
def mod( # pyright: ignore[reportOverlappingOverload]
self: Series[int],
other: Series[int] | int,
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[int]: ...
@overload
def mod(
self: Series[int],
other: Series[float] | float,
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[float]: ...
@overload
def mod(
self: Series[float],
other: Series[int] | Series[float] | float,
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[float]: ...
@overload
def mod(
self,
other: num | _ListLike | Series[S1],
@@ -1953,17 +2065,25 @@ class Series(IndexOpsMixin[S1], NDFrame):
axis: AxisIndex | None = ...,
) -> Series[S1]: ...
@overload
def mul(
def mul( # pyright: ignore[reportOverlappingOverload]
self: Series[int],
other: Series[int] | int,
level: Level | None = ...,
fill_value: int | None = ...,
axis: AxisIndex | None = ...,
) -> Series[int]: ...
@overload
def mul(
self: Series[float],
other: Series[int] | Series[float] | int,
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
) -> Series[float]: ...
@overload
def mul(
self: Series[int],
other: Series[float],
other: Series[float] | float,
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
@@ -2002,7 +2122,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def pow(
self: Series[int],
other: float,
other: float | Series[float],
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
@@ -2161,7 +2281,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
@overload
def sub(
self: Series[int],
other: float,
other: float | Series[float],
level: Level | None = ...,
fill_value: float | None = ...,
axis: AxisIndex | None = ...,
51 changes: 3 additions & 48 deletions tests/test_series.py
Original file line number Diff line number Diff line change
@@ -646,45 +646,6 @@ def get_depth(url: str) -> int:
check(assert_type(s.apply(lambda x: pd.NA), pd.Series), pd.Series, NAType)


def test_types_element_wise_arithmetic() -> None:
s = pd.Series([0, 1, -10])
s2 = pd.Series([7, -5, 10])

check(assert_type(s + s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.add(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

check(assert_type(s - s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.sub(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

check(assert_type(s * s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.mul(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

# GH1089 should be the following
check(assert_type(s / s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.div(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s // s2, "pd.Series[int]"), pd.Series, np.integer)
check(
assert_type(s.floordiv(s2, fill_value=0), "pd.Series[int]"),
pd.Series,
np.integer,
)

check(assert_type(s % s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.mod(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

check(assert_type(s ** s2.abs(), "pd.Series[int]"), pd.Series, np.integer)
check(
assert_type(s.pow(s2.abs(), fill_value=0), "pd.Series[int]"),
pd.Series,
np.integer,
)

check(assert_type(divmod(s, s2), tuple["pd.Series[int]", "pd.Series[int]"]), tuple)


def test_types_scalar_arithmetic() -> None:
s = pd.Series([0, 1, -10])

@@ -714,7 +675,7 @@ def test_types_scalar_arithmetic() -> None:

check(assert_type(s**2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s**0, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s**0.213, "pd.Series[int]"), pd.Series, np.float64)
check(assert_type(s**0.213, "pd.Series[float]"), pd.Series, np.float64)
check(assert_type(s.pow(0.5), "pd.Series[float]"), pd.Series, np.float64)


@@ -1335,15 +1296,9 @@ def test_series_min_max_sub_axis() -> None:
sm = s1 * s2
sd = s1 / s2
check(assert_type(sa, pd.Series), pd.Series)
check(
assert_type(ss, pd.Series), # pyright: ignore[reportAssertTypeFailure]
pd.Series,
)
check(assert_type(ss, pd.Series), pd.Series)
check(assert_type(sm, pd.Series), pd.Series)
check(
assert_type(sd, pd.Series), # pyright: ignore[reportAssertTypeFailure]
pd.Series,
)
check(assert_type(sd, "pd.Series[float]"), pd.Series)


def test_series_index_isin() -> None:
191 changes: 191 additions & 0 deletions tests/test_series_arithmetic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
"""Test module for arithmetic operations on Series."""

from typing import assert_type

import numpy as np
import pandas as pd

from tests import check


def test_element_wise_int_int() -> None:
s = pd.Series([0, 1, -10])
s2 = pd.Series([7, -5, 10])

check(assert_type(s + s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.add(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

check(assert_type(s - s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.sub(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

check(assert_type(s * s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.mul(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

# GH1089 should be the following
check(assert_type(s / s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.div(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s // s2, "pd.Series[int]"), pd.Series, np.integer)
check(
assert_type(s.floordiv(s2, fill_value=0), "pd.Series[int]"),
pd.Series,
np.integer,
)

check(assert_type(s % s2, "pd.Series[int]"), pd.Series, np.integer)
check(assert_type(s.mod(s2, fill_value=0), "pd.Series[int]"), pd.Series, np.integer)

check(assert_type(s ** s2.abs(), "pd.Series[int]"), pd.Series, np.integer)
check(
assert_type(s.pow(s2.abs(), fill_value=0), "pd.Series[int]"),
pd.Series,
np.integer,
)

check(assert_type(divmod(s, s2), tuple["pd.Series[int]", "pd.Series[int]"]), tuple)


def test_element_wise_int_float() -> None:
s = pd.Series([0, 1, -10])
s2 = pd.Series([7.0, -5.5, 10.4])

check(assert_type(s + s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.add(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s - s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.sub(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s * s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.mul(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

# GH1089 should be the following
check(assert_type(s / s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.div(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(
assert_type(s.floordiv(s2, fill_value=0), "pd.Series[float]"),
pd.Series,
np.float64,
)

check(assert_type(s % s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.mod(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s ** s2.abs(), "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.pow(s2.abs(), fill_value=0), "pd.Series[float]"),
pd.Series,
np.float64,
)

check(assert_type(divmod(s, s2), tuple["pd.Series[int]", "pd.Series[int]"]), tuple)


def test_element_wise_float_int() -> None:
s = pd.Series([0.0, 1.4, -10.25])
s2 = pd.Series([7, -5, 10])

check(assert_type(s + s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.add(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s - s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.sub(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s * s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.mul(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

# GH1089 should be the following
check(assert_type(s / s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.div(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s // s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.floordiv(s2, fill_value=0), "pd.Series[float]"),
pd.Series,
np.float64,
)

check(assert_type(s % s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.mod(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s ** s2.abs(), "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.pow(s2.abs(), fill_value=0), "pd.Series[float]"),
pd.Series,
np.float64,
)

check(
assert_type(divmod(s, s2), tuple["pd.Series[float]", "pd.Series[float]"]), tuple
)


def test_element_wise_float_float() -> None:
s = pd.Series([0.439, 1.43829, -10.432])
s2 = pd.Series([7.4389, -5.543, 10.432])

check(assert_type(s + s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.add(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s - s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.sub(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s * s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.mul(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

# GH1089 should be the following
check(assert_type(s / s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.div(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s // s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.floordiv(s2, fill_value=0), "pd.Series[float]"),
pd.Series,
np.float64,
)

check(assert_type(s % s2, "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.mod(s2, fill_value=0), "pd.Series[float]"), pd.Series, np.float64
)

check(assert_type(s ** s2.abs(), "pd.Series[float]"), pd.Series, np.float64)
check(
assert_type(s.pow(s2.abs(), fill_value=0), "pd.Series[float]"),
pd.Series,
np.float64,
)

check(
assert_type(divmod(s, s2), tuple["pd.Series[float]", "pd.Series[float]"]), tuple
)