Skip to content

Commit b76f27e

Browse files
committed
Changes
1 parent 0a3b2f7 commit b76f27e

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

pandas/_typing.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import IO, TYPE_CHECKING, AnyStr, TypeVar, Union
2+
from typing import IO, TYPE_CHECKING, AnyStr, Optional, TypeVar, Union
33

44
import numpy as np
55

@@ -27,6 +27,4 @@
2727
FrameOrSeries = TypeVar("FrameOrSeries", "Series", "DataFrame")
2828
Scalar = Union[str, int, float]
2929
Axis = Union[str, int]
30-
31-
# TODO(GH26403): Replace with Optional[bool] or bool
32-
OrderedType = Union[None, bool, object]
30+
Ordered = Optional[bool]

pandas/core/arrays/categorical.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from pandas.core.dtypes.inference import is_hashable
4949
from pandas.core.dtypes.missing import isna, notna
5050

51-
from pandas._typing import ArrayLike, Dtype, OrderedType
51+
from pandas._typing import ArrayLike, Dtype, Ordered
5252
from pandas.core import ops
5353
from pandas.core.accessor import PandasDelegate, delegate_names
5454
import pandas.core.algorithms as algorithms
@@ -475,7 +475,7 @@ def categories(self, categories):
475475
self._dtype = new_dtype
476476

477477
@property
478-
def ordered(self) -> OrderedType:
478+
def ordered(self) -> Ordered:
479479
"""
480480
Whether the categories have an ordered relationship.
481481
"""
@@ -716,8 +716,6 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
716716

717717
return cls(codes, dtype=dtype, fastpath=True)
718718

719-
_codes = None
720-
721719
def _get_codes(self):
722720
"""
723721
Get the codes.

pandas/core/dtypes/dtypes.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

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

14-
from pandas._typing import OrderedType
14+
from pandas._typing import Ordered
1515

1616
from .base import ExtensionDtype
1717
from .inference import is_bool, is_list_like
@@ -221,7 +221,11 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
221221
_metadata = ("categories", "ordered", "_ordered_from_sentinel")
222222
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
223223

224-
def __init__(self, categories=None, ordered: OrderedType = ordered_sentinel):
224+
def __init__(
225+
self, categories=None, ordered: Union[Ordered, object] = ordered_sentinel
226+
):
227+
# TODO(GH26403): Set type of ordered to Ordered
228+
ordered = cast(Ordered, ordered)
225229
self._finalize(categories, ordered, fastpath=False)
226230

227231
@classmethod
@@ -234,7 +238,7 @@ def _from_fastpath(
234238

235239
@classmethod
236240
def _from_categorical_dtype(
237-
cls, dtype: "CategoricalDtype", categories=None, ordered: OrderedType = None
241+
cls, dtype: "CategoricalDtype", categories=None, ordered: Ordered = None
238242
) -> "CategoricalDtype":
239243
if categories is ordered is None:
240244
return dtype
@@ -335,9 +339,7 @@ def _from_values_or_dtype(
335339

336340
return dtype
337341

338-
def _finalize(
339-
self, categories, ordered: OrderedType, fastpath: bool = False
340-
) -> None:
342+
def _finalize(self, categories, ordered: Ordered, fastpath: bool = False) -> None:
341343

342344
if ordered is not None and ordered is not ordered_sentinel:
343345
self.validate_ordered(ordered)
@@ -422,7 +424,7 @@ def __repr__(self):
422424
return tpl.format(data, self._ordered)
423425

424426
@staticmethod
425-
def _hash_categories(categories, ordered: OrderedType = True) -> int:
427+
def _hash_categories(categories, ordered: Ordered = True) -> int:
426428
from pandas.core.util.hashing import (
427429
hash_array,
428430
_combine_hash_arrays,
@@ -474,7 +476,7 @@ def construct_array_type(cls):
474476
return Categorical
475477

476478
@staticmethod
477-
def validate_ordered(ordered: OrderedType) -> None:
479+
def validate_ordered(ordered: Ordered) -> None:
478480
"""
479481
Validates that we have a valid ordered parameter. If
480482
it is not a boolean, a TypeError will be raised.
@@ -552,8 +554,9 @@ def update_dtype(
552554
"got {dtype!r}"
553555
).format(dtype=dtype)
554556
raise ValueError(msg)
555-
556-
dtype = cast(CategoricalDtype, dtype)
557+
else:
558+
# from here on, dtype is a CategoricalDtype
559+
dtype = cast(CategoricalDtype, dtype)
557560

558561
# dtype is CDT: keep current categories/ordered if None
559562
new_categories = dtype.categories
@@ -586,7 +589,7 @@ def categories(self):
586589
return self._categories
587590

588591
@property
589-
def ordered(self) -> OrderedType:
592+
def ordered(self) -> Ordered:
590593
"""
591594
Whether the categories have an ordered relationship.
592595
"""

0 commit comments

Comments
 (0)