Skip to content

Commit 96199e7

Browse files
authored
TYP: drop (pandas-dev#46423)
1 parent bebe00c commit 96199e7

File tree

10 files changed

+190
-35
lines changed

10 files changed

+190
-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/common.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ def asarray_tuplesafe(values, dtype: NpDtype | None = None) -> np.ndarray:
250250
return result
251251

252252

253-
def index_labels_to_array(labels, dtype: NpDtype | None = None) -> np.ndarray:
253+
def index_labels_to_array(
254+
labels: np.ndarray | Iterable, dtype: NpDtype | None = None
255+
) -> np.ndarray:
254256
"""
255257
Transform label or iterable of labels to array, for use in Index.
256258

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

+53-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,61 @@ 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+
# github.com/python/mypy/issues/12387
4879+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4880+
def drop( # type: ignore[override]
4881+
self,
4882+
labels: Hashable | list[Hashable] = None,
48384883
axis: Axis = 0,
4839-
index=None,
4840-
columns=None,
4884+
index: Hashable | list[Hashable] = None,
4885+
columns: Hashable | list[Hashable] = None,
48414886
level: Level | None = None,
48424887
inplace: bool = False,
4843-
errors: str = "raise",
4844-
):
4888+
errors: IgnoreRaise = "raise",
4889+
) -> DataFrame | None:
48454890
"""
48464891
Drop specified labels from rows or columns.
48474892
@@ -11187,7 +11232,7 @@ def where(
1118711232
inplace=False,
1118811233
axis=None,
1118911234
level=None,
11190-
errors="raise",
11235+
errors: IgnoreRaise = "raise",
1119111236
try_cast=lib.no_default,
1119211237
):
1119311238
return super().where(cond, other, inplace, axis, level, errors, try_cast)
@@ -11202,7 +11247,7 @@ def mask(
1120211247
inplace=False,
1120311248
axis=None,
1120411249
level=None,
11205-
errors="raise",
11250+
errors: IgnoreRaise = "raise",
1120611251
try_cast=lib.no_default,
1120711252
):
1120811253
return super().mask(cond, other, inplace, axis, level, errors, try_cast)

pandas/core/generic.py

+57-12
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(
42744277
self,
4275-
labels=None,
4276-
axis=0,
4277-
index=None,
4278-
columns=None,
4279-
level=None,
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Any,
1010
Callable,
1111
Hashable,
12+
Iterable,
1213
Literal,
1314
Sequence,
1415
TypeVar,
@@ -46,6 +47,7 @@
4647
Dtype,
4748
DtypeObj,
4849
F,
50+
IgnoreRaise,
4951
Shape,
5052
npt,
5153
)
@@ -6810,7 +6812,11 @@ def insert(self, loc: int, item) -> Index:
68106812
# TODO(2.0) can use Index instead of self._constructor
68116813
return self._constructor._with_infer(new_values, name=self.name)
68126814

6813-
def drop(self, labels, errors: str_t = "raise") -> Index:
6815+
def drop(
6816+
self,
6817+
labels: Index | np.ndarray | Iterable[Hashable],
6818+
errors: IgnoreRaise = "raise",
6819+
) -> Index:
68146820
"""
68156821
Make new Index with passed list of labels deleted.
68166822

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

+53-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,61 @@ 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+
# github.com/python/mypy/issues/12387
4811+
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
4812+
def drop( # type: ignore[override]
4813+
self,
4814+
labels: Hashable | list[Hashable] = None,
4815+
axis: Axis = 0,
4816+
index: Hashable | list[Hashable] = None,
4817+
columns: Hashable | list[Hashable] = None,
4818+
level: Level | None = None,
4819+
inplace: bool = False,
4820+
errors: IgnoreRaise = "raise",
4821+
) -> Series | None:
47774822
"""
47784823
Return Series with specified index labels removed.
47794824

0 commit comments

Comments
 (0)