Skip to content

Commit 098ff0b

Browse files
committed
TYP: drop
1 parent 55d1aca commit 098ff0b

File tree

8 files changed

+179
-35
lines changed

8 files changed

+179
-35
lines changed

pandas/_typing.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ def closed(self) -> bool:
297297
else:
298298
TakeIndexer = Any
299299

300+
# Shared by functions such as drop and astype
301+
IgnoreRaise = Literal["ignore", "raise"]
302+
300303
# Windowing rank methods
301304
WindowingRankType = Literal["average", "min", "max"]
302305

@@ -311,7 +314,7 @@ def closed(self) -> bool:
311314

312315
# datetime and NaTType
313316
DatetimeNaTType = Union[datetime, "NaTType"]
314-
DateTimeErrorChoices = Literal["ignore", "raise", "coerce"]
317+
DateTimeErrorChoices = Union[IgnoreRaise, Literal["coerce"]]
315318

316319
# sort_index
317320
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]

pandas/core/dtypes/astype.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pandas._typing import (
2020
ArrayLike,
2121
DtypeObj,
22+
IgnoreRaise,
2223
)
2324
from pandas.errors import IntCastingNaNError
2425
from pandas.util._exceptions import find_stack_level
@@ -235,7 +236,7 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
235236

236237

237238
def astype_array_safe(
238-
values: ArrayLike, dtype, copy: bool = False, errors: str = "raise"
239+
values: ArrayLike, dtype, copy: bool = False, errors: IgnoreRaise = "raise"
239240
) -> ArrayLike:
240241
"""
241242
Cast array (ndarray or ExtensionArray) to the new dtype.

pandas/core/frame.py

+52-8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
FloatFormatType,
5959
FormattersType,
6060
Frequency,
61+
IgnoreRaise,
6162
IndexKeyFunc,
6263
IndexLabel,
6364
Level,
@@ -4831,17 +4832,60 @@ def reindex(self, *args, **kwargs) -> DataFrame:
48314832
kwargs.pop("labels", None)
48324833
return super().reindex(**kwargs)
48334834

4834-
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4835+
@overload
4836+
def drop(
4837+
self,
4838+
labels: Hashable | list[Hashable] = ...,
4839+
*,
4840+
axis: Axis = ...,
4841+
index: Hashable | list[Hashable] = ...,
4842+
columns: Hashable | list[Hashable] = ...,
4843+
level: Level | None = ...,
4844+
inplace: Literal[True],
4845+
errors: IgnoreRaise = ...,
4846+
) -> None:
4847+
...
4848+
4849+
@overload
48354850
def drop(
48364851
self,
4837-
labels=None,
4852+
labels: Hashable | list[Hashable] = ...,
4853+
*,
4854+
axis: Axis = ...,
4855+
index: Hashable | list[Hashable] = ...,
4856+
columns: Hashable | list[Hashable] = ...,
4857+
level: Level | None = ...,
4858+
inplace: Literal[False] = ...,
4859+
errors: IgnoreRaise = ...,
4860+
) -> DataFrame:
4861+
...
4862+
4863+
@overload
4864+
def drop(
4865+
self,
4866+
labels: Hashable | list[Hashable] = ...,
4867+
*,
4868+
axis: Axis = ...,
4869+
index: Hashable | list[Hashable] = ...,
4870+
columns: Hashable | list[Hashable] = ...,
4871+
level: Level | None = ...,
4872+
inplace: bool = ...,
4873+
errors: IgnoreRaise = ...,
4874+
) -> DataFrame | None:
4875+
...
4876+
4877+
# error: Signature of "drop" incompatible with supertype "NDFrame"
4878+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4879+
def drop( # type: ignore[override]
4880+
self,
4881+
labels: Hashable | list[Hashable] = None,
48384882
axis: Axis = 0,
4839-
index=None,
4840-
columns=None,
4883+
index: Hashable | list[Hashable] = None,
4884+
columns: Hashable | list[Hashable] = None,
48414885
level: Level | None = None,
48424886
inplace: bool = False,
4843-
errors: str = "raise",
4844-
):
4887+
errors: IgnoreRaise = "raise",
4888+
) -> DataFrame | None:
48454889
"""
48464890
Drop specified labels from rows or columns.
48474891
@@ -11187,7 +11231,7 @@ def where(
1118711231
inplace=False,
1118811232
axis=None,
1118911233
level=None,
11190-
errors="raise",
11234+
errors: IgnoreRaise = "raise",
1119111235
try_cast=lib.no_default,
1119211236
):
1119311237
return super().where(cond, other, inplace, axis, level, errors, try_cast)
@@ -11202,7 +11246,7 @@ def mask(
1120211246
inplace=False,
1120311247
axis=None,
1120411248
level=None,
11205-
errors="raise",
11249+
errors: IgnoreRaise = "raise",
1120611250
try_cast=lib.no_default,
1120711251
):
1120811252
return super().mask(cond, other, inplace, axis, level, errors, try_cast)

