diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 469d44ea0..fbb87c35c 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -4610,14 +4610,33 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): **kwargs: Any, ) -> np_1darray: ... def tolist(self) -> list[S1]: ... + @overload def var( - self, + self: Series[Never], axis: AxisIndex | None = 0, skipna: _bool | None = True, ddof: int = 1, numeric_only: _bool = False, **kwargs: Any, - ) -> Scalar: ... + ) -> float: ... + @overload + def var( + self: Series[complex], + axis: AxisIndex | None = 0, + skipna: _bool | None = True, + ddof: int = 1, + numeric_only: _bool = False, + **kwargs: Any, + ) -> np.float64: ... + @overload + def var( + self: SupportsGetItem[Scalar, SupportsTruedivInt[S2]], + axis: AxisIndex | None = 0, + skipna: _bool | None = True, + ddof: int = 1, + numeric_only: _bool = False, + **kwargs: Any, + ) -> S2: ... # Rename axis with `mapper`, `axis`, and `inplace=True` @overload def rename_axis( diff --git a/tests/series/test_agg.py b/tests/series/test_agg.py index 2ed73502d..5c1cedf35 100644 --- a/tests/series/test_agg.py +++ b/tests/series/test_agg.py @@ -14,6 +14,7 @@ def test_agg_any_float() -> None: check(assert_type(series.mean(), float), np.float64) check(assert_type(series.median(), float), np.float64) check(assert_type(series.std(), float), np.float64) + check(assert_type(series.var(), float), np.float64) def test_agg_bool() -> None: @@ -21,6 +22,7 @@ def test_agg_bool() -> None: check(assert_type(series.mean(), float), np.float64) check(assert_type(series.median(), float), np.float64) check(assert_type(series.std(), float), np.float64) + check(assert_type(series.var(), float), np.float64) def test_agg_int() -> None: @@ -28,6 +30,7 @@ def test_agg_int() -> None: check(assert_type(series.mean(), float), np.float64) check(assert_type(series.median(), float), np.float64) check(assert_type(series.std(), float), np.float64) + check(assert_type(series.var(), float), np.float64) def test_agg_float() -> None: @@ -35,6 +38,7 @@ def test_agg_float() -> None: check(assert_type(series.mean(), float), np.float64) check(assert_type(series.median(), float), np.float64) check(assert_type(series.std(), float), np.float64) + check(assert_type(series.var(), float), np.float64) def test_agg_complex() -> None: @@ -57,6 +61,11 @@ def test_agg_complex() -> None: ), ): check(assert_type(series.std(), np.float64), np.float64) + with pytest_warns_bounded( + np.exceptions.ComplexWarning, + r"Casting complex values to real discards the imaginary part", + ): + check(assert_type(series.var(), np.float64), np.float64) def test_agg_str() -> None: @@ -65,6 +74,7 @@ def test_agg_str() -> None: series.mean() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue] series.median() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue] series.std() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue] + series.var() # type: ignore[misc] # pyright: ignore[reportAttributeAccessIssue] def test_agg_ts() -> None: @@ -83,3 +93,4 @@ def test_agg_td() -> None: check(assert_type(series.mean(), pd.Timedelta), pd.Timedelta) check(assert_type(series.median(), pd.Timedelta), pd.Timedelta) check(assert_type(series.std(), pd.Timedelta), pd.Timedelta) + # Note: var() is not supported for Timedelta series at runtime diff --git a/tests/series/test_series.py b/tests/series/test_series.py index 96d3e411c..af6a40b25 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -780,9 +780,9 @@ def test_types_abs() -> None: def test_types_var() -> None: s = pd.Series([-10, 2, 3, 10]) - s.var() - s.var(axis=0, ddof=1) - s.var(skipna=True, numeric_only=False) + check(assert_type(s.var(), float), np.float64) + check(assert_type(s.var(axis=0, ddof=1), float), np.float64) + check(assert_type(s.var(skipna=True, numeric_only=False), float), np.float64) def test_types_std() -> None: