@@ -605,11 +605,11 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
605
605
@classmethod
606
606
def from_codes (cls , codes , categories = None , ordered = None , dtype = None ):
607
607
"""
608
- Make a Categorical type from codes and categories arrays .
608
+ Make a Categorical type from codes and categories or dtype .
609
609
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.
613
613
614
614
If your data does not follow this convention, please use the normal
615
615
constructor.
@@ -618,9 +618,12 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
618
618
----------
619
619
codes : array-like, integers
620
620
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
622
622
categories : index-like, optional
623
623
The categories for the categorical. Items need to be unique.
624
+ .. versionchanged:: 0.24.0
625
+
626
+ The `categories` parameter has been made optional.
624
627
ordered : bool, optional
625
628
Whether or not this categorical is treated as an ordered
626
629
categorical. If not given, the resulting categorical will be
@@ -630,8 +633,9 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
630
633
631
634
The default value has been changed to ``None``. Previously
632
635
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`.
635
639
636
640
.. versionadded:: 0.24.0
637
641
@@ -642,8 +646,9 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
642
646
[a, b, a, b]
643
647
Categories (2, object): [a < b]
644
648
"""
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 )
647
652
648
653
codes = np .asarray (codes ) # #21767
649
654
if not is_integer_dtype (codes ):
@@ -658,12 +663,6 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
658
663
if msg :
659
664
raise ValueError (msg )
660
665
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
-
667
666
if len (codes ) and (
668
667
codes .max () >= len (dtype .categories ) or codes .min () < - 1 ):
669
668
raise ValueError ("codes need to be between -1 and "
0 commit comments