Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
Dtype,
DtypeObj,
F,
IgnoreRaise,
Shape,
npt,
)
Expand Down Expand Up @@ -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.

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
7 changes: 5 additions & 2 deletions pandas/io/json/_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading