Skip to content

TYP: drop #46423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ def closed(self) -> bool:
else:
TakeIndexer = Any

# Shared by functions such as drop and astype
IgnoreRaise = Literal["ignore", "raise"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added it to a few other functions (there might be more that only accept ignore/raise).


# Windowing rank methods
WindowingRankType = Literal["average", "min", "max"]

Expand All @@ -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"]
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pandas._typing import (
ArrayLike,
DtypeObj,
IgnoreRaise,
)
from pandas.errors import IntCastingNaNError
from pandas.util._exceptions import find_stack_level
Expand Down Expand Up @@ -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.
Expand Down
60 changes: 52 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
FloatFormatType,
FormattersType,
Frequency,
IgnoreRaise,
IndexKeyFunc,
IndexLabel,
Level,
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
69 changes: 57 additions & 12 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
DtypeArg,
DtypeObj,
FilePath,
IgnoreRaise,
IndexKeyFunc,
IndexLabel,
IntervalClosedType,
Expand Down Expand Up @@ -71,6 +72,7 @@
)
from pandas.util._decorators import (
deprecate_kwarg,
deprecate_nonkeyword_arguments,
doc,
rewrite_axis_style_signature,
)
Expand Down Expand Up @@ -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"])
Copy link
Member Author

@twoertwein twoertwein Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deprecation is new. Previously, this deprecation was only in Series/DataFrame.

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")

Expand Down Expand Up @@ -4312,7 +4357,7 @@ def _drop_axis(
labels,
axis,
level=None,
errors: str = "raise",
errors: IgnoreRaise = "raise",
only_slice: bool_t = False,
) -> NDFrameT:
"""
Expand Down Expand Up @@ -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``.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -9278,7 +9323,7 @@ def where(
inplace=False,
axis=None,
level=None,
errors="raise",
errors: IgnoreRaise = "raise",
try_cast=lib.no_default,
):
"""
Expand Down Expand Up @@ -9431,7 +9476,7 @@ def mask(
inplace=False,
axis=None,
level=None,
errors="raise",
errors: IgnoreRaise = "raise",
try_cast=lib.no_default,
):

Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Any,
Callable,
Hashable,
Iterable,
Literal,
Sequence,
TypeVar,
Expand Down Expand Up @@ -46,6 +47,7 @@
Dtype,
DtypeObj,
F,
IgnoreRaise,
Shape,
npt,
)
Expand Down Expand Up @@ -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.

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ArrayLike,
DtypeObj,
F,
IgnoreRaise,
Shape,
npt,
)
Expand Down Expand Up @@ -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.

Expand Down
60 changes: 52 additions & 8 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
Dtype,
DtypeObj,
FillnaOptions,
IgnoreRaise,
IndexKeyFunc,
Level,
NaPosition,
Expand Down Expand Up @@ -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.

Expand Down
Loading