Skip to content

Commit 7002235

Browse files
committed
clean-up Categorical.from_codes
1 parent fccb54d commit 7002235

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

pandas/core/arrays/categorical.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,11 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
605605
@classmethod
606606
def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
607607
"""
608-
Make a Categorical type from codes and categories arrays.
608+
Make a Categorical type from codes and categories or dtype.
609609
610-
This constructor is useful if you already have codes and categories and
611-
so do not need the (computation intensive) factorization step, which is
612-
usually done on the constructor.
610+
This constructor is useful if you already have codes and
611+
categories/dtype and so do not need the (computation intensive)
612+
factorization step, which is usually done on the constructor.
613613
614614
If your data does not follow this convention, please use the normal
615615
constructor.
@@ -618,9 +618,12 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
618618
----------
619619
codes : array-like, integers
620620
An integer array, where each integer points to a category in
621-
categories or -1 for NaN
621+
categories or dtype.categories, or else is -1 for NaN
622622
categories : index-like, optional
623623
The categories for the categorical. Items need to be unique.
624+
.. versionchanged:: 0.24.0
625+
626+
The `categories` parameter has been made optional.
624627
ordered : bool, optional
625628
Whether or not this categorical is treated as an ordered
626629
categorical. If not given, the resulting categorical will be
@@ -630,8 +633,9 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
630633
631634
The default value has been changed to ``None``. Previously
632635
the default value was ``False``.
633-
dtype : CategoricalDtype, optional
634-
An instance of ``CategoricalDtype`` to use for this categorical.
636+
dtype : CategoricalDtype or the string "category", optional
637+
If :class:`CategoricalDtype`, cannot be used together with
638+
`categories` or `ordered`.
635639
636640
.. versionadded:: 0.24.0
637641
@@ -642,8 +646,9 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
642646
[a, b, a, b]
643647
Categories (2, object): [a < b]
644648
"""
645-
dtype = CategoricalDtype._from_values_or_dtype(codes, categories,
646-
ordered, dtype)
649+
dtype = CategoricalDtype._from_values_or_dtype(categories=categories,
650+
ordered=ordered,
651+
dtype=dtype)
647652

648653
codes = np.asarray(codes) # #21767
649654
if not is_integer_dtype(codes):
@@ -658,12 +663,6 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
658663
if msg:
659664
raise ValueError(msg)
660665

661-
try:
662-
codes = coerce_indexer_dtype(codes, dtype.categories)
663-
except (ValueError, TypeError):
664-
raise ValueError(
665-
"codes need to be convertible to an arrays of integers")
666-
667666
if len(codes) and (
668667
codes.max() >= len(dtype.categories) or codes.min() < -1):
669668
raise ValueError("codes need to be between -1 and "

0 commit comments

Comments
 (0)