From 098ff0b1bf056e9cfe53c31aefc4abe0184a659a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 18 Mar 2022 17:15:40 -0400 Subject: [PATCH 1/6] TYP: drop --- pandas/_typing.py | 5 ++- pandas/core/dtypes/astype.py | 3 +- pandas/core/frame.py | 60 ++++++++++++++++++++++++---- pandas/core/generic.py | 71 +++++++++++++++++++++++++++------ pandas/core/indexes/base.py | 3 +- pandas/core/internals/blocks.py | 5 ++- pandas/core/series.py | 60 ++++++++++++++++++++++++---- pandas/io/json/_normalize.py | 7 +++- 8 files changed, 179 insertions(+), 35 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 8cab908d62507..e3b3a4774f558 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -297,6 +297,9 @@ def closed(self) -> bool: else: TakeIndexer = Any +# Shared by functions such as drop and astype +IgnoreRaise = Literal["ignore", "raise"] + # Windowing rank methods WindowingRankType = Literal["average", "min", "max"] @@ -311,7 +314,7 @@ def closed(self) -> bool: # datetime and NaTType DatetimeNaTType = Union[datetime, "NaTType"] -DateTimeErrorChoices = Literal["ignore", "raise", "coerce"] +DateTimeErrorChoices = Union[IgnoreRaise, Literal["coerce"]] # sort_index SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"] diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index daae491b09c8a..8d1427976276c 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -19,6 +19,7 @@ from pandas._typing import ( ArrayLike, DtypeObj, + IgnoreRaise, ) from pandas.errors import IntCastingNaNError from pandas.util._exceptions import find_stack_level @@ -235,7 +236,7 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra def astype_array_safe( - values: ArrayLike, dtype, copy: bool = False, errors: str = "raise" + values: ArrayLike, dtype, copy: bool = False, errors: IgnoreRaise = "raise" ) -> ArrayLike: """ Cast array (ndarray or ExtensionArray) to the new dtype. diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4d279b22a4980..43a3889bcc4b5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -58,6 +58,7 @@ FloatFormatType, FormattersType, Frequency, + IgnoreRaise, IndexKeyFunc, IndexLabel, Level, @@ -4831,17 +4832,60 @@ def reindex(self, *args, **kwargs) -> DataFrame: kwargs.pop("labels", None) return super().reindex(**kwargs) - @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) + @overload + def drop( + self, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: Literal[True], + errors: IgnoreRaise = ..., + ) -> None: + ... + + @overload def drop( self, - labels=None, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: Literal[False] = ..., + errors: IgnoreRaise = ..., + ) -> DataFrame: + ... + + @overload + def drop( + self, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: bool = ..., + errors: IgnoreRaise = ..., + ) -> DataFrame | None: + ... + + # error: Signature of "drop" incompatible with supertype "NDFrame" + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) + def drop( # type: ignore[override] + self, + labels: Hashable | list[Hashable] = None, axis: Axis = 0, - index=None, - columns=None, + index: Hashable | list[Hashable] = None, + columns: Hashable | list[Hashable] = None, level: Level | None = None, inplace: bool = False, - errors: str = "raise", - ): + errors: IgnoreRaise = "raise", + ) -> DataFrame | None: """ Drop specified labels from rows or columns. @@ -11187,7 +11231,7 @@ def where( inplace=False, axis=None, level=None, - errors="raise", + errors: IgnoreRaise = "raise", try_cast=lib.no_default, ): return super().where(cond, other, inplace, axis, level, errors, try_cast) @@ -11202,7 +11246,7 @@ def mask( inplace=False, axis=None, level=None, - errors="raise", + errors: IgnoreRaise = "raise", try_cast=lib.no_default, ): return super().mask(cond, other, inplace, axis, level, errors, try_cast) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 983fb8f0edf12..c4e8498c971d4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -44,6 +44,7 @@ DtypeArg, DtypeObj, FilePath, + IgnoreRaise, IndexKeyFunc, IndexLabel, IntervalClosedType, @@ -71,6 +72,7 @@ ) from pandas.util._decorators import ( deprecate_kwarg, + deprecate_nonkeyword_arguments, doc, rewrite_axis_style_signature, ) @@ -4270,16 +4272,59 @@ def reindex_like( return self.reindex(**d) + @overload def drop( - self, - labels=None, - axis=0, - index=None, - columns=None, - level=None, + self: NDFrameT, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: Literal[True], + errors: IgnoreRaise = ..., + ) -> None: + ... + + @overload + def drop( + self: NDFrameT, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: Literal[False] = ..., + errors: IgnoreRaise = ..., + ) -> NDFrameT: + ... + + @overload + def drop( + self: NDFrameT, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: bool_t = ..., + errors: IgnoreRaise = ..., + ) -> NDFrameT | None: + ... + + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) + def drop( + self: NDFrameT, + labels: Hashable | list[Hashable] = None, + axis: Axis = 0, + index: Hashable | list[Hashable] = None, + columns: Hashable | list[Hashable] = None, + level: Level | None = None, inplace: bool_t = False, - errors: str = "raise", - ): + errors: IgnoreRaise = "raise", + ) -> NDFrameT | None: inplace = validate_bool_kwarg(inplace, "inplace") @@ -4312,7 +4357,7 @@ def _drop_axis( labels, axis, level=None, - errors: str = "raise", + errors: IgnoreRaise = "raise", only_slice: bool_t = False, ) -> NDFrameT: """ @@ -5826,7 +5871,7 @@ def dtypes(self): return self._constructor_sliced(data, index=self._info_axis, dtype=np.object_) def astype( - self: NDFrameT, dtype, copy: bool_t = True, errors: str = "raise" + self: NDFrameT, dtype, copy: bool_t = True, errors: IgnoreRaise = "raise" ) -> NDFrameT: """ Cast a pandas object to a specified dtype ``dtype``. @@ -9139,7 +9184,7 @@ def _where( inplace=False, axis=None, level=None, - errors="raise", + errors: IgnoreRaise = "raise", ): """ Equivalent to public method `where`, except that `other` is not @@ -9278,7 +9323,7 @@ def where( inplace=False, axis=None, level=None, - errors="raise", + errors: IgnoreRaise = "raise", try_cast=lib.no_default, ): """ @@ -9431,7 +9476,7 @@ def mask( inplace=False, axis=None, level=None, - errors="raise", + errors: IgnoreRaise = "raise", try_cast=lib.no_default, ): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ab6e1c47057eb..9319782c646a8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -46,6 +46,7 @@ Dtype, DtypeObj, F, + IgnoreRaise, Shape, npt, ) @@ -6810,7 +6811,7 @@ def insert(self, loc: int, item) -> Index: # TODO(2.0) can use Index instead of self._constructor return self._constructor._with_infer(new_values, name=self.name) - def drop(self, labels, errors: str_t = "raise") -> Index: + def drop(self, labels, errors: IgnoreRaise = "raise") -> Index: """ Make new Index with passed list of labels deleted. diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index cdf2c47ad3f60..97e058851df14 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -27,6 +27,7 @@ ArrayLike, DtypeObj, F, + IgnoreRaise, Shape, npt, ) @@ -501,7 +502,9 @@ def dtype(self) -> DtypeObj: return self.values.dtype @final - def astype(self, dtype: DtypeObj, copy: bool = False, errors: str = "raise"): + def astype( + self, dtype: DtypeObj, copy: bool = False, errors: IgnoreRaise = "raise" + ): """ Coerce to the new dtype. diff --git a/pandas/core/series.py b/pandas/core/series.py index f53c82972a602..91feb8ca8bf8b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -38,6 +38,7 @@ Dtype, DtypeObj, FillnaOptions, + IgnoreRaise, IndexKeyFunc, Level, NaPosition, @@ -4763,17 +4764,60 @@ def reindex(self, *args, **kwargs) -> Series: kwargs.update({"index": index}) return super().reindex(**kwargs) - @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) + @overload def drop( self, - labels=None, - axis=0, - index=None, - columns=None, - level=None, - inplace=False, - errors="raise", + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: Literal[True], + errors: IgnoreRaise = ..., + ) -> None: + ... + + @overload + def drop( + self, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: Literal[False] = ..., + errors: IgnoreRaise = ..., ) -> Series: + ... + + @overload + def drop( + self, + labels: Hashable | list[Hashable] = ..., + *, + axis: Axis = ..., + index: Hashable | list[Hashable] = ..., + columns: Hashable | list[Hashable] = ..., + level: Level | None = ..., + inplace: bool = ..., + errors: IgnoreRaise = ..., + ) -> Series | None: + ... + + # error: Signature of "drop" incompatible with supertype "NDFrame" + @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) + def drop( # type: ignore[override] + self, + labels: Hashable | list[Hashable] = None, + axis: Axis = 0, + index: Hashable | list[Hashable] = None, + columns: Hashable | list[Hashable] = None, + level: Level | None = None, + inplace: bool = False, + errors: IgnoreRaise = "raise", + ) -> Series | None: """ Return Series with specified index labels removed. diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py index 36a7949a9f1e3..4a2e49fd85f45 100644 --- a/pandas/io/json/_normalize.py +++ b/pandas/io/json/_normalize.py @@ -16,7 +16,10 @@ import numpy as np from pandas._libs.writers import convert_json_to_lines -from pandas._typing import Scalar +from pandas._typing import ( + IgnoreRaise, + Scalar, +) from pandas.util._decorators import deprecate import pandas as pd @@ -244,7 +247,7 @@ def _json_normalize( meta: str | list[str | list[str]] | None = None, meta_prefix: str | None = None, record_prefix: str | None = None, - errors: str = "raise", + errors: IgnoreRaise = "raise", sep: str = ".", max_level: int | None = None, ) -> DataFrame: From 4e095ef65cc80c759e462e0024b111f94e4e6449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 18 Mar 2022 17:41:09 -0400 Subject: [PATCH 2/6] unrelated(?) type issue --- pandas/plotting/_matplotlib/misc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/misc.py b/pandas/plotting/_matplotlib/misc.py index 6583328f916f1..9beb8672ee41f 100644 --- a/pandas/plotting/_matplotlib/misc.py +++ b/pandas/plotting/_matplotlib/misc.py @@ -389,7 +389,9 @@ def parallel_coordinates( raise ValueError("Length of xticks must match number of columns") x = xticks else: - x = list(range(ncols)) + # error: Incompatible types in assignment (expression has type "List[int]", + # variable has type "Index") + x = list(range(ncols)) # type: ignore[assignment] if ax is None: ax = plt.gca() From 69d732639c96e4ed919dc312c390cc455bf5d39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 18 Mar 2022 22:15:24 -0400 Subject: [PATCH 3/6] Remove unused TypeVar --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c4e8498c971d4..8bfd9f3cd962d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4274,7 +4274,7 @@ def reindex_like( @overload def drop( - self: NDFrameT, + self, labels: Hashable | list[Hashable] = ..., *, axis: Axis = ..., From 4ae5fad552fc03378ca1ea95041abff86a91f04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 19 Mar 2022 13:55:13 -0400 Subject: [PATCH 4/6] index.drop --- pandas/core/common.py | 4 +++- pandas/core/indexes/base.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 62c2034505589..6c3d2c91ab012 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -250,7 +250,9 @@ def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray: return result -def index_labels_to_array(labels, dtype: NpDtype | None = None) -> np.ndarray: +def index_labels_to_array( + labels: np.ndarray | Iterable, dtype: NpDtype | None = None +) -> np.ndarray: """ Transform label or iterable of labels to array, for use in Index. diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9319782c646a8..8b9c537631d94 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -9,6 +9,7 @@ Any, Callable, Hashable, + Iterable, Literal, Sequence, TypeVar, @@ -6811,7 +6812,11 @@ def insert(self, loc: int, item) -> Index: # TODO(2.0) can use Index instead of self._constructor return self._constructor._with_infer(new_values, name=self.name) - def drop(self, labels, errors: IgnoreRaise = "raise") -> Index: + def drop( + self, + labels: Index | np.ndarray | Iterable[Hashable], + errors: IgnoreRaise = "raise", + ) -> Index: """ Make new Index with passed list of labels deleted. From 6cb6e08ad82b7fb0e31e97b814e476d8c359e460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sat, 19 Mar 2022 14:05:49 -0400 Subject: [PATCH 5/6] union for unrelated(?) issue --- pandas/plotting/_matplotlib/misc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/plotting/_matplotlib/misc.py b/pandas/plotting/_matplotlib/misc.py index 9beb8672ee41f..083d85ef0876d 100644 --- a/pandas/plotting/_matplotlib/misc.py +++ b/pandas/plotting/_matplotlib/misc.py @@ -27,6 +27,7 @@ from pandas import ( DataFrame, + Index, Series, ) @@ -378,6 +379,7 @@ def parallel_coordinates( ncols = len(df.columns) # determine values to use for xticks + x: list[int] | Index if use_columns is True: if not np.all(np.isreal(list(df.columns))): raise ValueError("Columns must be numeric to be used as xticks") @@ -389,9 +391,7 @@ def parallel_coordinates( raise ValueError("Length of xticks must match number of columns") x = xticks else: - # error: Incompatible types in assignment (expression has type "List[int]", - # variable has type "Index") - x = list(range(ncols)) # type: ignore[assignment] + x = list(range(ncols)) if ax is None: ax = plt.gca() From 6a98d00f705c259cdeb884e5528ea1d210c1d7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 20 Mar 2022 13:46:16 -0400 Subject: [PATCH 6/6] reference mypy issue --- pandas/core/frame.py | 1 + pandas/core/series.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 43a3889bcc4b5..0013ddf73cddc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4875,6 +4875,7 @@ def drop( ... # error: Signature of "drop" incompatible with supertype "NDFrame" + # github.com/python/mypy/issues/12387 @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) def drop( # type: ignore[override] self, diff --git a/pandas/core/series.py b/pandas/core/series.py index 91feb8ca8bf8b..00384ec26f71d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4807,6 +4807,7 @@ def drop( ... # error: Signature of "drop" incompatible with supertype "NDFrame" + # github.com/python/mypy/issues/12387 @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) def drop( # type: ignore[override] self,