pandas/core/generic.py

+58-13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
DtypeArg,
4545
DtypeObj,
4646
FilePath,
47+
IgnoreRaise,
4748
IndexKeyFunc,
4849
IndexLabel,
4950
IntervalClosedType,
@@ -71,6 +72,7 @@
7172
)
7273
from pandas.util._decorators import (
7374
deprecate_kwarg,
75+
deprecate_nonkeyword_arguments,
7476
doc,
7577
rewrite_axis_style_signature,
7678
)
@@ -4270,16 +4272,59 @@ def reindex_like(
42704272

42714273
return self.reindex(**d)
42724274

4275+
@overload
42734276
def drop(
4274-
self,
4275-
labels=None,
4276-
axis=0,
4277-
index=None,
4278-
columns=None,
4279-
level=None,
4277+
self: NDFrameT,
4278+
labels: Hashable | list[Hashable] = ...,
4279+
*,
4280+
axis: Axis = ...,
4281+
index: Hashable | list[Hashable] = ...,
4282+
columns: Hashable | list[Hashable] = ...,
4283+
level: Level | None = ...,
4284+
inplace: Literal[True],
4285+
errors: IgnoreRaise = ...,
4286+
) -> None:
4287+
...
4288+
4289+
@overload
4290+
def drop(
4291+
self: NDFrameT,
4292+
labels: Hashable | list[Hashable] = ...,
4293+
*,
4294+
axis: Axis = ...,
4295+
index: Hashable | list[Hashable] = ...,
4296+
columns: Hashable | list[Hashable] = ...,
4297+
level: Level | None = ...,
4298+
inplace: Literal[False] = ...,
4299+
errors: IgnoreRaise = ...,
4300+
) -> NDFrameT:
4301+
...
4302+
4303+
@overload
4304+
def drop(
4305+
self: NDFrameT,
4306+
labels: Hashable | list[Hashable] = ...,
4307+
*,
4308+
axis: Axis = ...,
4309+
index: Hashable | list[Hashable] = ...,
4310+
columns: Hashable | list[Hashable] = ...,
4311+
level: Level | None = ...,
4312+
inplace: bool_t = ...,
4313+
errors: IgnoreRaise = ...,
4314+
) -> NDFrameT | None:
4315+
...
4316+
4317+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4318+
def drop(
4319+
self: NDFrameT,
4320+
labels: Hashable | list[Hashable] = None,
4321+
axis: Axis = 0,
4322+
index: Hashable | list[Hashable] = None,
4323+
columns: Hashable | list[Hashable] = None,
4324+
level: Level | None = None,
42804325
inplace: bool_t = False,
4281-
errors: str = "raise",
4282-
):
4326+
errors: IgnoreRaise = "raise",
4327+
) -> NDFrameT | None:
42834328

42844329
inplace = validate_bool_kwarg(inplace, "inplace")
42854330

@@ -4312,7 +4357,7 @@ def _drop_axis(
43124357
labels,
43134358
axis,
43144359
level=None,
4315-
errors: str = "raise",
4360+
errors: IgnoreRaise = "raise",
43164361
only_slice: bool_t = False,
43174362
) -> NDFrameT:
43184363
"""
@@ -5826,7 +5871,7 @@ def dtypes(self):
58265871
return self._constructor_sliced(data, index=self._info_axis, dtype=np.object_)
58275872

58285873
def astype(
5829-
self: NDFrameT, dtype, copy: bool_t = True, errors: str = "raise"
5874+
self: NDFrameT, dtype, copy: bool_t = True, errors: IgnoreRaise = "raise"
58305875
) -> NDFrameT:
58315876
"""
58325877
Cast a pandas object to a specified dtype ``dtype``.
@@ -9139,7 +9184,7 @@ def _where(
91399184
inplace=False,
91409185
axis=None,
91419186
level=None,
9142-
errors="raise",
9187+
errors: IgnoreRaise = "raise",
91439188
):
91449189
"""
91459190
Equivalent to public method `where`, except that `other` is not
@@ -9278,7 +9323,7 @@ def where(
92789323
inplace=False,
92799324
axis=None,
92809325
level=None,
9281-
errors="raise",
9326+
errors: IgnoreRaise = "raise",
92829327
try_cast=lib.no_default,
92839328
):
92849329
"""
@@ -9431,7 +9476,7 @@ def mask(
94319476
inplace=False,
94329477
axis=None,
94339478
level=None,
9434-
errors="raise",
9479+
errors: IgnoreRaise = "raise",
94359480
try_cast=lib.no_default,
94369481
):
94379482

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
Dtype,
4747
DtypeObj,
4848
F,
49+
IgnoreRaise,
4950
Shape,
5051
npt,
5152
)
@@ -6810,7 +6811,7 @@ def insert(self, loc: int, item) -> Index:
68106811
# TODO(2.0) can use Index instead of self._constructor
68116812
return self._constructor._with_infer(new_values, name=self.name)
68126813

