Skip to content

Commit 5a6de11

Browse files
committed
Changes
1 parent 0a3b2f7 commit 5a6de11

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

Diff for: 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, TypeVar, Union, Optional
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]

Diff for: 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.

Diff for: pandas/core/dtypes/dtypes.py

+14-9
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__(self,
225+
categories=None,
226+
ordered: Union[Ordered, object] = ordered_sentinel):
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
@@ -336,7 +340,7 @@ def _from_values_or_dtype(
336340
return dtype
337341

338342
def _finalize(
339-
self, categories, ordered: OrderedType, fastpath: bool = False
343+
self, categories, ordered: Ordered, fastpath: bool = False
340344
) -> None:
341345

342346
if ordered is not None and ordered is not ordered_sentinel:
@@ -422,7 +426,7 @@ def __repr__(self):
422426
return tpl.format(data, self._ordered)
423427

424428
@staticmethod
425-
def _hash_categories(categories, ordered: OrderedType = True) -> int:
429+
def _hash_categories(categories, ordered: Ordered = True) -> int:
426430
from pandas.core.util.hashing import (
427431
hash_array,
428432
_combine_hash_arrays,
@@ -474,7 +478,7 @@ def construct_array_type(cls):
474478
return Categorical
475479

476480
@staticmethod
477-
def validate_ordered(ordered: OrderedType) -> None:
481+
def validate_ordered(ordered: Ordered) -> None:
478482
"""
479483
Validates that we have a valid ordered parameter. If
480484
it is not a boolean, a TypeError will be raised.
@@ -552,8 +556,9 @@ def update_dtype(
552556
"got {dtype!r}"
553557
).format(dtype=dtype)
554558
raise ValueError(msg)
555-
556-
dtype = cast(CategoricalDtype, dtype)
559+
else:
560+
# from here on, dtype is a CategoricalDtype
561+
dtype = cast(CategoricalDtype, dtype)
557562

558563
# dtype is CDT: keep current categories/ordered if None
559564
new_categories = dtype.categories
@@ -586,7 +591,7 @@ def categories(self):
586591
return self._categories
587592

588593
@property
589-
def ordered(self) -> OrderedType:
594+
def ordered(self) -> Ordered:
590595
"""
591596
Whether the categories have an ordered relationship.
592597
"""

0 commit comments

Comments
 (0)