Skip to content

Commit 2d0fa7e

Browse files
authored
TYP: #1867 Categorical (#1869)
fix: categorical
1 parent aac29bc commit 2d0fa7e

2 files changed

Lines changed: 28 additions & 38 deletions

File tree

pandas-stubs/core/arrays/categorical.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ from pandas._typing import (
3636
SequenceNotStr,
3737
np_1darray,
3838
np_1darray_bool,
39-
np_1darray_str,
4039
np_ndarray_anyint,
4140
np_ndarray_float,
41+
np_ndarray_str,
4242
)
4343

4444
from pandas.core.dtypes.dtypes import (
@@ -50,7 +50,7 @@ from pandas.core.dtypes.dtypes import (
5050
class Categorical(NDArrayBackedExtensionArray, Generic[CategoricalValueT]):
5151
__array_priority__: int = ...
5252
@overload
53-
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
53+
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
5454
cls,
5555
values: Sequence[Never],
5656
categories: SequenceNotStr[Hashable] | AnyArrayLike | None = None,
@@ -62,10 +62,10 @@ class Categorical(NDArrayBackedExtensionArray, Generic[CategoricalValueT]):
6262
def __new__( # pyright: ignore[reportOverlappingOverload]
6363
cls,
6464
values: (
65-
list[str] | np_1darray_str | SequenceNotStr[str] | Series[str] | Index[str]
65+
list[str] | np_ndarray_str | SequenceNotStr[str] | Series[str] | Index[str]
6666
),
6767
categories: (
68-
SequenceNotStr[str] | Series[str] | Index[str] | np_1darray_str | None
68+
SequenceNotStr[str] | Series[str] | Index[str] | np_ndarray_str | None
6969
) = None,
7070
ordered: bool | None = None,
7171
dtype: CategoricalDtype | None = None,
@@ -101,7 +101,7 @@ class Categorical(NDArrayBackedExtensionArray, Generic[CategoricalValueT]):
101101
SequenceNotStr[CategoricalValueT1]
102102
| Series[CategoricalValueT1]
103103
| Index[CategoricalValueT1]
104-
| np_1darray_str
104+
| np_ndarray_str
105105
| None
106106
) = None,
107107
ordered: bool | None = None,

tests/arrays/test_categorical.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import numpy as np
88
import pandas as pd
99
from pandas import Categorical
10-
from pandas.api.typing.aliases import (
11-
Ordered,
12-
)
10+
from pandas.api.typing.aliases import Ordered
1311
from pandas.core.arrays.categorical import CategoricalDtype
1412
from pandas.core.indexes.base import Index
1513

@@ -19,6 +17,7 @@
1917
from tests._typing import (
2018
np_1darray,
2119
np_1darray_bool,
20+
np_ndarray_str,
2221
)
2322

2423

@@ -60,54 +59,45 @@ def test_constructor() -> None:
6059
cat = Categorical(dd)
6160
check(assert_type(cat, "Categorical[str]"), Categorical, str)
6261

63-
values = np.array(["a", "b", "c", "a"])
64-
cat_np = Categorical(values)
65-
# np.array() is typed as ndarray[Any, Any] by numpy stubs, so mypy cannot infer
66-
# the element type; the actual type is Categorical[str]
67-
# TODO: https://github.com/facebook/pyrefly/issues/3891
68-
check(assert_type(cat_np, "Categorical[str]"), Categorical) # type: ignore[assert-type] # pyrefly: ignore[assert-type]
62+
values = check(
63+
assert_type(np.array(["a", "b", "c", "a"], np.str_), np_ndarray_str),
64+
np_1darray,
65+
np.str_,
66+
)
67+
check(assert_type(Categorical(values), "Categorical[str]"), Categorical, str)
6968

7069
cat = Categorical(["a", "b", "c"], categories=["a", "b", "c", "d"])
71-
check(assert_type(cat, "Categorical[str]"), Categorical)
70+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
7271

73-
cat = Categorical(["a", "b", "c"], categories=np.array(["a", "b", "c", "d"]))
74-
# TODO: https://github.com/facebook/pyrefly/issues/3891
75-
check(
76-
assert_type(cat, "Categorical[str]"), # pyrefly: ignore[assert-type]
77-
Categorical,
72+
cat = Categorical(
73+
["a", "b", "c"], categories=np.array(["a", "b", "c", "d"], np.str_)
7874
)
75+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
7976

8077
cat = Categorical(["a", "b", "c"], categories=["a", "b", "c"], ordered=True)
81-
check(assert_type(cat, "Categorical[str]"), Categorical)
78+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
8279

8380
cat = Categorical(["a", "b", "c"], categories=["a", "b", "c"], ordered=False)
84-
check(assert_type(cat, "Categorical[str]"), Categorical)
81+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
8582

8683
cat = Categorical(["a", "b", "c"], categories=["a", "b", "c"], ordered=None)
87-
check(assert_type(cat, "Categorical[str]"), Categorical)
84+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
8885

8986
cat = Categorical(
90-
values=["x", "y", "z", "x"],
91-
categories=["x", "y", "z"],
92-
ordered=True,
93-
copy=True,
87+
values=["x", "y", "z", "x"], categories=["x", "y", "z"], ordered=True, copy=True
9488
)
95-
check(assert_type(cat, "Categorical[str]"), Categorical)
89+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
9690

9791
dtype = pd.CategoricalDtype(categories=["x", "y", "z"], ordered=True)
9892
cat = Categorical(
9993
values=["x", "y", "z", "x"],
10094
dtype=dtype,
10195
copy=True,
10296
)
103-
check(assert_type(cat, "Categorical[str]"), Categorical)
97+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
10498

10599
cat_int = Categorical([1, 2, 3, 1, 2])
106-
# TODO: https://github.com/facebook/pyrefly/issues/3891
107-
check(
108-
assert_type(cat_int, "Categorical[int]"), # pyrefly: ignore[assert-type]
109-
Categorical,
110-
)
100+
check(assert_type(cat_int, "Categorical[int]"), Categorical, int)
111101

112102
cat_mixed = Categorical(["a", 1, "b", 2])
113103
# TODO: https://github.com/facebook/pyrefly/issues/3891
@@ -119,19 +109,19 @@ def test_constructor() -> None:
119109
check(assert_type(cat_empty, Categorical), Categorical)
120110

121111
cat = Categorical(["a", "b", "c"], categories=None)
122-
check(assert_type(cat, "Categorical[str]"), Categorical)
112+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
123113

124114
cat1 = Categorical(["a", "b", "c"])
125115
cat = Categorical(cat1)
126-
check(assert_type(cat, "Categorical[str]"), Categorical)
116+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
127117

128118
values_series = pd.Series(["a", "b", "c", "a"])
129119
cat = Categorical(values_series)
130-
check(assert_type(cat, "Categorical[str]"), Categorical)
120+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
131121

132122
values_index = pd.Index(["a", "b", "c", "a"])
133123
cat = Categorical(values_index)
134-
check(assert_type(cat, "Categorical[str]"), Categorical)
124+
check(assert_type(cat, "Categorical[str]"), Categorical, str)
135125

136126

137127
def test_categorical_dtype() -> None:

0 commit comments

Comments
 (0)