6813-
def drop(self, labels, errors: str_t = "raise") -> Index:
6814+
def drop(self, labels, errors: IgnoreRaise = "raise") -> Index:
68146815
"""
68156816
Make new Index with passed list of labels deleted.
68166817

pandas/core/internals/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ArrayLike,
2828
DtypeObj,
2929
F,
30+
IgnoreRaise,
3031
Shape,
3132
npt,
3233
)
@@ -501,7 +502,9 @@ def dtype(self) -> DtypeObj:
501502
return self.values.dtype
502503

503504
@final
504-
def astype(self, dtype: DtypeObj, copy: bool = False, errors: str = "raise"):
505+
def astype(
506+
self, dtype: DtypeObj, copy: bool = False, errors: IgnoreRaise = "raise"
507+
):
505508
"""
506509
Coerce to the new dtype.
507510

pandas/core/series.py

+52-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Dtype,
3939
DtypeObj,
4040
FillnaOptions,
41+
IgnoreRaise,
4142
IndexKeyFunc,
4243
Level,
4344
NaPosition,
@@ -4763,17 +4764,60 @@ def reindex(self, *args, **kwargs) -> Series:
47634764
kwargs.update({"index": index})
47644765
return super().reindex(**kwargs)
47654766

4766-
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4767+
@overload
47674768
def drop(
47684769
self,
4769-
labels=None,
4770-
axis=0,
4771-
index=None,
4772-
columns=None,
4773-
level=None,
4774-
inplace=False,
4775-
errors="raise",
4770+
labels: Hashable | list[Hashable] = ...,
4771+
*,
4772+
axis: Axis = ...,
4773+
index: Hashable | list[Hashable] = ...,
4774+
columns: Hashable | list[Hashable] = ...,
4775+
level: Level | None = ...,
4776+
inplace: Literal[True],
4777+
errors: IgnoreRaise = ...,
4778+
) -> None:
4779+
...
4780+
4781+
@overload
4782+
def drop(
4783+
self,
4784+
labels: Hashable | list[Hashable] = ...,
4785+
*,
4786+
axis: Axis = ...,
4787+
index: Hashable | list[Hashable] = ...,
4788+
columns: Hashable | list[Hashable] = ...,
4789+
level: Level | None = ...,
4790+
inplace: Literal[False] = ...,
4791+
errors: IgnoreRaise = ...,
47764792
) -> Series:
4793+
...
4794+
4795+
@overload
4796+
def drop(
4797+
self,
4798+
labels: Hashable | list[Hashable] = ...,
4799+
*,
4800+
axis: Axis = ...,
4801+
index: Hashable | list[Hashable] = ...,
4802+
columns: Hashable | list[Hashable] = ...,
4803+
level: Level | None = ...,
4804+
inplace: bool = ...,
4805+
errors: IgnoreRaise = ...,
4806+
) -> Series | None:
4807+
...
4808+
4809+
# error: Signature of "drop" incompatible with supertype "NDFrame"
4810+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4811+
def drop( # type: ignore[override]
4812+
self,
4813+
labels: Hashable | list[Hashable] = None,
4814+
axis: Axis = 0,
4815+
index: Hashable | list[Hashable] = None,
4816+
columns: Hashable | list[Hashable] = None,
4817+
level: Level | None = None,
4818+
inplace: bool = False,
4819+
errors: IgnoreRaise = "raise",
4820+
) -> Series | None:
47774821
"""
47784822
Return Series with specified index labels removed.
47794823

pandas/io/json/_normalize.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
import numpy as np
1717

1818
from pandas._libs.writers import convert_json_to_lines
19-
from pandas._typing import Scalar
19+
from pandas._typing import (
20+
IgnoreRaise,
21+
Scalar,
22+
)
2023
from pandas.util._decorators import deprecate
2124

2225
import pandas as pd
@@ -244,7 +247,7 @@ def _json_normalize(
244247
meta: str | list[str | list[str]] | None = None,
245248
meta_prefix: str | None = None,
246249
record_prefix: str | None = None,
247-
errors: str = "raise",
250+
errors: IgnoreRaise = "raise",
248251
sep: str = ".",
249252
max_level: int | None = None,
250253
) -> DataFrame:

0 commit comments

Comments
 (0)