o
    5c%                     @  sh   d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZ er(d dlmZ dddZdddZdS )    )annotations)TYPE_CHECKINGN)unique1d)CategoricalCategoricalDtyperecode_for_categories)CategoricalIndexcr   sortboolobservedreturn&tuple[Categorical, Categorical | None]c           	      C  s   |r3t | j}||dk }| jrt|}| j|}t| j| j|}t|| jd}t	||dd| fS |r9| dfS | 
 }|j|jdk }|jrMt|}||j|}|| j| 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
    )orderedT)dtypefastpathN)r   codesr   npr
   
categoriestaker   r   r   uniqueset_categoriesadd_categoriesisinreorder_categories)	r	   r
   r   unique_codes
take_codesr   r   r   cat r   U/var/www/html/gps/gps/lib/python3.10/site-packages/pandas/core/groupby/categorical.pyrecode_for_groupby   s$   $


r!   cir   c                 C  s0   |r| | jS | j| j|j  }||S )a;  
    Reverse the codes_to_groupby to account for sort / observed.

    Parameters
    ----------
    c : Categorical
    sort : bool
        The value of the sort parameter groupby was called with.
    ci : CategoricalIndex
        The codes / categories to recode

    Returns
    -------
    CategoricalIndex
    )r   r   r   r   )r	   r
   r"   new_catsr   r   r    recode_from_groupby_   s   
r$   )r	   r   r
   r   r   r   r   r   )r	   r   r
   r   r"   r   r   r   )
__future__r   typingr   numpyr   pandas.core.algorithmsr   pandas.core.arrays.categoricalr   r   r   pandas.core.indexes.apir   r!   r$   r   r   r   r    <module>   s    
M