Skip to content

Commit c1342d0

Browse files
committed
TYPIMG: Partial typing of Categorical
1 parent c64c9cb commit c1342d0

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

pandas/_typing.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pandas._libs.tslibs.period import Period
88
from pandas._libs.tslibs.timedeltas import Timedelta
99

10-
from pandas.core.dtypes.dtypes import ExtensionDtype
10+
from pandas.core.dtypes import dtypes
1111
from pandas.core.dtypes.generic import (
1212
ABCDataFrame,
1313
ABCExtensionArray,
@@ -26,8 +26,11 @@
2626
)
2727
ArrayLike = TypeVar("ArrayLike", ABCExtensionArray, np.ndarray)
2828
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", Period, Timestamp, Timedelta)
29-
Dtype = Union[str, np.dtype, ExtensionDtype]
29+
Dtype = Union[str, np.dtype, "dtypes.ExtensionDtype"]
3030
FilePathOrBuffer = Union[str, Path, IO[AnyStr]]
3131

3232
FrameOrSeries = TypeVar("FrameOrSeries", ABCSeries, ABCDataFrame)
3333
Scalar = Union[str, int, float]
34+
35+
# TODO(GH26403): Replace with Optional[bool] or bool
36+
OrderedType = Union[None, bool, object]

pandas/core/arrays/categorical.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from shutil import get_terminal_size
22
import textwrap
3+
from typing import Type, Union, cast
34
from warnings import warn
45

56
import numpy as np
@@ -47,6 +48,7 @@
4748
from pandas.core.dtypes.inference import is_hashable
4849
from pandas.core.dtypes.missing import isna, notna
4950

51+
from pandas._typing import ArrayLike, Dtype, OrderedType
5052
from pandas.core import ops
5153
from pandas.core.accessor import PandasDelegate, delegate_names
5254
import pandas.core.algorithms as algorithms
@@ -466,7 +468,7 @@ def categories(self, categories):
466468
self._dtype = new_dtype
467469

468470
@property
469-
def ordered(self):
471+
def ordered(self) -> OrderedType:
470472
"""
471473
Whether the categories have an ordered relationship.
472474
"""
@@ -480,11 +482,11 @@ def dtype(self) -> CategoricalDtype:
480482
return self._dtype
481483

482484
@property
483-
def _ndarray_values(self):
485+
def _ndarray_values(self) -> np.ndarray:
484486
return self.codes
485487

486488
@property
487-
def _constructor(self):
489+
def _constructor(self) -> Type["Categorical"]:
488490
return Categorical
489491

490492
@classmethod
@@ -495,15 +497,15 @@ def _formatter(self, boxed=False):
495497
# Defer to CategoricalFormatter's formatter.
496498
return None
497499

498-
def copy(self):
500+
def copy(self) -> "Categorical":
499501
"""
500502
Copy constructor.
501503
"""
502504
return self._constructor(
503505
values=self._codes.copy(), dtype=self.dtype, fastpath=True
504506
)
505507

506-
def astype(self, dtype, copy=True):
508+
def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
507509
"""
508510
Coerce this type to another dtype
509511
@@ -519,6 +521,8 @@ def astype(self, dtype, copy=True):
519521
520522
"""
521523
if is_categorical_dtype(dtype):
524+
dtype = cast(Union[str, CategoricalDtype], dtype)
525+
522526
# GH 10696/18593
523527
dtype = self.dtype.update_dtype(dtype)
524528
self = self.copy() if copy else self
@@ -528,27 +532,27 @@ def astype(self, dtype, copy=True):
528532
return np.array(self, dtype=dtype, copy=copy)
529533

530534
@cache_readonly
531-
def ndim(self):
535+
def ndim(self) -> int:
532536
"""
533537
Number of dimensions of the Categorical
534538
"""
535539
return self._codes.ndim
536540

537541
@cache_readonly
538-
def size(self):
542+
def size(self) -> int:
539543
"""
540544
return the len of myself
541545
"""
542546
return len(self)
543547

544548
@cache_readonly
545-
def itemsize(self):
549+
def itemsize(self) -> int:
546550
"""
547551
return the size of a single category
548552
"""
549553
return self.categories.itemsize
550554

551-
def tolist(self):
555+
def tolist(self) -> list:
552556
"""
553557
Return a list of the values.
554558
@@ -561,7 +565,7 @@ def tolist(self):
561565
to_list = tolist
562566

563567
@property
564-
def base(self):
568+
def base(self) -> None:
565569
"""
566570
compat, we are always our own object
567571
"""
@@ -769,7 +773,7 @@ def _set_categories(self, categories, fastpath=False):
769773

770774
self._dtype = new_dtype
771775

772-
def _set_dtype(self, dtype):
776+
def _set_dtype(self, dtype: CategoricalDtype) -> "Categorical":
773777
"""
774778
Internal method for directly updating the CategoricalDtype
775779

pandas/core/dtypes/dtypes.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" define extension dtypes """
22
import re
3-
from typing import Any, Dict, List, Optional, Tuple, Type, Union
3+
from typing import Any, Dict, List, Optional, Tuple, Type, Union, cast
44
import warnings
55

66
import numpy as np
@@ -11,6 +11,8 @@
1111

1212
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCDateOffset, ABCIndexClass
1313

14+
from pandas._typing import OrderedType
15+
1416
from .base import ExtensionDtype
1517
from .inference import is_list_like
1618

@@ -20,9 +22,6 @@
2022
# CategoricalDtype constructor to detect when ordered=None is explicitly passed
2123
ordered_sentinel = object() # type: object
2224

23-
# TODO(GH26403): Replace with Optional[bool] or bool
24-
OrderedType = Union[None, bool, object]
25-
2625

2726
def register_extension_dtype(cls: Type[ExtensionDtype],) -> Type[ExtensionDtype]:
2827
"""
@@ -524,7 +523,9 @@ def validate_categories(categories, fastpath: bool = False):
524523

525524
return categories
526525

527-
def update_dtype(self, dtype: "CategoricalDtype") -> "CategoricalDtype":
526+
def update_dtype(
527+
self, dtype: Union[str_type, "CategoricalDtype"]
528+
) -> "CategoricalDtype":
528529
"""
529530
Returns a CategoricalDtype with categories and ordered taken from dtype
530531
if specified, otherwise falling back to self if unspecified
@@ -547,6 +548,8 @@ def update_dtype(self, dtype: "CategoricalDtype") -> "CategoricalDtype":
547548
).format(dtype=dtype)
548549
raise ValueError(msg)
549550

551+
dtype = cast(CategoricalDtype, dtype)
552+
550553
# dtype is CDT: keep current categories/ordered if None
551554
new_categories = dtype.categories
552555
if new_categories is None:

0 commit comments

Comments
 (0)