Skip to content

Commit e6b23da

Browse files
GH1383 Add support for creating Series/Index with dtype='category'
1 parent b984cba commit e6b23da

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from pandas.core.base import (
4242
NumListLike,
4343
_ListLike,
4444
)
45+
from pandas.core.indexes.category import CategoricalIndex
4546
from pandas.core.strings.accessor import StringMethods
4647
from typing_extensions import (
4748
Never,
@@ -58,6 +59,7 @@ from pandas._typing import (
5859
AnyAll,
5960
ArrayLike,
6061
AxesData,
62+
CategoryDtypeArg,
6163
DropKeep,
6264
Dtype,
6365
DtypeArg,
@@ -229,6 +231,16 @@ class Index(IndexOpsMixin[S1]):
229231
tupleize_cols: bool = ...,
230232
) -> TimedeltaIndex: ...
231233
@overload
234+
def __new__(
235+
cls,
236+
data: AxesData,
237+
*,
238+
dtype: CategoryDtypeArg,
239+
copy: bool = ...,
240+
name: Hashable = ...,
241+
tupleize_cols: bool = ...,
242+
) -> CategoricalIndex: ...
243+
@overload
232244
def __new__(
233245
cls,
234246
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
@@ -353,6 +353,16 @@ class Series(IndexOpsMixin[S1], NDFrame):
353353
copy: bool = ...,
354354
) -> Series[Timestamp]: ...
355355
@overload
356+
def __new__(
357+
cls,
358+
data: _ListLike,
359+
index: AxesData | None = ...,
360+
*,
361+
dtype: CategoryDtypeArg,
362+
name: Hashable = ...,
363+
copy: bool = ...,
364+
) -> Series[CategoricalDtype]: ...
365+
@overload
356366
def __new__(
357367
cls,
358368
data: PeriodIndex | Sequence[Period],

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ scipy = { version = ">=1.9.1", python = "<3.14" }
6565
scipy-stubs = ">=1.15.3.0"
6666
SQLAlchemy = ">=2.0.39"
6767
types-python-dateutil = ">=2.8.19"
68-
beautifulsoup4 = "<=4.13.5"
68+
beautifulsoup4 = ">=4.13.5"
6969
html5lib = ">=1.1"
7070
python-calamine = ">=0.2.0"
7171

tests/indexes/test_indexes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas.core.arrays import DatetimeArray
1616
from pandas.core.arrays.categorical import Categorical
1717
from pandas.core.indexes.base import Index
18+
from pandas.core.indexes.category import CategoricalIndex
1819
from typing_extensions import (
1920
Never,
2021
assert_type,
@@ -1366,6 +1367,12 @@ def test_index_factorize() -> None:
13661367
check(assert_type(idx_uniques, np_1darray | Index | Categorical), pd.Index)
13671368

13681369

1370+
def test_index_categorical() -> None:
1371+
"""Test creating an index with Categorical type GH1383."""
1372+
sr = pd.Index([1], dtype="category")
1373+
check(assert_type(sr, CategoricalIndex), CategoricalIndex)
1374+
1375+
13691376
def test_disallow_empty_index() -> None:
13701377
# From GH 826
13711378
if TYPE_CHECKING_INVALID_USAGE:

tests/series/test_series.py

Lines changed: 6 additions & 0 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
54+
5355
from tests import (
5456
PD_LTE_23,
5557
TYPE_CHECKING_INVALID_USAGE,
@@ -1824,6 +1826,10 @@ def test_categorical_codes():
18241826
cat = pd.Categorical(["a", "b", "a"])
18251827
check(assert_type(cat.codes, np_1darray[np.signedinteger]), np_1darray[np.int8])
18261828

1829+
# GH1383
1830+
sr = pd.Series([1], dtype="category")
1831+
check(assert_type(sr, "pd.Series[CategoricalDtype]"), pd.Series, np.integer)
1832+
18271833

18281834
def test_relops() -> None:
18291835
# GH 175

0 commit comments

Comments
 (0)