diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index da31cff03..dd15b03a2 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -1003,7 +1003,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): axis: Axis | None = ..., copy: bool = ..., inplace: Literal[True], - level: Level | None = ..., + level: Level | None = None, errors: IgnoreRaise = ..., ) -> None: ... @overload @@ -1016,7 +1016,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): axis: Axis | None = ..., copy: bool = ..., inplace: Literal[False] = False, - level: Level | None = ..., + level: Level | None = None, errors: IgnoreRaise = ..., ) -> Self: ... @overload @@ -1128,7 +1128,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def dropna( self, *, - axis: Axis = ..., + axis: Axis = 0, how: AnyAll = ..., thresh: int | None = ..., subset: ListLikeU | Scalar | None = ..., @@ -1139,7 +1139,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def dropna( self, *, - axis: Axis = ..., + axis: Axis = 0, how: AnyAll = ..., thresh: int | None = ..., subset: ListLikeU | Scalar | None = ..., @@ -1174,7 +1174,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, by: _str | Sequence[_str], *, - axis: Axis = ..., + axis: Axis = 0, ascending: _bool | Sequence[_bool] = ..., kind: SortKind = ..., na_position: NaPosition = ..., @@ -1187,7 +1187,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, by: _str | Sequence[_str], *, - axis: Axis = ..., + axis: Axis = 0, ascending: _bool | Sequence[_bool] = ..., kind: SortKind = ..., na_position: NaPosition = ..., @@ -1199,8 +1199,8 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def sort_index( self, *, - axis: Axis = ..., - level: Level | None = ..., + axis: Axis = 0, + level: Level | None = None, ascending: _bool | Sequence[_bool] = ..., kind: SortKind = ..., na_position: NaPosition = ..., @@ -1213,7 +1213,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def sort_index( self, *, - axis: Axis = ..., + axis: Axis = 0, level: Level | list[int] | list[_str] | None = ..., ascending: _bool | Sequence[_bool] = ..., kind: SortKind = ..., @@ -1512,33 +1512,20 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ) -> Self: ... def diff(self, periods: int = 1, axis: Axis = 0) -> Self: ... @overload - def agg( # pyright: ignore[reportOverlappingOverload] - self, - func: AggFuncTypeBase | AggFuncTypeDictSeries, - axis: Axis = ..., - **kwargs: Any, - ) -> Series: ... - @overload - def agg( - self, - func: list[AggFuncTypeBase] | AggFuncTypeDictFrame = ..., - axis: Axis = ..., - **kwargs: Any, - ) -> Self: ... - @overload def aggregate( # pyright: ignore[reportOverlappingOverload] self, func: AggFuncTypeBase | AggFuncTypeDictSeries, - axis: Axis = ..., + axis: Axis = 0, **kwargs: Any, ) -> Series: ... @overload def aggregate( self, - func: list[AggFuncTypeBase] | AggFuncTypeDictFrame, - axis: Axis = ..., + func: list[AggFuncTypeBase] | AggFuncTypeDictFrame | None = ..., + axis: Axis = 0, **kwargs: Any, ) -> Self: ... + agg = aggregate def transform( self, func: AggFuncTypeFrame, @@ -1588,7 +1575,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, # Use S2 (TypeVar without `default=Any`) instead of S1 due to https://github.com/python/mypy/issues/19182. f: Callable[..., S2 | NAType], - axis: Axis = ..., + axis: Axis = 0, raw: _bool = ..., args: Any = ..., *, @@ -1599,7 +1586,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def apply( self, f: Callable[..., ListLikeExceptSeriesAndStr | Series | Mapping[Any, Any]], - axis: Axis = ..., + axis: Axis = 0, raw: _bool = ..., args: Any = ..., *, @@ -1610,7 +1597,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def apply( self, f: Callable[..., ListLikeExceptSeriesAndStr | Mapping[Any, Any]], - axis: Axis = ..., + axis: Axis = 0, raw: _bool = ..., args: Any = ..., *, @@ -1623,7 +1610,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): f: Callable[ ..., ListLikeExceptSeriesAndStr | Series | Scalar | Mapping[Any, Any] ], - axis: Axis = ..., + axis: Axis = 0, raw: _bool = ..., args: Any = ..., *, @@ -1636,7 +1623,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def apply( self, f: Callable[..., Series], - axis: AxisIndex = ..., + axis: AxisIndex = 0, raw: _bool = ..., args: Any = ..., *, @@ -1769,7 +1756,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def quantile( self, q: float = ..., - axis: Axis = ..., + axis: Axis = 0, numeric_only: _bool = ..., interpolation: QuantileInterpolation = ..., method: CalculationMethod = ..., @@ -1778,7 +1765,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def quantile( self, q: Sequence[float] | np_ndarray_float, - axis: Axis = ..., + axis: Axis = 0, numeric_only: _bool = ..., interpolation: QuantileInterpolation = ..., method: CalculationMethod = ..., @@ -1961,16 +1948,17 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def sub( self, other: complex | ListLike | DataFrame, - axis: Axis | None = ..., - level: Level | None = ..., + axis: Axis = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... + subtract = sub def __rsub__(self, other: Any) -> Self: ... def rsub( self, other: complex | ListLike | DataFrame, - axis: Axis = ..., - level: Level | None = ..., + axis: Axis = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... def __mul__(self, other: Any) -> Self: ... @@ -1990,7 +1978,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): @overload def all( self, - axis: Axis = ..., + axis: Axis = 0, bool_only: _bool | None = ..., skipna: _bool = ..., **kwargs: Any, @@ -2008,7 +1996,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def any( self, *, - axis: Axis = ..., + axis: Axis = 0, bool_only: _bool | None = ..., skipna: _bool = ..., **kwargs: Any, @@ -2054,7 +2042,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def bfill( self, *, - axis: Axis | None = ..., + axis: Axis | None = None, inplace: Literal[True], limit: int | None = ..., limit_area: Literal["inside", "outside"] | None = ..., @@ -2063,7 +2051,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def bfill( self, *, - axis: Axis | None = ..., + axis: Axis | None = None, inplace: Literal[False] = False, limit: int | None = ..., limit_area: Literal["inside", "outside"] | None = ..., @@ -2178,14 +2166,20 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def truediv( self, other: complex | ListLike | DataFrame, - axis: Axis | None = ..., - level: Level | None = ..., + axis: Axis | None = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... div = truediv + divide = truediv @final def droplevel(self, level: Level | list[Level], axis: Axis = 0) -> Self: ... - def eq(self, other, axis: Axis = "columns", level: Level | None = ...) -> Self: ... + def eq( + self, + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, + ) -> Self: ... @final def equals(self, other: Series | DataFrame) -> _bool: ... @final @@ -2237,11 +2231,16 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def floordiv( self, other: float | ListLike | DataFrame, - axis: Axis | None = ..., - level: Level | None = ..., + axis: Axis | None = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... - def ge(self, other, axis: Axis = "columns", level: Level | None = ...) -> Self: ... + def ge( + self, + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, + ) -> Self: ... @overload def get(self, key: Hashable, default: None = None) -> Series | None: ... @overload @@ -2250,7 +2249,12 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def get(self, key: list[Hashable], default: None = None) -> Self | None: ... @overload def get(self, key: list[Hashable], default: _T) -> Self | _T: ... - def gt(self, other, axis: Axis = "columns", level: Level | None = ...) -> Self: ... + def gt( + self, + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, + ) -> Self: ... @final def head(self, n: int = 5) -> Self: ... @final @@ -2260,7 +2264,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, method: InterpolateOptions = ..., *, - axis: Axis = ..., + axis: Axis = 0, limit: int | None = ..., limit_direction: Literal["forward", "backward", "both"] = ..., limit_area: Literal["inside", "outside"] | None = ..., @@ -2272,7 +2276,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, method: InterpolateOptions = ..., *, - axis: Axis = ..., + axis: Axis = 0, limit: int | None = ..., limit_direction: Literal["forward", "backward", "both"] = ..., limit_area: Literal["inside", "outside"] | None = ..., @@ -2296,8 +2300,18 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ) -> Series: ... @final def last_valid_index(self) -> Scalar: ... - def le(self, other, axis: Axis = "columns", level: Level | None = ...) -> Self: ... - def lt(self, other, axis: Axis = "columns", level: Level | None = ...) -> Self: ... + def le( + self, + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, + ) -> Self: ... + def lt( + self, + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, + ) -> Self: ... @overload def mask( self, @@ -2312,7 +2326,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): *, inplace: Literal[True], axis: Axis | None = ..., - level: Level | None = ..., + level: Level | None = None, ) -> None: ... @overload def mask( @@ -2328,32 +2342,32 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): *, inplace: Literal[False] = False, axis: Axis | None = ..., - level: Level | None = ..., + level: Level | None = None, ) -> Self: ... def max( self, - axis: Axis | None = ..., + axis: Axis | None = 0, skipna: _bool | None = True, numeric_only: _bool = False, **kwargs: Any, ) -> Series: ... def mean( self, - axis: Axis | None = ..., + axis: Axis | None = 0, skipna: _bool | None = True, numeric_only: _bool = False, **kwargs: Any, ) -> Series: ... def median( self, - axis: Axis | None = ..., + axis: Axis | None = 0, skipna: _bool | None = True, numeric_only: _bool = False, **kwargs: Any, ) -> Series: ... def min( self, - axis: Axis | None = ..., + axis: Axis | None = 0, skipna: _bool | None = True, numeric_only: _bool = False, **kwargs: Any, @@ -2361,18 +2375,24 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def mod( self, other: float | ListLike | DataFrame, - axis: Axis | None = ..., - level: Level | None = ..., + axis: Axis | None = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... def mul( self, other: complex | ListLike | DataFrame, - axis: Axis | None = ..., - level: Level | None = ..., + axis: Axis | None = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... - def ne(self, other, axis: Axis = "columns", level: Level | None = ...) -> Self: ... + multiply = mul + def ne( + self, + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, + ) -> Self: ... @final def pct_change( self, @@ -2386,26 +2406,19 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def pow( self, other: complex | ListLike | DataFrame, - axis: Axis | None = ..., - level: Level | None = ..., + axis: Axis | None = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... def prod( self, - axis: Axis | None = ..., + axis: Axis | None = "columns", skipna: _bool | None = True, numeric_only: _bool = False, min_count: int = 0, **kwargs: Any, ) -> Series: ... - def product( - self, - axis: Axis | None = ..., - skipna: _bool = True, - numeric_only: _bool = False, - min_count: int = 0, - **kwargs: Any, - ) -> Series: ... + product = prod @final def rank( self, @@ -2416,13 +2429,6 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ascending: _bool = True, pct: _bool = False, ) -> Self: ... - def rdiv( - self, - other, - axis: Axis = ..., - level: Level | None = ..., - fill_value: float | None = None, - ) -> Self: ... @final def reindex_like( self, @@ -2474,23 +2480,23 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ) -> Self: ... def rfloordiv( self, - other, - axis: Axis = ..., - level: Level | None = ..., + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... def rmod( self, - other, - axis: Axis = ..., - level: Level | None = ..., + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... def rmul( self, - other, - axis: Axis = ..., - level: Level | None = ..., + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... @overload @@ -2523,18 +2529,19 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ) -> Rolling[Self]: ... def rpow( self, - other, - axis: Axis = ..., - level: Level | None = ..., + other: complex | ListLike | DataFrame, + axis: Axis = "columns", + level: Level | None = None, fill_value: float | None = None, ) -> Self: ... def rtruediv( self, - other, + other: complex | ListLike | DataFrame, axis: Axis = "columns", level: Level | None = None, fill_value: float | None = None, ) -> Self: ... + rdiv = rtruediv @final def sample( self, @@ -2567,7 +2574,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): def squeeze(self, axis: Axis | None = None) -> DataFrame | Series | Scalar: ... def std( self, - axis: Axis = ..., + axis: Axis | None = 0, skipna: _bool = True, ddof: int = 1, numeric_only: _bool = False, @@ -2575,7 +2582,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ) -> Series: ... def sum( self, - axis: Axis = 0, + axis: Axis | None = 0, skipna: _bool | None = True, numeric_only: _bool = False, min_count: int = 0, @@ -2735,7 +2742,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): ) -> Self: ... def var( self, - axis: Axis = ..., + axis: Axis | None = 0, skipna: _bool | None = True, ddof: int = 1, numeric_only: _bool = False, @@ -2755,7 +2762,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): *, inplace: Literal[True], axis: Axis | None = ..., - level: Level | None = ..., + level: Level | None = None, ) -> None: ... @overload def where( @@ -2771,19 +2778,11 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): *, inplace: Literal[False] = False, axis: Axis | None = ..., - level: Level | None = ..., + level: Level | None = None, ) -> Self: ... # Move from generic because Series is Generic and it returns Series[bool] there @final def __invert__(self) -> Self: ... - @final - def xs( - self, - key: Hashable, - axis: Axis = 0, - level: Level | None = ..., - drop_level: _bool = True, - ) -> Self | Series: ... # floordiv overload def __floordiv__( self, other: float | DataFrame | Series[int] | Series[float] | Sequence[float] diff --git a/pandas-stubs/core/generic.pyi b/pandas-stubs/core/generic.pyi index d5bee980b..56d80c24a 100644 --- a/pandas-stubs/core/generic.pyi +++ b/pandas-stubs/core/generic.pyi @@ -470,3 +470,10 @@ class NDFrame(indexing.IndexingMixin): ) -> DatetimeIndexResampler[Self]: ... # pyrefly: ignore[bad-specialization] @final def take(self, indices: TakeIndexer, axis: Axis = 0, **kwargs: Any) -> Self: ... + def xs( + self, + key: IndexLabel, + axis: Axis = 0, + level: IndexLabel | None = None, + drop_level: _bool = True, + ) -> Self | Series: ... diff --git a/pandas-stubs/core/interchange/from_dataframe.pyi b/pandas-stubs/core/interchange/from_dataframe.pyi index 8dc8e7e0f..1cc31c0db 100644 --- a/pandas-stubs/core/interchange/from_dataframe.pyi +++ b/pandas-stubs/core/interchange/from_dataframe.pyi @@ -1,3 +1,6 @@ +from typing import Any + import pandas as pd -def from_dataframe(df, allow_copy: bool = True) -> pd.DataFrame: ... +# TODO: resolve with https://github.com/pandas-dev/pandas/issues/63111 +def from_dataframe(df: Any, allow_copy: bool = True) -> pd.DataFrame: ... diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 654679fbd..4e030fb91 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -4627,10 +4627,10 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): copy: _bool | _NoDefaultDoNotUse = ..., ) -> Self: ... @final - def xs( + def xs( # pyright: ignore[reportIncompatibleMethodOverride] # pyrefly: ignore[bad-override] self, key: Hashable, - axis: AxisIndex = 0, + axis: AxisIndex = 0, # type: ignore[override] level: Level | None = ..., drop_level: _bool = True, ) -> Self: ... diff --git a/pyproject.toml b/pyproject.toml index 54771f994..59ffe5c73 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -204,10 +204,6 @@ ignore = [ "PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/ "ERA001", "PLR0402", "PLC0105" ] -"*frame.pyi" = [ - # TODO: remove when pandas-dev/pandas-stubs#1446 is resolved - "ANN001", "ANN201", "ANN204", "ANN206", -] "indexers.pyi" = [ # TODO: remove when indexers.pyi is fully typed "ANN001", "ANN201", "ANN204", "ANN206", diff --git a/tests/test_frame.py b/tests/test_frame.py index d007e687c..90c368cb1 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -4263,6 +4263,13 @@ def test_xs_frame_new() -> None: } df = pd.DataFrame(data=d) df = df.set_index(["class", "animal", "locomotion"]) + check( + assert_type( + df.xs(("mammal", "cat"), level=["class", "animal"]), + pd.Series | pd.DataFrame, + ), + pd.DataFrame, + ) s1 = df.xs("mammal", axis=0) s2 = df.xs("num_wings", axis=1) check(assert_type(s1, pd.Series | pd.DataFrame), pd.DataFrame)