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/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/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..0013ddf73cddc 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,61 @@ 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" + # github.com/python/mypy/issues/12387 + @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 +11232,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 +11247,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..8bfd9f3cd962d 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, + 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..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, @@ -46,6 +47,7 @@ Dtype, DtypeObj, F, + IgnoreRaise, Shape, npt, ) @@ -6810,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: str_t = "raise") -> Index: + def drop( + self, + labels: Index | np.ndarray | Iterable[Hashable], + 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 2ff195fa8e5d2..26569b571724d 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..00384ec26f71d 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,61 @@ 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" + # github.com/python/mypy/issues/12387 + @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: diff --git a/pandas/plotting/_matplotlib/misc.py b/pandas/plotting/_matplotlib/misc.py index 6583328f916f1..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")