Skip to content

Commit f1acc37

Browse files
committed
Merge branch 'main' into feature/cmp0xff/arithmetic-mul
2 parents b824be1 + a9f0bd3 commit f1acc37

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
@@ -43,6 +43,7 @@ from pandas.core.base import (
4343
NumListLike,
4444
_ListLike,
4545
)
46+
from pandas.core.indexes.category import CategoricalIndex
4647
from pandas.core.strings.accessor import StringMethods
4748
from typing_extensions import (
4849
Never,
@@ -62,6 +63,7 @@ from pandas._typing import (
6263
AnyAll,
6364
ArrayLike,
6465
AxesData,
66+
CategoryDtypeArg,
6567
DropKeep,
6668
Dtype,
6769
DtypeArg,
@@ -235,6 +237,16 @@ class Index(IndexOpsMixin[S1]):
235237
tupleize_cols: bool = ...,
236238
) -> TimedeltaIndex: ...
237239
@overload
240+
def __new__(
241+
cls,
242+
data: AxesData,
243+
*,
244+
dtype: CategoryDtypeArg,
245+
copy: bool = ...,
246+
name: Hashable = ...,
247+
tupleize_cols: bool = ...,
248+
) -> CategoricalIndex: ...
249+
@overload
238250
def __new__(
239251
cls,
240252
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
@@ -362,6 +362,16 @@ class Series(IndexOpsMixin[S1], NDFrame):
362362
copy: bool = ...,
363363
) -> Series[Timestamp]: ...
364364
@overload
365+
def __new__(
366+
cls,
367+
data: _ListLike,
368+
index: AxesData | None = ...,
369+
*,
370+
dtype: CategoryDtypeArg,
371+
name: Hashable = ...,
372+
copy: bool = ...,
373+
) -> Series[CategoricalDtype]: ...
374+
@overload
365375
def __new__(
366376
cls,
367377
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)