diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 600ebc767..61635325e 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -695,42 +695,6 @@ class DataFrame(NDFrame, OpsMixin): tolerance: float | None = ..., ) -> DataFrame: ... @overload - def drop( - self, - labels: Hashable | Sequence[Hashable] | Index = ..., - *, - axis: Axis = ..., - index: Hashable | Sequence[Hashable] | Index = ..., - columns: Hashable | Sequence[Hashable] | Index = ..., - level: Level | None = ..., - inplace: Literal[True], - errors: IgnoreRaise = ..., - ) -> None: ... - @overload - def drop( - self, - labels: Hashable | Sequence[Hashable] | Index = ..., - *, - axis: Axis = ..., - index: Hashable | Sequence[Hashable] | Index = ..., - columns: Hashable | Sequence[Hashable] | Index = ..., - level: Level | None = ..., - inplace: Literal[False] = ..., - errors: IgnoreRaise = ..., - ) -> DataFrame: ... - @overload - def drop( - self, - labels: Hashable | Sequence[Hashable] | Index = ..., - *, - axis: Axis = ..., - index: Hashable | Sequence[Hashable] | Index = ..., - columns: Hashable | Sequence[Hashable] | Index = ..., - level: Level | None = ..., - inplace: bool = ..., - errors: IgnoreRaise = ..., - ) -> DataFrame | None: ... - @overload def rename( self, mapper: Renamer | None = ..., diff --git a/pandas-stubs/core/generic.pyi b/pandas-stubs/core/generic.pyi index 7af030803..7b1c6345b 100644 --- a/pandas-stubs/core/generic.pyi +++ b/pandas-stubs/core/generic.pyi @@ -305,11 +305,11 @@ class NDFrame(indexing.IndexingMixin): @overload def drop( self, - labels: Hashable | Sequence[Hashable] = ..., + labels: None = ..., *, axis: Axis = ..., - index: Hashable | Sequence[Hashable] = ..., - columns: Hashable | Sequence[Hashable] = ..., + index: Hashable | Sequence[Hashable] | Index[Any] = ..., + columns: Hashable | Sequence[Hashable] | Index[Any], level: Level | None = ..., inplace: Literal[True], errors: IgnoreRaise = ..., @@ -317,11 +317,47 @@ class NDFrame(indexing.IndexingMixin): @overload def drop( self, - labels: Hashable | Sequence[Hashable] = ..., + labels: None = ..., *, axis: Axis = ..., - index: Hashable | Sequence[Hashable] = ..., - columns: Hashable | Sequence[Hashable] = ..., + index: Hashable | Sequence[Hashable] | Index[Any], + columns: Hashable | Sequence[Hashable] | Index[Any] = ..., + level: Level | None = ..., + inplace: Literal[True], + errors: IgnoreRaise = ..., + ) -> None: ... + @overload + def drop( + self, + labels: Hashable | Sequence[Hashable] | Index[Any], + *, + axis: Axis = ..., + index: None = ..., + columns: None = ..., + level: Level | None = ..., + inplace: Literal[True], + errors: IgnoreRaise = ..., + ) -> None: ... + @overload + def drop( + self, + labels: None = ..., + *, + axis: Axis = ..., + index: Hashable | Sequence[Hashable] | Index[Any] = ..., + columns: Hashable | Sequence[Hashable] | Index[Any], + level: Level | None = ..., + inplace: Literal[False] = ..., + errors: IgnoreRaise = ..., + ) -> Self: ... + @overload + def drop( + self, + labels: None = ..., + *, + axis: Axis = ..., + index: Hashable | Sequence[Hashable] | Index[Any], + columns: Hashable | Sequence[Hashable] | Index[Any] = ..., level: Level | None = ..., inplace: Literal[False] = ..., errors: IgnoreRaise = ..., @@ -329,11 +365,47 @@ class NDFrame(indexing.IndexingMixin): @overload def drop( self, - labels: Hashable | Sequence[Hashable] = ..., + labels: Hashable | Sequence[Hashable] | Index[Any], + *, + axis: Axis = ..., + index: None = ..., + columns: None = ..., + level: Level | None = ..., + inplace: Literal[False] = ..., + errors: IgnoreRaise = ..., + ) -> Self: ... + @overload + def drop( + self, + labels: None = ..., + *, + axis: Axis = ..., + index: Hashable | Sequence[Hashable] | Index[Any] = ..., + columns: Hashable | Sequence[Hashable] | Index[Any], + level: Level | None = ..., + inplace: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Self | None: ... + @overload + def drop( + self, + labels: None = ..., + *, + axis: Axis = ..., + index: Hashable | Sequence[Hashable] | Index[Any], + columns: Hashable | Sequence[Hashable] | Index[Any] = ..., + level: Level | None = ..., + inplace: _bool = ..., + errors: IgnoreRaise = ..., + ) -> Self | None: ... + @overload + def drop( + self, + labels: Hashable | Sequence[Hashable] | Index[Any], *, axis: Axis = ..., - index: Hashable | Sequence[Hashable] = ..., - columns: Hashable | Sequence[Hashable] = ..., + index: None = ..., + columns: None = ..., level: Level | None = ..., inplace: _bool = ..., errors: IgnoreRaise = ..., diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 2cb50fc38..41ee06d5c 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -116,8 +116,6 @@ from pandas._typing import ( FloatDtypeArg, GroupByObjectNonScalar, HashableT1, - HashableT2, - HashableT3, IgnoreRaise, IndexingInt, IndexLabel, @@ -1052,42 +1050,6 @@ class Series(IndexOpsMixin[S1], NDFrame): tolerance: float | None = ..., ) -> Self: ... @overload - def drop( - self, - labels: Hashable | list[HashableT1] | Index = ..., - *, - axis: Axis = ..., - index: Hashable | list[HashableT2] | Index = ..., - columns: Hashable | list[HashableT3] | Index = ..., - level: Level | None = ..., - inplace: Literal[True], - errors: IgnoreRaise = ..., - ) -> None: ... - @overload - def drop( - self, - labels: Hashable | list[HashableT1] | Index = ..., - *, - axis: Axis = ..., - index: Hashable | list[HashableT2] | Index = ..., - columns: Hashable | list[HashableT3] | Index = ..., - level: Level | None = ..., - inplace: Literal[False] = ..., - errors: IgnoreRaise = ..., - ) -> Self: ... - @overload - def drop( - self, - labels: Hashable | list[HashableT1] | Index = ..., - *, - axis: Axis = ..., - index: Hashable | list[HashableT2] | Index = ..., - columns: Hashable | list[HashableT3] | Index = ..., - level: Level | None = ..., - inplace: bool = ..., - errors: IgnoreRaise = ..., - ) -> Series | None: ... - @overload def fillna( self, value: Scalar | NAType | dict | Series[S1] | DataFrame | None = ..., diff --git a/tests/test_frame.py b/tests/test_frame.py index 1f4f6bbb4..e17cff54c 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -344,6 +344,20 @@ def test_types_drop() -> None: check(assert_type(df.drop(columns=pd.Index(["col1"])), pd.DataFrame), pd.DataFrame) +def test_arguments_drop() -> None: + # GH 950 + if TYPE_CHECKING_INVALID_USAGE: + df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) + res1 = df.drop() # type: ignore[call-overload] # pyright: ignore[reportCallIssue] + res2 = df.drop([0], columns=["col1"]) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] + res3 = df.drop([0], index=[0]) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] + # These should also fail, but `None` is Hasheable and i do not know how + # to type hint a non-None hashable. + # res4 = df.drop(columns=None) + # res5 = df.drop(index=None) + # res6 = df.drop(None) + + def test_types_dropna() -> None: df = pd.DataFrame(data={"col1": [np.nan, np.nan], "col2": [3, np.nan]}) res: pd.DataFrame = df.dropna() diff --git a/tests/test_series.py b/tests/test_series.py index 8f948faba..470bfb431 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -266,6 +266,20 @@ def test_types_drop() -> None: ) +def test_arguments_drop() -> None: + # GH 950 + if TYPE_CHECKING_INVALID_USAGE: + s = pd.Series([0, 1, 2]) + res1 = s.drop() # type: ignore[call-overload] # pyright: ignore[reportCallIssue] + res2 = s.drop([0], columns=["col1"]) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] + res3 = s.drop([0], index=[0]) # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] + # These should also fail, but `None` is Hasheable and i do not know how + # to type hint a non-None hashable. + # res4 = s.drop(columns=None) + # res5 = s.drop(index=None) + # res6 = s.drop(None) + + def test_types_drop_multilevel() -> None: index = pd.MultiIndex( levels=[["top", "bottom"], ["first", "second", "third"]],