Skip to content

Commit

Permalink
gh:552 - Corrected Series.mean return type (#622)
Browse files Browse the repository at this point in the history
* corrected Series.mean() to return np.float64

* req changes

* added args to IndexIterScalar

* Update resample.pyi
  • Loading branch information
ramvikrams committed Apr 10, 2023
1 parent 7fbda1e commit c03e23c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ IndexIterScalar: TypeAlias = (
| float
| Timestamp
| Timedelta
| np.integer
| np.float_
)
Scalar: TypeAlias = IndexIterScalar | complex
ScalarT = TypeVar("ScalarT", bound=Scalar)
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
level: None = ...,
numeric_only: _bool = ...,
**kwargs,
) -> float: ...
) -> np.float64: ...
def median(
self,
axis: AxisIndex | None = ...,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2019,12 +2019,12 @@ def test_groupby_apply() -> None:
# GH 167
df = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})

def sum_mean(x: pd.DataFrame) -> float:
def sum_mean(x: pd.DataFrame) -> np.float64:
return x.sum().mean()

check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series)

lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean()
lfunc: Callable[[pd.DataFrame], np.float64] = lambda x: x.sum().mean()
check(
assert_type(df.groupby("col1").apply(lfunc), pd.Series),
pd.Series,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def g(val: DataFrame) -> Series:

check(assert_type(DF.resample("m").pipe(g), DataFrame), DataFrame)

def h(val: DataFrame) -> float:
def h(val: DataFrame) -> np.float64:
return val.mean().mean()

check(assert_type(DF.resample("m").pipe(h), Series), Series)
Expand Down Expand Up @@ -245,10 +245,10 @@ def test_aggregate_series() -> None:
DataFrame,
)

def f(val: Series) -> float:
def f(val: Series) -> np.float64:
return val.mean()

check(assert_type(S.resample("m").aggregate(f), _AggRetType), Series)
check(assert_type(S.resample("m").aggregate(f), _AggRetType), pd.Series)


def test_asfreq_series() -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ def test_types_rank() -> None:

def test_types_mean() -> None:
s = pd.Series([1, 2, 3, np.nan])
f1: float = s.mean()
check(assert_type(s.mean(), np.float64), np.float64)
s1: pd.Series = s.groupby(level=0).mean()
f2: float = s.mean(skipna=False)
f3: float = s.mean(numeric_only=False)
f2: np.float64 = s.mean(skipna=False)
f3: np.float64 = s.mean(numeric_only=False)


def test_types_median() -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_windowing.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_rolling_basic_math_series() -> None:
def test_rolling_apply_series() -> None:
check(assert_type(S.rolling(10).apply(np.mean), Series), Series)

def _mean(df: Series) -> float:
def _mean(df: Series) -> np.float64:
return df.mean()

check(assert_type(S.rolling(10).apply(_mean), Series), Series)
Expand All @@ -163,7 +163,7 @@ def test_rolling_aggregate_series() -> None:
check(assert_type(S.rolling(10).aggregate(np.mean), Series), Series)
check(assert_type(S.rolling(10).aggregate("mean"), Series), Series)

def _mean(s: Series) -> float:
def _mean(s: Series) -> np.float64:
return s.mean()

check(assert_type(S.rolling(10).aggregate(_mean), Series), Series)
Expand Down Expand Up @@ -256,7 +256,7 @@ def test_expanding_basic_math_series() -> None:
def test_expanding_apply_series() -> None:
check(assert_type(S.expanding(10).apply(np.mean), Series), Series)

def _mean(df: Series) -> float:
def _mean(df: Series) -> np.float64:
return df.mean()

check(assert_type(S.expanding(10).apply(_mean), Series), Series)
Expand Down

0 comments on commit c03e23c

Please sign in to comment.