Skip to content

Commit 5f79e32

Browse files
committed
Categorical
1 parent a2087e5 commit 5f79e32

File tree

9 files changed

+71
-21
lines changed

9 files changed

+71
-21
lines changed

pandas-stubs/core/construction.pyi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ from pandas.core.indexes.period import PeriodIndex
3030
from pandas.core.indexes.range import RangeIndex
3131
from pandas.core.indexes.timedeltas import TimedeltaIndex
3232
from pandas.core.series import Series
33-
from typing_extensions import Never
3433

3534
from pandas._libs.interval import Interval
3635
from pandas._libs.missing import NAType
@@ -66,13 +65,13 @@ from pandas.core.dtypes.dtypes import (
6665
)
6766

6867
@overload
69-
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
70-
data: Sequence[Never] | Index[Never] | Series[Never],
71-
dtype: str | np.dtype | ExtensionDtype | None = None,
68+
def array( # type: ignore[overload-overlap]
69+
data: SequenceNotStr[Any] | np_ndarray | ExtensionArray | Index | Series,
70+
dtype: CategoryDtypeArg,
7271
copy: bool = True,
73-
) -> ExtensionArray: ...
72+
) -> Categorical: ...
7473
@overload
75-
def array( # type: ignore[overload-overlap]
74+
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
7675
data: Sequence[NAType | None],
7776
dtype: str | np.dtype | ExtensionDtype | None = None,
7877
copy: bool = True,
@@ -137,21 +136,22 @@ def array( # type: ignore[overload-overlap]
137136
copy: bool = True,
138137
) -> PeriodArray: ...
139138
@overload
140-
def array( # type: ignore[overload-overlap]
139+
def array(
141140
# float("nan") also works, but I don't know how to put it in
142141
data: Sequence[IntervalT | None] | IntervalArray | IntervalIndex | Series[Interval],
143142
dtype: IntervalDtype | None = None,
144143
copy: bool = True,
145144
) -> IntervalArray: ...
146145
@overload
147146
def array(
147+
# TODO: Categorical Series pandas-dev/pandas-stubs#1415
148148
data: Categorical | CategoricalIndex,
149149
dtype: CategoryDtypeArg | None = None,
150150
copy: bool = True,
151151
) -> Categorical: ...
152152
@overload
153153
def array(
154-
data: Sequence[object] | np.typing.NDArray[np.object_] | RangeIndex,
154+
data: SequenceNotStr[object] | np.typing.NDArray[np.object_] | RangeIndex,
155155
dtype: str | np.dtype | ExtensionDtype | None = None,
156156
copy: bool = True,
157157
) -> NumpyExtensionArray: ...
@@ -169,7 +169,7 @@ def array(
169169
) -> ArrowExtensionArray: ...
170170
@overload
171171
def array(
172-
data: Sequence[Any] | np_ndarray | ExtensionArray | Index[Any] | Series[Any],
172+
data: SequenceNotStr[Any] | np_ndarray | ExtensionArray | Index | Series,
173173
dtype: str | np.dtype | ExtensionDtype | None = None,
174174
copy: bool = True,
175175
) -> ExtensionArray: ...

tests/arrays/test_categorical.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
import pandas as pd
3+
from pandas.core.arrays.categorical import Categorical
4+
from typing_extensions import assert_type
5+
6+
from tests import check
7+
8+
9+
def test_constructor() -> None:
10+
check(assert_type(pd.array(["🐼"], dtype="category"), Categorical), Categorical)
11+
check(
12+
assert_type( # type: ignore[assert-type] # I do not understand
13+
pd.array(np.array(["🐼"]), dtype="category"), Categorical
14+
),
15+
Categorical,
16+
)
17+
check(
18+
assert_type(pd.array(pd.array(["🐼"]), dtype="category"), Categorical),
19+
Categorical,
20+
)
21+
check(
22+
assert_type(pd.array(pd.Index(["🐼"]), dtype="category"), Categorical),
23+
Categorical,
24+
)
25+
check(
26+
assert_type(pd.array(pd.Series(["🐼"]), dtype="category"), Categorical),
27+
Categorical,
28+
)
29+
30+
check(
31+
assert_type(pd.array(pd.array(["🐼"], dtype="category")), Categorical),
32+
Categorical,
33+
)
34+
check(
35+
assert_type( # type: ignore[assert-type] # I do not understand
36+
pd.array(pd.Index(["🐼"], dtype="category")), Categorical
37+
),
38+
Categorical,
39+
)
40+
# TODO: Categorical Series pandas-dev/pandas-stubs#1415
41+
# check(
42+
# assert_type(pd.array(pd.Series(["🐼"], dtype="category")), Categorical),
43+
# Categorical,
44+
# )

tests/arrays/test_datetime_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_constructor() -> None:
2525
check(assert_type(pd.array(dt_nat), DatetimeArray), DatetimeArray)
2626

2727
check(
28-
assert_type( # type: ignore[assert-type] # I do not understand
28+
assert_type( # type: ignore[assert-type] # I do not understand
2929
pd.array(np.array([dt], np.datetime64)), DatetimeArray
3030
),
3131
DatetimeArray,

tests/arrays/test_floating_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_constructor() -> None:
1313
check(assert_type(pd.array([1.0, pd.NA, None]), FloatingArray), FloatingArray)
1414

1515
check(
16-
assert_type( # type: ignore[assert-type] # I do not understand
16+
assert_type( # type: ignore[assert-type] # I do not understand
1717
pd.array(np.array([1.0], np.float64)), FloatingArray
1818
),
1919
FloatingArray,

tests/arrays/test_integer_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_constructor() -> None:
1313
check(assert_type(pd.array([1, pd.NA, None]), IntegerArray), IntegerArray)
1414

1515
check(
16-
assert_type( # type: ignore[assert-type] # I do not understand
16+
assert_type( # type: ignore[assert-type] # I do not understand
1717
pd.array(np.array([1], np.int64)), IntegerArray
1818
),
1919
IntegerArray,

tests/arrays/test_interval_array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
def test_constructor() -> None:
99
itv = pd.Interval(0, 1)
1010
check(
11-
assert_type( # type: ignore[assert-type] # I do not understand
11+
assert_type( # type: ignore[assert-type] # I do not understand
1212
pd.array([itv]), IntervalArray
1313
),
1414
IntervalArray,
1515
)
1616
check(
17-
assert_type( # type: ignore[assert-type] # I do not understand
17+
assert_type( # type: ignore[assert-type] # I do not understand
1818
pd.array([itv, None]), IntervalArray
1919
),
2020
IntervalArray,
2121
)
2222

2323
check(
24-
assert_type( # type: ignore[assert-type] # I do not understand
24+
assert_type( # type: ignore[assert-type] # I do not understand
2525
pd.array(pd.array([itv])), IntervalArray
2626
),
2727
IntervalArray,

tests/arrays/test_period_array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
def test_constructor() -> None:
99
prd = pd.Period("2023-01-01")
1010
check(
11-
assert_type( # type: ignore[assert-type] # I do not understand
11+
assert_type( # type: ignore[assert-type] # I do not understand
1212
pd.array([prd]), PeriodArray
1313
),
1414
PeriodArray,
1515
)
1616
check(
17-
assert_type( # type: ignore[assert-type] # I do not understand
17+
assert_type( # type: ignore[assert-type] # I do not understand
1818
pd.array([prd, None]), PeriodArray
1919
),
2020
PeriodArray,
2121
)
2222
check(
23-
assert_type( # type: ignore[assert-type] # I do not understand
23+
assert_type( # type: ignore[assert-type] # I do not understand
2424
pd.array([prd, pd.NaT, None]), PeriodArray
2525
),
2626
PeriodArray,
2727
)
2828

2929
check(
30-
assert_type( # type: ignore[assert-type] # I do not understand
30+
assert_type( # type: ignore[assert-type] # I do not understand
3131
pd.array(pd.array([prd])), PeriodArray
3232
),
3333
PeriodArray,

tests/arrays/test_string_array.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from pandas.core.arrays.string_ import StringArray
44
from typing_extensions import assert_type
55

6-
from tests import check
6+
from tests import (
7+
TYPE_CHECKING_INVALID_USAGE,
8+
check,
9+
)
710

811

912
def test_constructor() -> None:
@@ -16,3 +19,6 @@ def test_constructor() -> None:
1619
check(assert_type(pd.array(["🐼", pd.NA, None]), StringArray), StringArray)
1720

1821
check(assert_type(pd.array(pd.array(["🐼"])), StringArray), StringArray)
22+
23+
if TYPE_CHECKING_INVALID_USAGE:
24+
pd.array("🐼🎫") # type: ignore[arg-type] # pyright: ignore[reportArgumentType,reportCallIssue]

tests/arrays/test_timedelta_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_constructor() -> None:
2020
check(assert_type(pd.array([td, pd.NaT, None]), TimedeltaArray), TimedeltaArray)
2121

2222
check(
23-
assert_type( # type: ignore[assert-type] # I do not understand
23+
assert_type( # type: ignore[assert-type] # I do not understand
2424
pd.array(np.array([td], np.timedelta64)), TimedeltaArray
2525
),
2626
TimedeltaArray,

0 commit comments

Comments
 (0)