Skip to content

Commit a9f0bd3

Browse files
GH1383 Add support for creating Series/Index with dtype='category' (#1391)
* GH1383 Add support for creating Series/Index with dtype='category' * GH1383 Add support for creating Series/Index with dtype='category' * GH1383 Add support for creating Series/Index with dtype='category' * GH1383 Add support for creating Series/Index with dtype='category'
1 parent 0042dfd commit a9f0bd3

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ from pandas.core.base import (
4141
NumListLike,
4242
_ListLike,
4343
)
44+
from pandas.core.indexes.category import CategoricalIndex
4445
from pandas.core.strings.accessor import StringMethods
4546
from typing_extensions import (
4647
Never,
@@ -57,6 +58,7 @@ from pandas._typing import (
5758
AnyAll,
5859
ArrayLike,
5960
AxesData,
61+
CategoryDtypeArg,
6062
DropKeep,
6163
Dtype,
6264
DtypeArg,
@@ -228,6 +230,16 @@ class Index(IndexOpsMixin[S1]):
228230
tupleize_cols: bool = ...,
229231
) -> TimedeltaIndex: ...
230232
@overload
233+
def __new__(
234+
cls,
235+
data: AxesData,
236+
*,
237+
dtype: CategoryDtypeArg,
238+
copy: bool = ...,
239+
name: Hashable = ...,
240+
tupleize_cols: bool = ...,
241+
) -> CategoricalIndex: ...
242+
@overload
231243
def __new__(
232244
cls,
233245
data: Sequence[Interval[_OrderableT]] | IndexOpsMixin[Interval[_OrderableT]],

pandas-stubs/core/series.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ class Series(IndexOpsMixin[S1], NDFrame):
359359
copy: bool = ...,
360360
) -> Series[Timestamp]: ...
361361
@overload
362+
def __new__(
363+
cls,
364+
data: _ListLike,
365+
index: AxesData | None = ...,
366+
*,
367+
dtype: CategoryDtypeArg,
368+
name: Hashable = ...,
369+
copy: bool = ...,
370+
) -> Series[CategoricalDtype]: ...
371+
@overload
362372
def __new__(
363373
cls,
364374
data: PeriodIndex | Sequence[Period],

tests/indexes/test_indexes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pandas.core.arrays.interval import IntervalArray
1919
from pandas.core.arrays.timedeltas import TimedeltaArray
2020
from pandas.core.indexes.base import Index
21+
from pandas.core.indexes.category import CategoricalIndex
2122
from typing_extensions import (
2223
Never,
2324
assert_type,
@@ -1369,6 +1370,12 @@ def test_index_factorize() -> None:
13691370
check(assert_type(idx_uniques, np_1darray | Index | Categorical), pd.Index)
13701371

13711372

1373+
def test_index_categorical() -> None:
1374+
"""Test creating an index with Categorical type GH1383."""
1375+
sr = pd.Index([1], dtype="category")
1376+
check(assert_type(sr, CategoricalIndex), CategoricalIndex)
1377+
1378+
13721379
def test_disallow_empty_index() -> None:
13731380
# From GH 826
13741381
if TYPE_CHECKING_INVALID_USAGE:

tests/series/test_properties.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import (
22
TYPE_CHECKING,
3-
cast,
43
)
54

65
import numpy as np
@@ -54,12 +53,9 @@ def test_dt_property() -> None:
5453

5554
def test_array_property() -> None:
5655
"""Test that Series.array returns ExtensionArray and its subclasses"""
57-
# casting due to pandas-dev/pandas-stubs#1383
5856
check(
5957
assert_type(
60-
cast(
61-
"pd.Series[pd.CategoricalDtype]", pd.Series([1], dtype="category")
62-
).array,
58+
pd.Series([1], dtype="category").array,
6359
pd.Categorical,
6460
),
6561
pd.Categorical,

tests/series/test_series.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
Scalar,
5151
)
5252

53+
from pandas.core.dtypes.dtypes import CategoricalDtype # noqa F401
54+
5355
from tests import (
5456
PD_LTE_23,
5557
TYPE_CHECKING_INVALID_USAGE,
@@ -1819,6 +1821,10 @@ def test_categorical_codes():
18191821
cat = pd.Categorical(["a", "b", "a"])
18201822
check(assert_type(cat.codes, np_1darray[np.signedinteger]), np_1darray[np.int8])
18211823

1824+
# GH1383
1825+
sr = pd.Series([1], dtype="category")
1826+
check(assert_type(sr, "pd.Series[CategoricalDtype]"), pd.Series, np.integer)
1827+
18221828

18231829
def test_relops() -> None:
18241830
# GH 175
@@ -2908,8 +2914,6 @@ def test_astype_categorical(cast_arg: CategoryDtypeArg, target_type: type) -> No
29082914
# pandas category
29092915
assert_type(s.astype(pd.CategoricalDtype()), "pd.Series[pd.CategoricalDtype]")
29102916
assert_type(s.astype(cast_arg), "pd.Series[pd.CategoricalDtype]")
2911-
# pyarrow dictionary
2912-
# assert_type(s.astype("dictionary[pyarrow]"), "pd.Series[Categorical]")
29132917

29142918

29152919
@pytest.mark.parametrize("cast_arg, target_type", ASTYPE_OBJECT_ARGS, ids=repr)

0 commit comments

Comments
 (0)