Skip to content

Commit 8298620

Browse files
check_untyped_defs pandas.core.arrays.categorical closes #28669
1 parent d2bc6a4 commit 8298620

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

pandas/core/arrays/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,9 @@ def _values_for_argsort(self) -> np.ndarray:
428428
# Note: this is used in `ExtensionArray.argsort`.
429429
return np.array(self)
430430

431-
def argsort(self, ascending=True, kind="quicksort", *args, **kwargs):
431+
def argsort(
432+
self, ascending: bool = True, kind: str = "quicksort", *args, **kwargs
433+
) -> np.ndarray:
432434
"""
433435
Return the indices that would sort this array.
434436
@@ -444,7 +446,7 @@ def argsort(self, ascending=True, kind="quicksort", *args, **kwargs):
444446
445447
Returns
446448
-------
447-
index_array : ndarray
449+
ndarray
448450
Array of indices that sort ``self``. If NaN values are contained,
449451
NaN values are placed at the end.
450452

pandas/core/arrays/categorical.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import operator
44
from shutil import get_terminal_size
55
import textwrap
6-
from typing import Type, Union, cast
6+
from typing import Optional, Type, Union, cast
77
from warnings import warn
88

99
import numpy as np
@@ -368,6 +368,7 @@ def __init__(
368368
values = _convert_to_list_like(values)
369369

370370
# By convention, empty lists result in object dtype:
371+
sanitize_dtype: Optional[str]
371372
if len(values) == 0:
372373
sanitize_dtype = "object"
373374
else:
@@ -670,6 +671,7 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
670671
dtype = CategoricalDtype._from_values_or_dtype(
671672
categories=categories, ordered=ordered, dtype=dtype
672673
)
674+
msg: Optional[str]
673675
if dtype.categories is None:
674676
msg = (
675677
"The categories must be provided in 'categories' or "
@@ -1115,7 +1117,7 @@ def remove_categories(self, removals, inplace=False):
11151117
removals = [removals]
11161118

11171119
removal_set = set(list(removals))
1118-
not_included = removal_set - set(self.dtype.categories)
1120+
not_included = list(removal_set - set(self.dtype.categories))
11191121
new_categories = [c for c in self.dtype.categories if c not in removal_set]
11201122

11211123
# GH 10156
@@ -1561,7 +1563,9 @@ def check_for_ordered(self, op):
15611563
def _values_for_argsort(self):
15621564
return self._codes.copy()
15631565

1564-
def argsort(self, ascending=True, kind="quicksort", *args, **kwargs):
1566+
def argsort(
1567+
self, ascending: bool = True, kind: str = "quicksort", *args, **kwargs
1568+
) -> np.ndarray:
15651569
"""
15661570
Return the indices that would sort the Categorical.
15671571
@@ -1612,7 +1616,14 @@ def argsort(self, ascending=True, kind="quicksort", *args, **kwargs):
16121616
>>> cat.argsort()
16131617
array([2, 0, 1])
16141618
"""
1615-
return super().argsort(ascending=ascending, kind=kind, *args, **kwargs)
1619+
# https://github.com/python/mypy/issues/2582
1620+
# error: "argsort" of "ExtensionArray" gets multiple values for keyword
1621+
# argument "ascending" [misc]
1622+
# error: "argsort" of "ExtensionArray" gets multiple values for keyword
1623+
# argument "kind" [misc]
1624+
return super().argsort( # type: ignore[misc]
1625+
ascending=ascending, kind=kind, *args, **kwargs
1626+
)
16161627

16171628
def sort_values(self, inplace=False, ascending=True, na_position="last"):
16181629
"""
@@ -2193,8 +2204,8 @@ def _reverse_indexer(self):
21932204
self.codes.astype("int64"), categories.size
21942205
)
21952206
counts = counts.cumsum()
2196-
result = (r[start:end] for start, end in zip(counts, counts[1:]))
2197-
result = dict(zip(categories, result))
2207+
result_ = (r[start:end] for start, end in zip(counts, counts[1:]))
2208+
result = dict(zip(categories, result_))
21982209
return result
21992210

22002211
# reduction ops #

pandas/core/sorting.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ def lexsort_indexer(keys, orders=None, na_position="last"):
235235
return indexer_from_factorized(labels, shape)
236236

237237

238-
def nargsort(items, kind="quicksort", ascending=True, na_position="last"):
238+
def nargsort(
239+
items, kind: str = "quicksort", ascending: bool = True, na_position: str = "last"
240+
) -> np.ndarray:
239241
"""
240242
This is intended to be a drop-in replacement for np.argsort which
241243
handles NaNs. It adds ascending and na_position parameters.

pandas/tests/arrays/categorical/test_api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -340,9 +342,13 @@ def test_remove_categories(self):
340342
assert res is None
341343

342344
# removal is not in categories
343-
with pytest.raises(ValueError):
345+
msg = re.escape("removals must all be in old categories: ['c']")
346+
with pytest.raises(ValueError, match=msg):
344347
cat.remove_categories(["c"])
345348

349+
with pytest.raises(ValueError, match=msg):
350+
cat.remove_categories(["c", np.nan])
351+
346352
def test_remove_unused_categories(self):
347353
c = Categorical(["a", "b", "c", "d", "a"], categories=["a", "b", "c", "d", "e"])
348354
exp_categories_all = Index(["a", "b", "c", "d", "e"])

setup.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ check_untyped_defs=False
173173
[mypy-pandas._version]
174174
check_untyped_defs=False
175175

176-
[mypy-pandas.core.arrays.categorical]
177-
check_untyped_defs=False
178-
179176
[mypy-pandas.core.arrays.interval]
180177
check_untyped_defs=False
181178

0 commit comments

Comments
 (0)