
    h                    P    d dl mZ d dlZd dlmZ d dlmZmZm	Z	 	 	 	 	 	 	 	 	 ddZ
y)    )annotationsN)unique1d)CategoricalCategoricalDtyperecode_for_categoriesc                    |rt        | j                        }||dk7     }|rt        j                  |      }| j                  j                  |      }t        | j                  | j                  |      }t        || j                        }t        j                  ||      | fS |r| dfS t        j                  | j                  j                               }t        | j                  | j                  dk7           }	|rt        j                  |	      }	t        |      t        |	      kD  r0t        j                  ||	d      }
t        j                  |	|
f      }n|	}t        | | j!                         j                  j                  |            dfS )a=  
    Code the categories to ensure we can groupby for categoricals.

    If observed=True, we return a new Categorical with the observed
    categories only.

    If sort=False, return a copy of self, coded with categories as
    returned by .unique(), followed by any categories not appearing in
    the data. If sort=True, return self.

    This method is needed solely to ensure the categorical index of the
    GroupBy result has categories in the order of appearance in the data
    (GH-8868).

    Parameters
    ----------
    c : Categorical
    sort : bool
        The value of the sort parameter groupby was called with.
    observed : bool
        Account only for the observed values

    Returns
    -------
    Categorical
        If sort=False, the new categories are set to the order of
        appearance in codes (unless ordered=True, in which case the
        original order is preserved), followed by any unrepresented
        categories in the original order.
    Categorical or None
        If we are observed, return the original categorical, otherwise None
    )ordered)dtypeNT)assume_unique)r   codesnpsort
categoriestaker   r   r
   r   _simple_newarangenuniquelen	setdiff1dconcatenateunique)cr   observedunique_codes
take_codesr   r   r   	all_codesunique_notnan_codesmissing_codess              DD:\jyotish\venv\Lib\site-packages\pandas/core/groupby/categorical.pyrecode_for_groupbyr!      sP   H   (!,""45
,J \\&&z2
%aggq||ZH !QYY?&&uE:A== $w
 		!,,..01I"177177b=#9: gg&9:
9~/00Y0CSWX^^%8-$HI
(
q!((*//44Z@A4GG    )r   r   r   boolr   r#   returnz&tuple[Categorical, Categorical | None])
__future__r   numpyr   pandas.core.algorithmsr   pandas.core.arrays.categoricalr   r   r   r!    r"   r    <module>r*      sG    "  + JHJHJH*.JH+JHr"   