Sindbad~EG File Manager

Current Path : /usr/local/lib/python3.12/site-packages/pandas/core/arrays/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.12/site-packages/pandas/core/arrays/__pycache__/categorical.cpython-312.pyc

�

Mٜg���	��ddlmZddlmZddlmZddlZddlmZddl	m
Z
mZmZm
Z
ddlZddlZddlmZddlmZmZmZdd	lmZdd
lmZddlmZddlm Z dd
l!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4ddl5m6Z6m7Z7ddl8m9Z9m:Z:ddl;m<Z<m=Z=m>Z>ddl?m@Z@mAZAddlBmCZCmDZDddlEmFZFmGZGddlHmIZImJZJmKZKddlLmMcmNZOddlPmQZQmRZRddlSmTZTddlUmVZVddlWmXZXddlYmZZZe
r.ddl[m\Z\m]Z]m^Z^ddl_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjddlkmlZlmmZmmnZnd�Zod/d �ZpGd!�d"eFeKeX�ZqeAeqd#d$gd%�&�eAeqgd'�d(�&�Gd)�d*e@eKeJ���Zr						d0d+�Zs	d1					d2d,�Ztd3d-�Zud4d.�Zvy)5�)�annotations)�QUOTE_NONNUMERIC)�partialN)�get_terminal_size)�
TYPE_CHECKING�Literal�cast�overload)�
get_option)�NaT�algos�lib)�
NDArrayBacked)�function)�find_stack_level)�validate_bool_kwarg)�coerce_indexer_dtype�find_common_type)�ensure_int64�ensure_platform_int�is_any_real_numeric_dtype�
is_bool_dtype�is_dict_like�is_hashable�is_integer_dtype�is_list_like�	is_scalar�needs_i8_conversion�pandas_dtype)�
ArrowDtype�CategoricalDtype�CategoricalDtypeType�ExtensionDtype)�ABCIndex�	ABCSeries)�is_valid_na_for_dtype�isna)�
algorithms�	arraylike�ops)�PandasDelegate�delegate_names)�	factorize�take_nd)�NDArrayBackedExtensionArray�ravel_compat)�ExtensionArray�NoNewAttributesMixin�PandasObject)�
extract_array�sanitize_array)�unpack_zerodim_and_defer)�nargsort)�ObjectStringArrayMixin)�console)�Hashable�Iterator�Sequence)�	ArrayLike�	AstypeArg�AxisInt�Dtype�DtypeObj�NpDtype�Ordered�Self�Shape�SortKind�npt)�	DataFrame�Index�Seriesc�����d�j�d���tju�t�����fd��}�|_|S)N�__c�2��t|�}t|�r$t|�t|�k7r
|std��|js�
dvrtd��t
|t�r�d}|j|�st|��|jsS|jj|j�s.t|j|j|jd��}n|j}�	|j|�}|jdk(|dk(z}|j�r�||<|S|rc||jvr>|j|�}�	|j|�}�
dvr|jdk(}�||<|St!j"||�	�S�
d	vrtd
�
�dt%|��d���t
|t&�rt)|j*�r	�	||�St-t/j0|��
�t/j0|��S)
NzLengths must match.)�__lt__�__gt__�__le__�__ge__z7Unordered Categoricals can only compare equality or notz?Categoricals can only be compared if 'categories' are the same.F��copy���>�__eq__rQrO)rU�__ne__z$Cannot compare a Categorical for op z with type zB.
If you want to compare values, use 'np.asarray(cat) <op> other'.)rr�len�
ValueError�ordered�	TypeError�
isinstance�Categorical�#_categories_match_up_to_permutation�
categories�equals�recode_for_categories�codes�_codes�any�
_unbox_scalarr*�invalid_comparison�typer1r�dtype�getattr�np�array)�self�other�hashable�msg�other_codes�ret�mask�i�
fill_value�op�opnames        ����I/usr/local/lib/python3.12/site-packages/pandas/core/arrays/categorical.py�funcz_cat_compare_op.<locals>.func~s�����u�%�����3�u�:��T��#:�8��2�3�3��|�|��A�A��M����e�[�)�T�C��;�;�E�B���n�$��<�<����(>�(>�u�?O�?O�(P�3��K�K��!1�!1�4�?�?����$�l�l���T�[�[�+�.�C��K�K�2�%�+��*;�<�D��x�x�z�&��D�	��J������'��&�&�u�-������a�(���!?�?� �;�;�"�,�D� *�C��I��
��-�-�d�E�2�>�>��1�1��:�6�(�C� ��K�=�)8�8����%��0�5H����5U��%���&�2�7�2�8�8�D�>�6�2�2�8�8�E�?�C�C�)�__name__�operator�ner6)rtrwrsrus` @@rv�_cat_compare_opr|zsK����"�+�+��b�
!�F��x�{�{�"�J��f�%�=D�&�=D�~�D�M��Krxc����t|�	|jj|�}t|�r|�vSt
�fd�|D��S#ttf$rYywxYw)aO
    Helper for membership check for ``key`` in ``cat``.

    This is a helper method for :method:`__contains__`
    and :class:`CategoricalIndex.__contains__`.

    Returns True if ``key`` is in ``cat.categories`` and the
    location of ``key`` in ``categories`` is in ``container``.

    Parameters
    ----------
    cat : :class:`Categorical`or :class:`categoricalIndex`
    key : a hashable object
        The key to check membership for.
    container : Container (e.g. list-like or mapping)
        The container to check for membership in.

    Returns
    -------
    is_in : bool
        True if ``key`` is in ``self.categories`` and location of
        ``key`` in ``categories`` is in ``container``, else False.

    Notes
    -----
    This method does not check for NaN values. Do that separately
    before calling this method.
    Fc3�&�K�|]}|�v���
y�w�N�)�.0�loc_�	containers  �rv�	<genexpr>zcontains.<locals>.<genexpr>�s�����5���4�9�$��s�)�hashr^�get_loc�KeyErrorrZrrc)�cat�keyr��locs  ` rv�containsr��se���:	��I�
��n�n�$�$�S�)����~��i����5��5�5�5��
�i� ����s�A�A�Ac�8��eZdZUdZdZejedg�zZdZde	d<e
						df�fd��Zdddejd	f							dg�fd
�
Zedhd��Zedid��Ze
dd
d�					djd��Ze
dkd��Zedldmd��Zedldnd��Zedldod��Zdpdo�fd�
Zd�Ze
	dq	drd��Ze
				ds					dtd��Zedud��Zedvd��Zedwd��Zdxdy�fd�
Zdzd�Zd{d�Zdrd�Zdrd �Z d|d}d!�Z!drd"�Z"dqdrd#�Z#drd$�Z$drd%�Z%drd&�Z&ejf	d~d'�Z'e(e)jT�Z+e(e)jX�Z-e(e)j\�Z/e(e)j`�Z1e(e)jd�Z3e(e)jh�Z5d(�Z6d)�Z7e
dd*��Z8e9	d�					d�d+��Z:d�d,�Z;d��fd-�Z<edid.��Z=dxd�d/�Z>d�d0�Z?e?Z@d�d1�ZAeAZBdpd�d2�ZCe
						d�d3��ZDd�d4�ZEd�d5�ZFd	d6d7�			d��fd8�ZGedddd9�							d�d:��ZHeddd;�							d�d<��ZHd
d	d=d9�							d�d>�ZHd?d@dAd	d
dB�									d�dC�ZIdwdD�ZJ								d�dE�ZKedwdF��ZLd�dG�ZMdidH�ZNd�dI�ZOd�dJ�ZPdxd�dK�ZQd�dL�ZRd�dM�ZSd�dN�ZTd�dO�ZUdP�ZVd�dQ�ZWd	d
dR�					d��fdS�ZXd	dT�d�dU�ZYd	dT�d�dV�ZZdpd�dW�Z[dr�fdX�Z\d�dY�Z]d�d[�Z^e
d�d�d\��Z_d�d]�Z`d�d^�Zad�d_�Zbd�d`�Zcd
da�d�db�Zdeej�eej dZ�d	f	d�dc�Zgd�d�dd�Zh										d�de�Zi�xZjS)�r\aZ

    Represent a categorical variable in classic R / S-plus fashion.

    `Categoricals` can only take on a limited, and usually fixed, number
    of possible values (`categories`). In contrast to statistical categorical
    variables, a `Categorical` might have an order, but numerical operations
    (additions, divisions, ...) are not possible.

    All values of the `Categorical` are either in `categories` or `np.nan`.
    Assigning values outside of `categories` will raise a `ValueError`. Order
    is defined by the order of the `categories`, not lexical order of the
    values.

    Parameters
    ----------
    values : list-like
        The values of the categorical. If categories are given, values not in
        categories will be replaced with NaN.
    categories : Index-like (unique), optional
        The unique categories for this categorical. If not given, the
        categories are assumed to be the unique values of `values` (sorted, if
        possible, otherwise in the order in which they appear).
    ordered : bool, default False
        Whether or not this categorical is treated as a ordered categorical.
        If True, the resulting categorical will be ordered.
        An ordered categorical respects, when sorted, the order of its
        `categories` attribute (which in turn is the `categories` argument, if
        provided).
    dtype : CategoricalDtype
        An instance of ``CategoricalDtype`` to use for this categorical.

    Attributes
    ----------
    categories : Index
        The categories of this categorical.
    codes : ndarray
        The codes (integer positions, which point to the categories) of this
        categorical, read only.
    ordered : bool
        Whether or not this Categorical is ordered.
    dtype : CategoricalDtype
        The instance of ``CategoricalDtype`` storing the ``categories``
        and ``ordered``.

    Methods
    -------
    from_codes
    __array__

    Raises
    ------
    ValueError
        If the categories do not validate.
    TypeError
        If an explicit ``ordered=True`` is given but no `categories` and the
        `values` are not sortable.

    See Also
    --------
    CategoricalDtype : Type for categorical data.
    CategoricalIndex : An Index with an underlying ``Categorical``.

    Notes
    -----
    See the `user guide
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html>`__
    for more.

    Examples
    --------
    >>> pd.Categorical([1, 2, 3, 1, 2, 3])
    [1, 2, 3, 1, 2, 3]
    Categories (3, int64): [1, 2, 3]

    >>> pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'])
    ['a', 'b', 'c', 'a', 'b', 'c']
    Categories (3, object): ['a', 'b', 'c']

    Missing values are not included as a category.

    >>> c = pd.Categorical([1, 2, 3, 1, 2, 3, np.nan])
    >>> c
    [1, 2, 3, 1, 2, 3, NaN]
    Categories (3, int64): [1, 2, 3]

    However, their presence is indicated in the `codes` attribute
    by code `-1`.

    >>> c.codes
    array([ 0,  1,  2,  0,  1,  2, -1], dtype=int8)

    Ordered `Categoricals` can be sorted according to the custom order
    of the categories and can have a min and max value.

    >>> c = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'], ordered=True,
    ...                    categories=['c', 'b', 'a'])
    >>> c
    ['a', 'b', 'c', 'a', 'b', 'c']
    Categories (3, object): ['c' < 'b' < 'a']
    >>> c.min()
    'c'
    i��tolist�categoricalr!�_dtypec���t||j�}td��j|�}t�|�||�S)NF�rY)rr^r!�update_dtype�super�_simple_new)�clsrarg�	__class__s   �rvr�zCategorical._simple_newgs>���%�U�E�,<�,<�=�� ��/�<�<�U�C���w�"�5�%�0�0rxNTc�
	��|tjur%tjdtt���nd}t
j||||�}|rBt||j�}t
d��j|�}t�|�1||�yt|�std��tj d�}t#|dd�}	t%|	t�r/|j��Nt
|j|j&�}�n,t%|t(t*t,f��st/j0|�}t%|t2�r*t5|�dk(rtj gt6��}n�t%|tj8�r'|j:d	kDrt=d
��t?|d�}n�t?|d�}
tA|
�}|jC�rWtjD|�dD�cgc]}||��	}}|s|
jFdk(rd}
n|
jF}
t?|d|
��}
|
}|j��t%|jFtH�r�tK|jFjLtN�r|jPjS�}
|
jTjWtH��}|
jXj[�}t
||jFj\j&�}n�t%|t(�st?|d�}	t_|d
��\}}t
||j&�}nst%|jFt�rCta|�jb}te||jFj|j|��}ntg||j�}|jC�r4tjh|jj|jF��}|||<|}t
d��j|�}t||j�}
t�|�1|
|�ycc}w#t$r3}t_|d��\}}|j&rtd�|�Yd}~��Ld}~wwxYw)NzThe 'fastpath' keyword in Categorical is deprecated and will be removed in a future version. Use Categorical.from_codes instead��
stacklevelFr�z#Categorical input must be list-likergr�rg�z3> 1 ndim Categorical are not supported at this time�object)�types_mapperT��sortzl'values' is not ordered, please explicitly specify the categories order by passing in a categories argument.rR)6r�
no_default�warnings�warn�DeprecationWarningrr!�_from_values_or_dtyperr^r�r��__init__rrZrirjrhr[rYr$r%r1�com�convert_to_list_like�listrWr��ndarray�ndim�NotImplementedErrorr5r'rc�wherergr �
issubclassrfr"�	_pa_array�combine_chunks�
dictionary�	to_pandas�indices�to_numpy�
pyarrow_dtyper-r4rbr`�_get_codes_for_values�ones�shape)rk�valuesr^rYrg�fastpathrSra�	null_mask�vdtype�arr�idx�arr_list�sanitize_dtype�err�	old_codes�
full_codesr�s                 �rvr�zCategorical.__init__ss�����3�>�>�)��M�M�U�"�+�-�	
��H� �6�6��J���
���(���1A�1A�B�E�$�U�3�@�@��G�E��G��U�E�*���F�#��A�B�B��H�H�U�O�	����$�/���f�.�/����'�(��):�):�E�M�M�J���F�X�y�.�$I�J��-�-�f�5�F��&�$�'�C��K�1�,<����"�F�3���F�B�J�J�/��;�;��?�-�M���(���5��%�V�T�2�� ��I�	��=�=�?�8:�x�x��
�7K�A�7N�O�7N���s��7N�H�O� �3�9�9��#8�)-��),����(��4�~�N�C������#��&�,�,�
�3�
����!�!�#7�9��&�&�5�5�7�� �^�^�5�5�:�5�N�
����,�,�.��(��V�\�\�5O�5O�5W�5W�X��!�&�(�3�+�F�D�9�F�#�(1�&�t�(D�%�E�:�)��U�]�]�C��
����&6�
7�%�f�-�4�4�I�)��6�<�<�2�2�E�4D�4D�4��E�
*�&�%�2B�2B�C�E��=�=�?��'�'�)�/�/����E�E�J�%*�J�	�z�"��E� ��/�<�<�U�C��"�5�%�*:�*:�;��
����e�$��q P��2!�	#�(1�&�u�(E�%�E�:��}�}�(�C�� #�	#�%��	#�s�<Q�!Q�	R�(Q=�=Rc��|jS)an
        The :class:`~pandas.api.types.CategoricalDtype` for this instance.

        Examples
        --------
        >>> cat = pd.Categorical(['a', 'b'], ordered=True)
        >>> cat
        ['a', 'b']
        Categories (2, object): ['a' < 'b']
        >>> cat.dtype
        CategoricalDtype(categories=['a', 'b'], ordered=True, categories_dtype=object)
        )r��rks rvrgzCategorical.dtype�s���{�{�rxc�P�|jj}|jd�S�NrT)�_ndarrayrgrf)rkrgs  rv�_internal_fill_valuez Categorical._internal_fill_value�s!���
�
�#�#���z�z�"�~�rxF�rgrSc��||||��S)Nr�r�)r��scalarsrgrSs    rv�_from_sequencezCategorical._from_sequences���7�%�d�3�3rxc��|�t�|j||��}t|�}||j�k(j�st�|S)Nr�)r�r�r'�allrX)r�r�rg�resrqs     rv�
_from_scalarszCategorical._from_scalarssO���=�%�%�� � ��� �6���G�}������
�"�'�'�)����
rx.c��yrr��rkrgrSs   rv�astypezCategorical.astype���rxc��yrr�r�s   rvr�zCategorical.astyper�rxc��yrr�r�s   rvr�zCategorical.astype#r�rxc�6��t|�}|j|ur|r|j�}|S|}|St|t�rB|jj|�}|r|j�n|}|j
|�}|St|t�rt�|�%||��S|jdvr)|j�j�rtd��t|j�dk(st|j �dk(rt#j$|||��}|S|j j&}	|j||��}|j j(}t+||�sKt-j.t#j$|j j(�j|��}t3|t5|j6�|��}|S#t0tf$r(d|j j�d|��}t|��wxYw)	aZ
        Coerce this type to another dtype

        Parameters
        ----------
        dtype : numpy dtype or pandas type
        copy : bool, default True
            By default, astype always returns a newly allocated object.
            If copy is set to False and dtype is categorical, the original
            object is returned.
        rR�iuz#Cannot convert float NaN to integerrr�zCannot cast z
 dtype to �rs)rrgrSr[r!r��
_set_dtyper#r�r��kindr'rcrXrWrar^rirj�_values�	_na_valuer&r�item_from_zerodimrZr.rrb)rkrgrS�result�new_catsrsrnr�s       �rvr�zCategorical.astype's�����U�#���:�:���$(�T�Y�Y�[�F�V�
�W/3�F�V�
�S��/�
0��J�J�+�+�E�2�E�"&�4�9�9�;�D�D��_�_�U�+�F�J�
�G��~�
.��7�>�%�d�>�3�3�
�Z�Z�4�
�D�I�I�K�O�O�$5��B�C�C�
����_��
!�S����%9�Q�%>��X�X�����F�8�
�)���.�.�H�
&�#�?�?��T�?�B��!�_�_�6�6�
�,�Z��?�!$�!6�!6�������!:�!:�;�B�B�5�I�"�J���-�d�k�k�:�z��F��
�����
&�%�T�_�_�%:�%:�$;�:�e�W�M�� ��o�%�
&�s
�>BG!�!7Hc�j�tjdtt���|j	�S)z#
        Alias for tolist.
        zcCategorical.to_list is deprecated and will be removed in a future version. Use obj.tolist() insteadr�)r�r��
FutureWarningrr�r�s rv�to_listzCategorical.to_listbs,��
	�
�
�
0��'�)�		
��{�{�}�rxc��ddlm}m}m}m}||�}	t|t�xr|jdu}
|
r�t|jj�r||d��}	n�tj|jjd�r||d��}	nktj|jjd�r||d��}	n6t|jj�r|�gd�}|	j|�}	|
r|j}t||	|�}nV|	js;|	j!�}
|	j#�}t||
|�}t
|d	�
�}nt
|	d	�
�}|}|j%||��S)aA
        Construct a Categorical from inferred values.

        For inferred categories (`dtype` is None) the categories are sorted.
        For explicit `dtype`, the `inferred_categories` are cast to the
        appropriate type.

        Parameters
        ----------
        inferred_categories : Index
        inferred_codes : Index
        dtype : CategoricalDtype or 'category'
        true_values : list, optional
            If none are provided, the default ones are
            "True", "TRUE", and "true."

        Returns
        -------
        Categorical
        r)rI�to_datetime�
to_numeric�to_timedeltaN�coerce)�errors�M�m)�True�TRUE�trueFr�r�)�pandasrIr�r�r�r[r!r^rrgr�is_np_dtyper�isinr`�is_monotonic_increasingrS�sort_valuesr�)r��inferred_categories�inferred_codesrg�true_valuesrIr�r�r��cats�known_categoriesr^ra�unsorteds              rv�_from_inferred_categoriesz%Categorical._from_inferred_categoriesosX��0	
�	
��(�)���u�.�/�P�E�4D�4D�D�4P�	��(��)9�)9�)?�)?�@�!�"5�h�G������!1�!1�!7�!7��=�"�#6�x�H������!1�!1�!7�!7��=�#�$7��I���u�/�/�5�5�6��&�":�K��y�y��-����)�)�J�)�.�$�
�K�E��-�-��y�y�{�H��)�)�+�J�)�.�(�J�O�E�$�Z��?�E�$�T�5�9�E�"�E����u�E��2�2rxc��tj|||��}|j�
d}t|��|r|j	||��}|j||��S)a
        Make a Categorical type from codes and categories or dtype.

        This constructor is useful if you already have codes and
        categories/dtype and so do not need the (computation intensive)
        factorization step, which is usually done on the constructor.

        If your data does not follow this convention, please use the normal
        constructor.

        Parameters
        ----------
        codes : array-like of int
            An integer array, where each integer points to a category in
            categories or dtype.categories, or else is -1 for NaN.
        categories : index-like, optional
            The categories for the categorical. Items need to be unique.
            If the categories are not given here, then they must be provided
            in `dtype`.
        ordered : bool, optional
            Whether or not this categorical is treated as an ordered
            categorical. If not given here or in `dtype`, the resulting
            categorical will be unordered.
        dtype : CategoricalDtype or "category", optional
            If :class:`CategoricalDtype`, cannot be used together with
            `categories` or `ordered`.
        validate : bool, default True
            If True, validate that the codes are valid for the dtype.
            If False, don't validate that the codes are valid. Be careful about skipping
            validation, as invalid codes can lead to severe problems, such as segfaults.

            .. versionadded:: 2.1.0

        Returns
        -------
        Categorical

        Examples
        --------
        >>> dtype = pd.CategoricalDtype(['a', 'b'], ordered=True)
        >>> pd.Categorical.from_codes(codes=[0, 1, 0, 1], dtype=dtype)
        ['a', 'b', 'a', 'b']
        Categories (2, object): ['a' < 'b']
        )r^rYrgzKThe categories must be provided in 'categories' or 'dtype'. Both were None.r�)r!r�r^rX�_validate_codes_for_dtyper�)r�rar^rYrg�validaterns       rv�
from_codeszCategorical.from_codes�sk��j!�6�6�!�7�%�
�����#�+�
��S�/�!���1�1�%�u�1�E�E����u�E��2�2rxc�.�|jjS)a]
        The categories of this categorical.

        Setting assigns new values to each category (effectively a rename of
        each individual category).

        The assigned value has to be a list-like object. All items must be
        unique and the number of items in the new categories must be the same
        as the number of items in the old categories.

        Raises
        ------
        ValueError
            If the new categories do not validate as categories or if the
            number of new categories is unequal the number of old categories

        See Also
        --------
        rename_categories : Rename categories.
        reorder_categories : Reorder categories.
        add_categories : Add new categories.
        remove_categories : Remove the specified categories.
        remove_unused_categories : Remove categories which are not used.
        set_categories : Set the categories to the specified ones.

        Examples
        --------
        For :class:`pandas.Series`:

        >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
        >>> ser.cat.categories
        Index(['a', 'b', 'c'], dtype='object')

        >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], categories=['b', 'c', 'd'])
        >>> ser = pd.Series(raw_cat)
        >>> ser.cat.categories
        Index(['b', 'c', 'd'], dtype='object')

        For :class:`pandas.Categorical`:

        >>> cat = pd.Categorical(['a', 'b'], ordered=True)
        >>> cat.categories
        Index(['a', 'b'], dtype='object')

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'c', 'b', 'a', 'c', 'b'])
        >>> ci.categories
        Index(['a', 'b', 'c'], dtype='object')

        >>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a'])
        >>> ci.categories
        Index(['c', 'b', 'a'], dtype='object')
        )rgr^r�s rvr^zCategorical.categories�s��p�z�z�$�$�$rxc�.�|jjS)a{
        Whether the categories have an ordered relationship.

        Examples
        --------
        For :class:`pandas.Series`:

        >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
        >>> ser.cat.ordered
        False

        >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], ordered=True)
        >>> ser = pd.Series(raw_cat)
        >>> ser.cat.ordered
        True

        For :class:`pandas.Categorical`:

        >>> cat = pd.Categorical(['a', 'b'], ordered=True)
        >>> cat.ordered
        True

        >>> cat = pd.Categorical(['a', 'b'], ordered=False)
        >>> cat.ordered
        False

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'b'], ordered=True)
        >>> ci.ordered
        True

        >>> ci = pd.CategoricalIndex(['a', 'b'], ordered=False)
        >>> ci.ordered
        False
        )rgrYr�s rvrYzCategorical.ordered6s��L�z�z�!�!�!rxc�\�|jj�}d|j_|S)a�
        The category codes of this categorical index.

        Codes are an array of integers which are the positions of the actual
        values in the categories array.

        There is no setter, use the other categorical methods and the normal item
        setter to change values in the categorical.

        Returns
        -------
        ndarray[int]
            A non-writable view of the ``codes`` array.

        Examples
        --------
        For :class:`pandas.Categorical`:

        >>> cat = pd.Categorical(['a', 'b'], ordered=True)
        >>> cat.codes
        array([0, 1], dtype=int8)

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'])
        >>> ci.codes
        array([0, 1, 2, 0, 1, 2], dtype=int8)

        >>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a'])
        >>> ci.codes
        array([2, 0], dtype=int8)
        F)rb�view�flags�	writeable)rk�vs  rvrazCategorical.codes^s(��D
�K�K�����!������rxc�^��|r!tj||j�}nt||j��}|sV|jj�@t|j�t|jj�k7rt
d��t�|�!|j|�y)a�
        Sets new categories inplace

        Parameters
        ----------
        fastpath : bool, default False
           Don't perform validation of the categories for uniqueness or nulls

        Examples
        --------
        >>> c = pd.Categorical(['a', 'b'])
        >>> c
        ['a', 'b']
        Categories (2, object): ['a', 'b']

        >>> c._set_categories(pd.Index(['a', 'c']))
        >>> c
        ['a', 'c']
        Categories (2, object): ['a', 'c']
        r�NzKnew categories need to have the same number of items as the old categories!)
r!�_from_fastpathrYrgr^rWrXr�r�r�)rkr^r��	new_dtyper�s    �rv�_set_categorieszCategorical._set_categories�s����*�(�7�7�
�D�L�L�Q�I�(��T�\�\�J�I���
�
�%�%�1��I�(�(�)�S����1F�1F�-G�G��/��
�
	������	�2rxc��t|j|j|j�}t|�j	||��S)a+
        Internal method for directly updating the CategoricalDtype

        Parameters
        ----------
        dtype : CategoricalDtype

        Notes
        -----
        We don't do any validation here. It's assumed that the dtype is
        a (valid) instance of `CategoricalDtype`.
        r�)r`rar^rfr�)rkrgras   rvr�zCategorical._set_dtype�s:��&�d�j�j�$�/�/�5�CS�CS�T���D�z�%�%�e�5�%�9�9rxc��t|j|��}|j�}tj||j
|�|S)z�
        Set the ordered attribute to the boolean value.

        Parameters
        ----------
        value : bool
           Set whether this categorical is ordered (True) or not (False).
        r�)r!r^rSrr�r�)rk�valuerr�s    rv�set_orderedzCategorical.set_ordered�s:��%�T�_�_�e�D�	��i�i�k�����s�C�L�L�)�<��
rxc�$�|jd�S)ae
        Set the Categorical to be ordered.

        Returns
        -------
        Categorical
            Ordered Categorical.

        Examples
        --------
        For :class:`pandas.Series`:

        >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
        >>> ser.cat.ordered
        False
        >>> ser = ser.cat.as_ordered()
        >>> ser.cat.ordered
        True

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'])
        >>> ci.ordered
        False
        >>> ci = ci.as_ordered()
        >>> ci.ordered
        True
        T�r
r�s rv�
as_orderedzCategorical.as_ordered�s��:����%�%rxc�$�|jd�S)a�
        Set the Categorical to be unordered.

        Returns
        -------
        Categorical
            Unordered Categorical.

        Examples
        --------
        For :class:`pandas.Series`:

        >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], ordered=True)
        >>> ser = pd.Series(raw_cat)
        >>> ser.cat.ordered
        True
        >>> ser = ser.cat.as_unordered()
        >>> ser.cat.ordered
        False

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'], ordered=True)
        >>> ci.ordered
        True
        >>> ci = ci.as_unordered()
        >>> ci.ordered
        False
        Frr�s rv�as_unorderedzCategorical.as_unordered�s��<����&�&rxc��|�|jj}t||��}|j�}|r�|jj�dt|j�t|jj�kr/d|j|jt|j�k\<|j}n+t|j|j|j�}tj|||�|S)a�
        Set the categories to the specified new categories.

        ``new_categories`` can include new categories (which will result in
        unused categories) or remove old categories (which results in values
        set to ``NaN``). If ``rename=True``, the categories will simply be renamed
        (less or more items than in old categories will result in values set to
        ``NaN`` or in unused categories respectively).

        This method can be used to perform more than one action of adding,
        removing, and reordering simultaneously and is therefore faster than
        performing the individual steps via the more specialised methods.

        On the other hand this methods does not do checks (e.g., whether the
        old categories are included in the new categories on a reorder), which
        can result in surprising changes, for example when using special string
        dtypes, which does not considers a S1 string equal to a single char
        python string.

        Parameters
        ----------
        new_categories : Index-like
           The categories in new order.
        ordered : bool, default False
           Whether or not the categorical is treated as a ordered categorical.
           If not given, do not change the ordered information.
        rename : bool, default False
           Whether or not the new_categories should be considered as a rename
           of the old categories or as reordered categories.

        Returns
        -------
        Categorical with reordered categories.

        Raises
        ------
        ValueError
            If new_categories does not validate as categories

        See Also
        --------
        rename_categories : Rename categories.
        reorder_categories : Reorder categories.
        add_categories : Add new categories.
        remove_categories : Remove the specified categories.
        remove_unused_categories : Remove categories which are not used.

        Examples
        --------
        For :class:`pandas.Series`:

        >>> raw_cat = pd.Categorical(['a', 'b', 'c', 'A'],
        ...                           categories=['a', 'b', 'c'], ordered=True)
        >>> ser = pd.Series(raw_cat)
        >>> ser
        0   a
        1   b
        2   c
        3   NaN
        dtype: category
        Categories (3, object): ['a' < 'b' < 'c']

        >>> ser.cat.set_categories(['A', 'B', 'C'], rename=True)
        0   A
        1   B
        2   C
        3   NaN
        dtype: category
        Categories (3, object): ['A' < 'B' < 'C']

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'A'],
        ...                          categories=['a', 'b', 'c'], ordered=True)
        >>> ci
        CategoricalIndex(['a', 'b', 'c', nan], categories=['a', 'b', 'c'],
                         ordered=True, dtype='category')

        >>> ci.set_categories(['A', 'b', 'c'])
        CategoricalIndex([nan, 'b', 'c', nan], categories=['A', 'b', 'c'],
                         ordered=True, dtype='category')
        >>> ci.set_categories(['A', 'b', 'c'], rename=True)
        CategoricalIndex(['A', 'b', 'c', nan], categories=['A', 'b', 'c'],
                         ordered=True, dtype='category')
        r�rT)rgrYr!rSr^rWrbr`rarr�)rk�new_categoriesrY�renamerr�ras       rv�set_categorieszCategorical.set_categoriess���n�?��j�j�(�(�G�$�^�W�E�	��i�i�k����y�y�#�#�/�C�	�8L�8L�4M�PS��	�	�$�$�Q�5�GI��
�
�3�:�:��Y�-A�-A�)B�B�C��J�J�E�)��	�	�3�>�>�9�+?�+?��E�	���s�E�9�5��
rxc��t|�r*|jD�cgc]}|j||���}}n*t|�r|jD�cgc]
}||���}}|j	�}|j|�|Scc}wcc}w)a�
        Rename categories.

        Parameters
        ----------
        new_categories : list-like, dict-like or callable

            New categories which will replace old categories.

            * list-like: all items must be unique and the number of items in
              the new categories must match the existing number of categories.

            * dict-like: specifies a mapping from
              old categories to new. Categories not contained in the mapping
              are passed through and extra categories in the mapping are
              ignored.

            * callable : a callable that is called on all items in the old
              categories and whose return values comprise the new categories.

        Returns
        -------
        Categorical
            Categorical with renamed categories.

        Raises
        ------
        ValueError
            If new categories are list-like and do not have the same number of
            items than the current categories or do not validate as categories

        See Also
        --------
        reorder_categories : Reorder categories.
        add_categories : Add new categories.
        remove_categories : Remove the specified categories.
        remove_unused_categories : Remove categories which are not used.
        set_categories : Set the categories to the specified ones.

        Examples
        --------
        >>> c = pd.Categorical(['a', 'a', 'b'])
        >>> c.rename_categories([0, 1])
        [0, 0, 1]
        Categories (2, int64): [0, 1]

        For dict-like ``new_categories``, extra keys are ignored and
        categories not in the dictionary are passed through

        >>> c.rename_categories({'a': 'A', 'c': 'C'})
        ['A', 'A', 'b']
        Categories (2, object): ['A', 'b']

        You may also provide a callable to create the new categories

        >>> c.rename_categories(lambda x: x.upper())
        ['A', 'A', 'B']
        Categories (2, object): ['A', 'B']
        )rr^�get�callablerSr	)rkr�itemr�s    rv�rename_categorieszCategorical.rename_categoriesps���z��'�;?�?�?��;J�4��"�"�4��.�?�
���n�
%�?C���O��t�n�T�2��N�O��i�i�k�����N�+��
����Ps�B�Bc���t|j�t|�k7s%|jj|�jst	d��|j||��S)a�
        Reorder categories as specified in new_categories.

        ``new_categories`` need to include all old categories and no new category
        items.

        Parameters
        ----------
        new_categories : Index-like
           The categories in new order.
        ordered : bool, optional
           Whether or not the categorical is treated as a ordered categorical.
           If not given, do not change the ordered information.

        Returns
        -------
        Categorical
            Categorical with reordered categories.

        Raises
        ------
        ValueError
            If the new categories do not contain all old category items or any
            new ones

        See Also
        --------
        rename_categories : Rename categories.
        add_categories : Add new categories.
        remove_categories : Remove the specified categories.
        remove_unused_categories : Remove categories which are not used.
        set_categories : Set the categories to the specified ones.

        Examples
        --------
        For :class:`pandas.Series`:

        >>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category')
        >>> ser = ser.cat.reorder_categories(['c', 'b', 'a'], ordered=True)
        >>> ser
        0   a
        1   b
        2   c
        3   a
        dtype: category
        Categories (3, object): ['c' < 'b' < 'a']

        >>> ser.sort_values()
        2   c
        1   b
        0   a
        3   a
        dtype: category
        Categories (3, object): ['c' < 'b' < 'a']

        For :class:`pandas.CategoricalIndex`:

        >>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'])
        >>> ci
        CategoricalIndex(['a', 'b', 'c', 'a'], categories=['a', 'b', 'c'],
                         ordered=False, dtype='category')
        >>> ci.reorder_categories(['c', 'b', 'a'], ordered=True)
        CategoricalIndex(['a', 'b', 'c', 'a'], categories=['c', 'b', 'a'],
                         ordered=True, dtype='category')
        z=items in new_categories are not the same as in old categoriesr�)rWr^�
difference�emptyrXr)rkrrYs   rv�reorder_categorieszCategorical.reorder_categories�s[��F
���� �C��$7�7��?�?�-�-�n�=�C�C��O��
��"�"�>�7�"�C�Crxc��t|�s|g}t|�t|jj�z}t	|�dk7rtd|����t
|d�roddlm}t|jjj|jg�}|t|jj�t|�z|��}n+t|jj�t|�z}t||j�}|j�}t|j|j�}t!j"|||�|S)a�
        Add new categories.

        `new_categories` will be included at the last/highest place in the
        categories and will be unused directly after this call.

        Parameters
        ----------
        new_categories : category or list-like of category
           The new categories to be included.

        Returns
        -------
        Categorical
            Categorical with new categories added.

        Raises
        ------
        ValueError
            If the new categories include old categories or do not validate as
            categories

        See Also
        --------
        rename_categories : Rename categories.
        reorder_categories : Reorder categories.
        remove_categories : Remove the specified categories.
        remove_unused_categories : Remove categories which are not used.
        set_categories : Set the categories to the specified ones.

        Examples
        --------
        >>> c = pd.Categorical(['c', 'b', 'c'])
        >>> c
        ['c', 'b', 'c']
        Categories (2, object): ['b', 'c']

        >>> c.add_categories(['d', 'a'])
        ['c', 'b', 'c']
        Categories (4, object): ['b', 'c', 'd', 'a']
        rz0new categories must not include old categories: rg�rJr�)r�setrgr^rWrX�hasattrr�rJrr�r!rYrSrr�rr�)rkr�already_includedrJrgrr�ras        rv�add_categorieszCategorical.add_categoriess$��V�N�+�,�-�N��~�.��T�Z�Z�5J�5J�1K�K���� �A�%��B�CS�BT�U��
��>�7�+�%�$����&�&�,�,�n�.B�.B�C��E�$��T�Z�Z�*�*�+�d�>�.B�B�%��N�"�$�*�*�"7�"7�8�4��;O�O�N�$�^�T�\�\�B�	��i�i�k��$�S�\�\�9�3G�3G�H�����s�E�9�5��
rxc��ddlm}t|�s|g}||�j�j	�}|j
jdur'|j
jj|d��n$|j
jj|�}|j|j
j�}t|�dk7rt|�}td|����|j||jd��S)a�
        Remove the specified categories.

        `removals` must be included in the old categories. Values which were in
        the removed categories will be set to NaN

        Parameters
        ----------
        removals : category or list of categories
           The categories which should be removed.

        Returns
        -------
        Categorical
            Categorical with removed categories.

        Raises
        ------
        ValueError
            If the removals are not contained in the categories

        See Also
        --------
        rename_categories : Rename categories.
        reorder_categories : Reorder categories.
        add_categories : Add new categories.
        remove_unused_categories : Remove categories which are not used.
        set_categories : Set the categories to the specified ones.

        Examples
        --------
        >>> c = pd.Categorical(['a', 'c', 'b', 'c', 'd'])
        >>> c
        ['a', 'c', 'b', 'c', 'd']
        Categories (4, object): ['a', 'b', 'c', 'd']

        >>> c.remove_categories(['d', 'a'])
        [NaN, 'c', 'b', 'c', NaN]
        Categories (2, object): ['b', 'c']
        r�rITFr�z(removals must all be in old categories: )rYr)
r�rIr�unique�dropnargrYr^rrWr"rXr)rk�removalsrIr�not_includeds     rv�remove_categorieszCategorical.remove_categoriesHs���R	!��H�%� �z�H���?�)�)�+�2�2�4���z�z�!�!�T�)�
�J�J�!�!�,�,�X�E�,�B����&�&�1�1�(�;�	�
 �*�*�4�:�:�+@�+@�A���|���!��|�,�L��G��~�V�W�W��"�"�>�4�<�<�PU�"�V�Vrxc��tj|jd��\}}|jdk7r|ddk(r
|dd|dz
}}|jj
j
|�}tj||j��}t||j
�}|j�}tj|||�|S)a�
        Remove categories which are not used.

        Returns
        -------
        Categorical
            Categorical with unused categories dropped.

        See Also
        --------
        rename_categories : Rename categories.
        reorder_categories : Reorder categories.
        add_categories : Add new categories.
        remove_categories : Remove the specified categories.
        set_categories : Set the categories to the specified ones.

        Examples
        --------
        >>> c = pd.Categorical(['a', 'c', 'b', 'c', 'd'])
        >>> c
        ['a', 'c', 'b', 'c', 'd']
        Categories (4, object): ['a', 'b', 'c', 'd']

        >>> c[2] = 'a'
        >>> c[4] = 'c'
        >>> c
        ['a', 'c', 'a', 'c', 'c']
        Categories (4, object): ['a', 'b', 'c', 'd']

        >>> c.remove_unused_categories()
        ['a', 'c', 'a', 'c', 'c']
        Categories (2, object): ['a', 'c']
        T)�return_inverserrTr�Nr�)rir(rb�sizergr^�taker!rrYrrSrr�)rkr��invrr�	new_codesr�s       rv�remove_unused_categoriesz$Categorical.remove_unused_categories�s���D�9�9�T�[�[��>���S��8�8�q�=�S��V�r�\��1�2�w��a���C����.�.�3�3�C�8��$�3�3��D�L�L�
�	�)��i�.B�.B�C�	��i�i�k�����s�I�y�9��
rxc�6�|tjur&tjdtt���d}t
|�s
t|�sJ�|jj|�}tj|jdk(�}tj}|�Q|rOt
|�r|tj�n-|jtjtj�}|jra|j sU|tjurCt#||j$��}|j'|jj)�|d��S|r|j+t-|�|�}tj.||j�S)a
        Map categories using an input mapping or function.

        Maps the categories to new categories. If the mapping correspondence is
        one-to-one the result is a :class:`~pandas.Categorical` which has the
        same order property as the original, otherwise a :class:`~pandas.Index`
        is returned. NaN values are unaffected.

        If a `dict` or :class:`~pandas.Series` is used any unmapped category is
        mapped to `NaN`. Note that if this happens an :class:`~pandas.Index`
        will be returned.

        Parameters
        ----------
        mapper : function, dict, or Series
            Mapping correspondence.
        na_action : {None, 'ignore'}, default 'ignore'
            If 'ignore', propagate NaN values, without passing them to the
            mapping correspondence.

            .. deprecated:: 2.1.0

               The default value of 'ignore' has been deprecated and will be changed to
               None in the future.

        Returns
        -------
        pandas.Categorical or pandas.Index
            Mapped categorical.

        See Also
        --------
        CategoricalIndex.map : Apply a mapping correspondence on a
            :class:`~pandas.CategoricalIndex`.
        Index.map : Apply a mapping correspondence on an
            :class:`~pandas.Index`.
        Series.map : Apply a mapping correspondence on a
            :class:`~pandas.Series`.
        Series.apply : Apply more complex functions on a
            :class:`~pandas.Series`.

        Examples
        --------
        >>> cat = pd.Categorical(['a', 'b', 'c'])
        >>> cat
        ['a', 'b', 'c']
        Categories (3, object): ['a', 'b', 'c']
        >>> cat.map(lambda x: x.upper(), na_action=None)
        ['A', 'B', 'C']
        Categories (3, object): ['A', 'B', 'C']
        >>> cat.map({'a': 'first', 'b': 'second', 'c': 'third'}, na_action=None)
        ['first', 'second', 'third']
        Categories (3, object): ['first', 'second', 'third']

        If the mapping is one-to-one the ordering of the categories is
        preserved:

        >>> cat = pd.Categorical(['a', 'b', 'c'], ordered=True)
        >>> cat
        ['a', 'b', 'c']
        Categories (3, object): ['a' < 'b' < 'c']
        >>> cat.map({'a': 3, 'b': 2, 'c': 1}, na_action=None)
        [3, 2, 1]
        Categories (3, int64): [3 < 2 < 1]

        If the mapping is not one-to-one an :class:`~pandas.Index` is returned:

        >>> cat.map({'a': 'first', 'b': 'second', 'c': 'first'}, na_action=None)
        Index(['first', 'second', 'first'], dtype='object')

        If a `dict` is used, all unmapped categories are mapped to `NaN` and
        the result is an :class:`~pandas.Index`:

        >>> cat.map({'a': 'first', 'b': 'second'}, na_action=None)
        Index(['first', 'second', nan], dtype='object')
        z�The default value of 'ignore' for the `na_action` parameter in pandas.Categorical.map is deprecated and will be changed to 'None' in a future version. Please set na_action to the desired value to avoid seeing this warningr��ignorerTr�F�rgr�)rr�r�r�r�rrrr^�maprircrb�nanr�	is_unique�hasnansr!rYr�rS�insertrWr0)rk�mapper�	na_actionr�has_nans�na_valrs       rvr7zCategorical.map�s2��b����&��M�M�=��+�-�

�!�I����<��#7�7�7����,�,�V�4���6�6�$�+�+��+�,���������'/��'7�V�B�F�F�^�V�Z�Z����PR�PV�PV�=W�F��#�#�N�,B�,B�v�QS�QW�QW�GW�(�����N�I��?�?�4�;�;�#3�#3�#5�Y�QV�?�W�W��+�2�2�3�~�3F��O�N��w�w�~�t�{�{�3�3rxc�\�t|�s|j|�S|j|�Sr)r�_validate_listlike�_validate_scalar)rkrs  rv�_validate_setitem_valuez#Categorical._validate_setitem_value0s,���5�!��*�*�5�1�1��(�(��/�/rxc��t||jj�rd}|S||jvr|j|�}|St	d|�d��d�)aK
        Convert a user-facing fill_value to a representation to use with our
        underlying ndarray, raising TypeError if this is not possible.

        Parameters
        ----------
        fill_value : object

        Returns
        -------
        fill_value : int

        Raises
        ------
        TypeError
        rTz5Cannot setitem on a Categorical with a new category (z), set the categories firstN)r&r^rgrdrZ)rkrss  rvrBzCategorical._validate_scalar7sr��$!��T�_�_�-B�-B�C��J����4�?�?�
*��+�+�J�7�J���	��'�L�(C�E���
rxc��t|t�rZt|j�rEt	|�j�rt
d��|jtj��}ntj|�}t|�r#|jjdvrt
d��t|�rD|j�t|j�k\s|j�dkrt
d��|S)Nzcodes cannot contain NA valuesr�r�z$codes need to be array-like integersrTz1codes need to be between -1 and len(categories)-1)r[r1rrgr'rcrXr�ri�int64�asarrayrWr��maxr^�min)r�rargs   rvr�z%Categorical._validate_codes_for_dtypeTs����e�^�,�1A�%�+�+�1N��E�{��� � �!A�B�B��N�N����N�2�E��J�J�u�%�E��u�:�%�+�+�*�*�$�6��C�D�D��u�:�5�9�9�;�#�e�.>�.>�*?�?�5�9�9�;�QS�CS��P�Q�Q��rxc��t|jj|j�}|rBt	j
|�|jj
k7rt	j||�St	j|�S)a�
        The numpy array interface.

        Returns
        -------
        numpy.array
            A numpy array of either the specified dtype or,
            if dtype==None (default), the same dtype as
            categorical.categories.dtype.

        Examples
        --------

        >>> cat = pd.Categorical(['a', 'b'], ordered=True)

        The following calls ``cat.__array__``

        >>> np.asarray(cat)
        array(['a', 'b'], dtype=object)
        )r.r^r�rbrirgrG)rkrgrSrps    rv�	__array__zCategorical.__array__fs[��0�d�o�o�-�-�t�{�{�;���R�X�X�e�_����(=�(=�=��:�:�c�5�)�)��z�z�#��rxc�(�tj|||g|��i|��}|tur|Sd|vrtj|||g|��i|��S|dk(r%tj|||g|��i|��}|tur|Std|j�d|j����)N�out�reducezObject with dtype z cannot perform the numpy op )r)�!maybe_dispatch_ufunc_to_dunder_op�NotImplemented�dispatch_ufunc_with_out�dispatch_reduction_ufuncrZrgry)rk�ufunc�method�inputs�kwargsr�s      rv�__array_ufunc__zCategorical.__array_ufunc__�s����<�<��%��
�"(�
�,2�
����'��M��F�?��4�4��e�V��&,��06��
��X���7�7��e�V��&,��06��F��^�+��
�� �����-�!�N�N�+�
-�
�	
rxc����t|t�st�|�
|�Sd|vrt	|d|d�|d<d|vrd|vr|jd�|d<t�|�
|�y)z*Necessary for making this object picklabler��_categories�_orderedrbr�N)r[�dictr��__setstate__r!�pop)rk�stater�s  �rvr\zCategorical.__setstate__�sn����%��&��7�'��.�.��5� �.�u�]�/C�U�:�EV�W�E�(�O��u���5�!8� %�	�	�(� 3�E�*��
���U�#rxc��|jj|jjjjzSr)rb�nbytesrgr^r�r�s rvr`zCategorical.nbytes�s-���{�{�!�!�D�J�J�$9�$9�$@�$@�$G�$G�G�Grxc�|�|jj|jjj	|��zS)a�
        Memory usage of my values

        Parameters
        ----------
        deep : bool
            Introspect the data deeply, interrogate
            `object` dtypes for system-level memory consumption

        Returns
        -------
        bytes used

        Notes
        -----
        Memory usage does not include memory consumed by elements that
        are not components of the array if deep=False

        See Also
        --------
        numpy.ndarray.nbytes
        )�deep)rbr`rgr^�memory_usage)rkrbs  rvrczCategorical.memory_usage�s1��.�{�{�!�!�D�J�J�$9�$9�$F�$F�D�$F�$Q�Q�Qrxc� �|jdk(S)aX
        Detect missing values

        Missing values (-1 in .codes) are detected.

        Returns
        -------
        np.ndarray[bool] of whether my values are null

        See Also
        --------
        isna : Top-level isna.
        isnull : Alias of isna.
        Categorical.notna : Boolean inverse of Categorical.isna.

        rT)rbr�s rvr'zCategorical.isna�s��"�{�{�b� � rxc�$�|j�S)a�
        Inverse of isna

        Both missing values (-1 in .codes) and NA as a category are detected as
        null.

        Returns
        -------
        np.ndarray[bool] of whether my values are not null

        See Also
        --------
        notna : Top-level notna.
        notnull : Alias of notna.
        Categorical.isna : Boolean inverse of Categorical.notna.

        )r'r�s rv�notnazCategorical.notna�s��$�	�	��|�rxc��ddlm}m}|j|j}}t|�|dk\}}t
j|�|j�}	}|s|	r%|	r|n||}
t
j|
|xsd��}n@t
jt
j|||��}t
j|d�}t||jj�}|j|�}||||�ddd��S)	a{
        Return a Series containing counts of each category.

        Every category will have an entry, even those with a count of 0.

        Parameters
        ----------
        dropna : bool, default True
            Don't include counts of NaN.

        Returns
        -------
        counts : Series

        See Also
        --------
        Series.value_counts
        r)�CategoricalIndexrJ)�	minlengthrTrF�countF)�indexrg�namerS)r�rhrJrbr^rWri�aranger��bincountr��appendrrg�_from_backing_data)rkr)rhrJ�coder��ncatrq�ix�clean�obsrjs            rv�value_countszCategorical.value_counts�s���&	
�
�K�K����c���#�h���	�d���I�I�d�O�T�X�X�Z�E���U��$�T�$�Z�C��K�K��t�y�q�9�E��K�K�����t�T� :�;�E����2�r�"�B�
!�"�d�j�j�&;�&;�
<��
�
$�
$�R�
(����)�"�-�W�7�QV�
�	
rxc��|jg|��}tj||jj��}|j|�S)z�
        Analogous to np.empty(shape, dtype=dtype)

        Parameters
        ----------
        shape : tuple[int]
        dtype : CategoricalDtype
        r�)r�ri�zerosr�rgrp)r�r�rgr��backings     rv�_emptyzCategorical._empty&sF��� � ��5� �1��
�(�(�5����(:�(:�;���%�%�g�.�.rxc���t|jj�r5|jj|jt
��jSt|jj�r\d|jvrN|jjd�j|jtj��jStj|�S)a
        Return the values.

        For internal compatibility with pandas formatting.

        Returns
        -------
        np.ndarray or ExtensionArray
            A numpy array or ExtensionArray of the same dtype as
            categorical.categories.dtype.
        r�rTr�)rr^rgr0rbrr�rr�rir8rjr�s rv�_internal_get_valuesz Categorical._internal_get_values;s����t���4�4�5��?�?�'�'�����'�D�L�L�L�
�d�o�o�3�3�
4��t�{�{�9J����&�&�x�0���d�k�k�b�f�f��5���
�
�x�x��~�rxc�:�|jstd|�d���y)zassert that we are orderedz)Categorical is not ordered for operation zG
you can use .as_ordered() to change the Categorical to an ordered one
N)rYrZ)rkrts  rv�check_for_orderedzCategorical.check_for_orderedRs-���|�|��;�B�4�@2�2��
�rx�	quicksort��	ascendingr�c�(��t�|�d||d�|��S)a�
        Return the indices that would sort the Categorical.

        Missing values are sorted at the end.

        Parameters
        ----------
        ascending : bool, default True
            Whether the indices should result in an ascending
            or descending sort.
        kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
            Sorting algorithm.
        **kwargs:
            passed through to :func:`numpy.argsort`.

        Returns
        -------
        np.ndarray[np.intp]

        See Also
        --------
        numpy.ndarray.argsort

        Notes
        -----
        While an ordering is applied to the category values, arg-sorting
        in this context refers more to organizing and grouping together
        based on matching category values. Thus, this function can be
        called on an unordered Categorical instance unlike the functions
        'Categorical.min' and 'Categorical.max'.

        Examples
        --------
        >>> pd.Categorical(['b', 'b', 'a', 'c']).argsort()
        array([2, 0, 1, 3])

        >>> cat = pd.Categorical(['b', 'b', 'a', 'c'],
        ...                      categories=['c', 'b', 'a'],
        ...                      ordered=True)
        >>> cat.argsort()
        array([3, 0, 1, 2])

        Missing values are placed at the end

        >>> cat = pd.Categorical([2, None, 1])
        >>> cat.argsort()
        array([2, 0, 1])
        r�r�)r��argsort)rkr�r�rVr�s    �rvr�zCategorical.argsort[s���f�w��H���H��H�Hrx)�inplacer��na_positionc��yrr��rkr�r�r�s    rvr�zCategorical.sort_values�s��	rx�r�r�c��yrr�r�s    rvr�zCategorical.sort_values�s��	rx�lastc���t|d�}|dvrtdt|�����t|||��}|s |j|}|j|�S|j||jddy)a�
        Sort the Categorical by category value returning a new
        Categorical by default.

        While an ordering is applied to the category values, sorting in this
        context refers more to organizing and grouping together based on
        matching category values. Thus, this function can be called on an
        unordered Categorical instance unlike the functions 'Categorical.min'
        and 'Categorical.max'.

        Parameters
        ----------
        inplace : bool, default False
            Do operation in place.
        ascending : bool, default True
            Order ascending. Passing False orders descending. The
            ordering parameter provides the method by which the
            category values are organized.
        na_position : {'first', 'last'} (optional, default='last')
            'first' puts NaNs at the beginning
            'last' puts NaNs at the end

        Returns
        -------
        Categorical or None

        See Also
        --------
        Categorical.sort
        Series.sort_values

        Examples
        --------
        >>> c = pd.Categorical([1, 2, 2, 1, 5])
        >>> c
        [1, 2, 2, 1, 5]
        Categories (3, int64): [1, 2, 5]
        >>> c.sort_values()
        [1, 1, 2, 2, 5]
        Categories (3, int64): [1, 2, 5]
        >>> c.sort_values(ascending=False)
        [5, 2, 2, 1, 1]
        Categories (3, int64): [1, 2, 5]

        >>> c = pd.Categorical([1, 2, 2, 1, 5])

        'sort_values' behaviour with NaNs. Note that 'na_position'
        is independent of the 'ascending' parameter:

        >>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])
        >>> c
        [NaN, 2, 2, NaN, 5]
        Categories (2, int64): [2, 5]
        >>> c.sort_values()
        [2, 2, 5, NaN, NaN]
        Categories (2, int64): [2, 5]
        >>> c.sort_values(ascending=False)
        [5, 2, 2, NaN, NaN]
        Categories (2, int64): [2, 5]
        >>> c.sort_values(na_position='first')
        [NaN, NaN, 2, 2, 5]
        Categories (2, int64): [2, 5]
        >>> c.sort_values(ascending=False, na_position='first')
        [NaN, NaN, 5, 2, 2]
        Categories (2, int64): [2, 5]
        r�)r��firstzinvalid na_position: r�N)rrX�reprr7rbrp)rkr�r�r��
sorted_idxras      rvr�zCategorical.sort_values�s|��R&�g�y�9���/�/��4�T�+�5F�4G�H�I�I��d�i�[�Q�
���K�K�
�+�E��*�*�5�1�1����Z�0����A��rxr�average�keep��axisrT�	na_optionr��pctc�n�|dk7rt�|j�}tj||||||��S)z*
        See Series.rank.__doc__.
        rr�)r��_values_for_rankr(�rank)rkr�rTr�r�r��vffs       rv�_rankzCategorical._rank�sB���1�9�%�%��#�#�%�����������

�	
rxc��ddlm}|jrG|j}|dk(}|j	�r$|jd�}tj||<|St|jj�rt
j|�}|St
j|j||jd��j�j��}|S)z�
        For correctly ranking ordered categorical data. See GH#15420

        Ordered categorical data should be ranked on the basis of
        codes with -1 translated to NaN.

        Returns
        -------
        numpy.array

        rr!rT�float64FrR)r�rJrYrarcr�rir8rr^rgrjrr�r�)rkrJr�rqs    rvr�zCategorical._values_for_rank
s���	"��<�<��Z�Z�F��R�<�D��x�x�z����y�1��!�v�v��t���
�'�t���'<�'<�
=��X�X�d�^�F��
��X�X��&�&��4�?�?��7�<�<�>�E�E���F�
�
rxc�p�ddlm}tj|jj
�}||||d��}|j
�}t|�r|j|j�}n tjt|�d��}|j�rtj||<|S)aA
        Hash a Categorical by hashing its categories, and then mapping the codes
        to the hashes.

        Parameters
        ----------
        encoding : str
        hash_key : str
        categorize : bool
            Ignored for Categorical.

        Returns
        -------
        np.ndarray[uint64]
        r)�
hash_arrayF)�
categorize�uint64r�)�pandas.core.util.hashingr�rirGr^r�r'rWr0rbrxrcr�u8max)	rk�encoding�hash_keyr�r�r��hashedrqr�s	         rv�_hash_pandas_objectzCategorical._hash_pandas_object-s���&	8����D�O�O�3�3�4���F�H�h�5�I���y�y�{���v�;��[�[����-�F��X�X�c�$�i�x�8�F��8�8�:��9�9�F�4�L��
rxc��|jSr)r�r�s rvrbzCategorical._codes[s���}�}�rxc�J�|dk(rtjS|j|Sr�)rir8r^)rkrrs  rv�	_box_funczCategorical._box_func_s!����7��6�6�M����q�!�!rxc��|jj|�}|jjj	|�}|Sr)r^r�r�rgrf)rkr�rqs   rvrdzCategorical._unbox_scalards7�����&�&�s�+���}�}�"�"�'�'��-���rxc����jdk(r't�j�j��S�fd�t	t���D�S)zJ
        Returns an Iterator over the values of this Categorical.
        r�c3�(�K�|]	}�|���y�wrr�)r��nrks  �rvr�z'Categorical.__iter__.<locals>.<genexpr>ts�����6�%5��D��G�%5�s�)r��iterr|r��rangerWr�s`rv�__iter__zCategorical.__iter__msA����9�9��>���1�1�3�:�:�<�=�=�6�U�3�t�9�%5�6�6rxc��t||jj�r't|j	�j��St
|||j��S)z?
        Returns True if `key` is in this Categorical.
        )r�)r&r^rg�boolr'rcr�rb)rkr�s  rv�__contains__zCategorical.__contains__vsC��
!��d�o�o�&;�&;�<���	�	����)�*�*���c�T�[�[�9�9rxc��yrr�)rk�boxeds  rv�
_formatterzCategorical._formatter�s��rxc���td�dk(rdn
td�}ddlm}t|jdt
��}t
|j�|kDrN|dz}||jd|j�}||j|dj�}|dgz|z}n||jj�}|D�cgc]}|j���}}|Scc}w)	z9
        return the base repr for the categories
        zdisplay.max_categoriesr�
��formatN)�	formatter�quoting�z...)
r�pandas.io.formatsr�r�format_arrayrrWr^r��strip)	rk�max_categories�fmtr��num�head�tail�
category_strs�xs	         rv�_repr_categorieszCategorical._repr_categories�s����2�3�q�8�
��4�5�	�
	4������6F�
���t����.�0� �A�%�C������� 5� =� =�>�D�������� 6� >� >�?�D� �E�7�N�T�1�M�(����)@�)@�A�M�-:�:�M�q�����M�
�:����;s�Cc�Z�|j�}t|jj�}dt	|j��d|�d�}t�\}}t
d�xs|}tj�rd}d}d}t	|�}	|jrdnd	\}
}|j��d
�}|D]]}
|dk7r:|	|
zt	|
�z|kDr&||dt	|�dzzzz
}t	|�dz}	n|s||z
}|	t	|
�z
}	||
z
}d
}�_|�d|jdd��d�S)z@
        Returns a string representation of the footer.
        zCategories (�, z): z
display.widthr�T)�z < )r�r��
� r�F�[z	 < ... < z ... �])r��strr^rgrWrrr9�in_ipython_frontendrY�rstrip�replace)rkr�rg�	levheader�width�_�	max_width�	levstring�start�cur_col_len�sep_len�sep�linesep�vals              rv�_get_repr_footerzCategorical._get_repr_footer�sD���-�-�/�
��D�O�O�)�)�*��"�3�t���#7�"8��5�'��E�	�$�&���q���/�8�5�	��&�&�(��I��	����)�n��%)�\�\�z�y�����Z�Z�\�N�"�%�� �C��A�~�+��"7�#�c�(�"B�Y�"N��W��s�9�~��/A�(B�C�C�	�!�)�n�q�0����S� �	��s�3�x�'�����I��E�!���A�i�/�/��W�E�F�a�H�Hrxc��ddlm}t|�dkDsJ�|j�}|j	|dddt
��}|D�cgc]}|j
���}}dj|�}d|zdz}|Scc}w)Nrr��NaN)�float_format�na_repr�r�r�r�)r�r�rWr|r�rr��join)rkr��vals�
fmt_valuesrr�joinedr�s       rv�_get_values_reprzCategorical._get_values_repr�s���3��4�y�1�}��}��(�(�*���%�%�����$�&�
�
�*4�4��A�a�g�g�i��
�4����:�&���v���#���
��5s�A9c�D�|j�}t|�}d}||kDrV|dz}|d|j�}|||z
dj�}|dd�d|dd��}dt|���}|�d|�d|��}	|	S|d	kDr|j�}|�d|��}	|	Sd
}|�d|��}	|	S)z(
        String representation.
        r�r�NrTz, ..., r�zLength: r�rz[]r�)r�rWr�)
rk�footer�length�max_lenr�r�r��body�length_infor�s
          rv�__repr__zCategorical.__repr__�s����&�&�(���T������G���Q�,�C����:�.�.�0�D��'�C�-�(�*�+�<�<�>�D��3�B�i�[���Q�R��z�2�D�$�S��Y�K�0�K��v�R��}�B�v�h�7�F��
��a�Z��(�(�*�D��v�R��x�(�F��
��D��v�R��x�(�F��
rxc��t|d��}t|t�rA|j|jk7rt	d��|j|�}|jSddlm}|j|d��j|j�}t|�r$t|�j�st	d��|jj|�}|j!|j"jd�	�S)
NT)�
extract_numpyzCCannot set a Categorical with another, without identical categoriesrr'F)�
tupleize_colszMCannot setitem on a Categorical with a new category, set the categories firstrR)r4r[r\rgrZ�_encode_with_my_categoriesrbr�rI�_with_inferrr^rWr'r��get_indexerr�r�)rkrrI�to_addras     rvrAzCategorical._validate_listlike�s����e�4�8���e�[�)��z�z�U�[�[�(��3���
�3�3�E�:�E��<�<�� �#��"�"�5��>�I�I��O�O�
���v�;�t�F�|�/�/�1��5��
�
���+�+�E�2���|�|�D�M�M�/�/�e�|�<�<rxc���|j}tjt|j�|j
�\�}t
|�j�}�fd�t||dd�D�}tt||��S)a�
        Compute the inverse of a categorical, returning
        a dict of categories -> indexers.

        *This is an internal function*

        Returns
        -------
        Dict[Hashable, np.ndarray[np.intp]]
            dict of categories -> indexers

        Examples
        --------
        >>> c = pd.Categorical(list('aabca'))
        >>> c
        ['a', 'a', 'b', 'c', 'a']
        Categories (3, object): ['a', 'b', 'c']
        >>> c.categories
        Index(['a', 'b', 'c'], dtype='object')
        >>> c.codes
        array([0, 0, 1, 2, 0], dtype=int8)
        >>> c._reverse_indexer()
        {'a': array([0, 1, 4]), 'b': array([2]), 'c': array([3])}

        c3�.�K�|]\}}�||���y�wrr�)r�r��end�rs   �rvr�z/Categorical._reverse_indexer.<locals>.<genexpr>.	s�����J�2I�J�E�3�1�U�3�<�2I�s�r�N)
r^�libalgos�groupsort_indexerrrar/r�cumsum�zipr[)rkr^�counts�_resultr�s    @rv�_reverse_indexerzCategorical._reverse_indexer	sr���4�_�_�
��.�.���
�
�+�Z�_�_�
�	��6��f�%�,�,�.��J�#�f�f�Q�R�j�2I�J���C�
�G�,�-�-rx��skipna�keepdimsc�x��t�|�|f||d�|��}|dvr|S|rt|�||j��S|S)Nr)�argmax�argminr�)r��_reducerfrg)rkrlrrrVr�r�s      �rvrzCategorical._reduce4	sL�������R�f�x�R�6�R���'�'��M���4��:�f�D�J�J�7�7��Mrx)rc��tj|jdd��tjd|�|j	d�t|j�s|jjS|jdk7}|j�s@|r.|j�r|j|j�}n*tjS|jj�}|jd|�S)a/
        The minimum value of the object.

        Only ordered `Categoricals` have a minimum!

        Raises
        ------
        TypeError
            If the `Categorical` is not `ordered`.

        Returns
        -------
        min : the minimum of this `Categorical`, NA value if empty
        r�rr�rIrTN)�nv�validate_minmax_axisr�validate_minr~rWrbrg�na_valuer�rcrIrir8�_wrap_reduction_result�rkrrV�good�pointers     rvrIzCategorical.min@	���	����
�
�6�1� 5�6�
����F�#����u�%��4�;�;���:�:�&�&�&��{�{�b� ���x�x�z��$�(�(�*��+�+�d�+�/�/�1���v�v�
��k�k�o�o�'�G��*�*�4��9�9rxc��tj|jdd��tjd|�|j	d�t|j�s|jjS|jdk7}|j�s@|r.|j�r|j|j�}n*tjS|jj�}|jd|�S)a2
        The maximum value of the object.

        Only ordered `Categoricals` have a maximum!

        Raises
        ------
        TypeError
            If the `Categorical` is not `ordered`.

        Returns
        -------
        max : the maximum of this `Categorical`, NA if array is empty
        r�rr�rHrTN)r
rr�validate_maxr~rWrbrgr
r�rcrHrir8rrs     rvrHzCategorical.max`	rrxc��|j}d}|r|j�}tj||��}t	t
j|�}|j|jk(sJ�|j|�}|S)N)rq)	rbr'r(�moder	rir�rgrp)rkr)rarq�	res_codesr�s      rv�_modezCategorical._mode�	si����������9�9�;�D��O�O�E��5�	�����Y�/�	����%�+�+�-�-�-��%�%�i�0���
rxc� ��t�|��S)a�
        Return the ``Categorical`` which ``categories`` and ``codes`` are
        unique.

        .. versionchanged:: 1.3.0

            Previously, unused categories were dropped from the new categories.

        Returns
        -------
        Categorical

        See Also
        --------
        pandas.unique
        CategoricalIndex.unique
        Series.unique : Return unique values of Series object.

        Examples
        --------
        >>> pd.Categorical(list("baabc")).unique()
        ['b', 'a', 'c']
        Categories (3, object): ['a', 'b', 'c']
        >>> pd.Categorical(list("baab"), categories=list("abc"), ordered=True).unique()
        ['b', 'a']
        Categories (3, object): ['a' < 'b' < 'c']
        )r�r()rkr�s �rvr(zCategorical.unique�	s���:�w�~��rxc�P�|j|jjk(sJ�|Sr)rgr�)rk�
res_valuess  rv�_cast_quantile_resultz!Categorical._cast_quantile_result�	s&�����4�=�=�#6�#6�6�6�6��rxr�c��t|t�sy|j|�r;|j|�}t	j
|j|j�Sy)z�
        Returns True if categorical arrays are equal.

        Parameters
        ----------
        other : `Categorical`

        Returns
        -------
        bool
        F)r[r\r]r�ri�array_equalrb�rkrls  rvr_zCategorical.equals�	sK���%��-��
�
5�
5�e�
<��3�3�E�:�E��>�>�$�+�+�u�|�|�<�<�rxc	��ddlm}|d}||jk\rtd|�d|j����|dk(r�t	d�|D��st�g}|D]>}|jt
|jd�D�cgc]}|dd�|f��
c}��@|j|d��}|jt|�dd	�
�}	|	S||�}	|	Scc}w)Nr)�union_categoricalszaxis z) is out of bounds for array of dimension r�c3�:K�|]}|jdk(���y�w)r�N)r�)r�r�s  rvr�z0Categorical._concat_same_type.<locals>.<genexpr>�	s����6�I�q�q�v�v��{�I�s��r�rT�F)�order)�pandas.core.dtypes.concatr!r�rXr��extendr�r��_concat_same_type�reshaperW)
r��	to_concatr�r!r��tc_flat�objrr�res_flatr�s
          rvr(zCategorical._concat_same_type�	s���@��!����5�:�:�����v�F�u�z�z�l�S��
��1�9��6�I�6�6� � ��G� �����5����1��3F�G�3F�a��A�q�D�	�3F�G�H�!��,�,�W�1�,�=�H��%�%�c�%�j�"�C�%�@�F��M�#�I�.���
�� Hs�?C
c�~�t|j|j|jd��}|j|�S)z�
        Re-encode another categorical using this Categorical's categories.

        Notes
        -----
        This assumes we have already checked
        self._categories_match_up_to_permutation(other).
        FrR)r`rar^rp)rkrlras   rvr�z&Categorical._encode_with_my_categories�	s8��&��K�K��)�)�4�?�?��
���&�&�u�-�-rxc�X�t|j�t|j�k(S)z�
        Returns True if categoricals are the same dtype
          same categories, and same ordered

        Parameters
        ----------
        other : Categorical

        Returns
        -------
        bool
        )r�rgrs  rvr]z/Categorical._categories_match_up_to_permutation�	s!���D�J�J��4����#4�4�4rxc��|jd��}||j�z}ddlm}ddlm}|||gd��}|dd	g�|_d
|j_|S)z�
        Describes this Categorical

        Returns
        -------
        description: `DataFrame`
            A dataframe with frequency and counts by category.
        F)r)rr')�concatr�r#r��freqsr^)	rv�sumr�rI�pandas.core.reshape.concatr1�columnsrkrl)rkr�r2rIr1r�s      rv�describezCategorical.describe
s_���"�"�%�"�0�������%�� �5�����a�0����'�2�3���(������
rxc���tjt|��}|jj	|�}|||dk\z}tj|j|�S)a�
        Check whether `values` are contained in Categorical.

        Return a boolean NumPy Array showing whether each element in
        the Categorical matches an element in the passed sequence of
        `values` exactly.

        Parameters
        ----------
        values : np.ndarray or ExtensionArray
            The sequence of values to test. Passing in a single string will
            raise a ``TypeError``. Instead, turn a single string into a
            list of one element.

        Returns
        -------
        np.ndarray[bool]

        Raises
        ------
        TypeError
          * If `values` is not a set or list-like

        See Also
        --------
        pandas.Series.isin : Equivalent method on Series.

        Examples
        --------
        >>> s = pd.Categorical(['lama', 'cow', 'lama', 'beetle', 'lama',
        ...                'hippo'])
        >>> s.isin(['cow', 'lama'])
        array([ True,  True,  True, False,  True, False])

        Passing a single string as ``s.isin('lama')`` will raise an error. Use
        a list of one element instead:

        >>> s.isin(['lama'])
        array([ True, False,  True, False,  True, False])
        r)rirGr'r^�get_indexer_forr(r�ra)rkr�r��code_valuess    rvr�zCategorical.isin
sT��R�J�J�t�F�|�,�	��o�o�5�5�f�=��!�)�{�a�/?�"@�A�����t�z�z�;�7�7rx)r�c�>�ddlm}|j}t|d�}|r|n|j	�}tt
j|��}|j�r|t
j|�|}|j|jj|�}|j|�}	tj||	j|	j�|jj�}
|
j!||��}
||
�}|jj#|�}t
j$t'|
��}
t
j(|dk(|
|�}
|
j+�}
|
j-|
�}|j/d��}||�}t1|j2||d�	�}t5||jj6�
�}tj|||�||k7r$t9j:dt<t?���|s|Sy)
Nrr'r�)�
to_replacerrTr�)r�FrRr�z�The behavior of Series.replace (and DataFrame.replace) with CategoricalDtype is deprecated. In a future version, replace will only be used for cases that preserve the categories. To change the categories, use ser.cat.rename_categories instead.r�) r�rIrgrrSr'rirGrcr^r�r,rr�ra�	to_seriesr�r8rmrWr�r�r0�drop_duplicatesr`rbr!rYr�r�r�r)rkr;rr�rI�
orig_dtyper�rqr*�new_cat�ser�
all_values�idxr�locsrr2rs                 rv�_replacezCategorical._replaceG
s��� ��Z�Z�
�%�g�y�9���d�4�9�9�;���B�J�J�u�%�&���8�8�:��z�z�*�-�d�3�H��~�~�c�n�n�&9�&9�(�&C�D�H��+�+�H�5�G��"�"�3��
�
�w�}�}�E��n�n�&�&�(���k�k�Z�u�k�=���3�Z�
��~�~�-�-�j�9���y�y��S��"���x�x���
�D�$�/���|�|�~�����$���'�7�7�W�7�E���~�.��)��J�J�
�N��
�	�%�^�T�Z�Z�=O�=O�P�	����s�I�y�9��
�"��M�M��
�+�-�	
���J�rxc��ddlm}|j}|j}||j	��j|||�}t
|||��S)Nr��NumpyExtensionArrayr�)�pandas.core.arraysrGr^rar��_str_mapr.)	rk�fr
rg�convertrGr^rar�s	         rvrIzCategorical._str_mapz
sK��	;��_�_�
��
�
��$�Z�%8�%8�%:�;�D�D�Q��RW�X���v�u��:�:rxc�b�ddlm}||jt��j	|�S)NrrF)rHrGr�r��_str_get_dummies)rkr�rGs   rvrMzCategorical._str_get_dummies�
s$��:�"�4�;�;�s�#3�4�E�E�c�J�Jrxc	��ddlm}|j|�}||||��}	|j}
|dvrt	|
�d|�d���|dvr|
j
st	d|�d	���|d
vr'|dk(rt	|
�d|�d���t	|
�d|�d
���d}|j
�}|dk(r|j
sJ�|j}
nA|dvr(|j}
tj|t��}n|jt�}
|	j|
f|||||d�|��}||	jvr|S|dvrd||dk(<|j|�S)Nr)�WrappedCythonOp)�howr��has_dropped_na)r3�prodr��cumprod�skewz type does not support z operations)rIrHr��idxmin�idxmaxzCannot perform z with non-ordered Categorical)	r�rcr�r�r�rIrHrUrV�	transformz% dtype does not support aggregation '�'r�)r�r�rIrHrUrVr�)�	min_count�ngroups�comp_idsrq�result_mask)r�r�rIrHrTr�)�pandas.core.groupby.opsrO�get_kind_from_howrgrZrYr'r�rirxr�r��_cython_op_ndim_compat�cast_blocklistrp)rkrPrQrYrZ�idsrVrOr�rtrgr\rq�npvaluesrs               rv�_groupby_opzCategorical._groupby_op�
s���	<��0�0��5��
��4��
O���
�
���>�>��u�g�%<�S�E��M�N�N��<�<�U�]�]��o�c�U�2O�P�Q�Q��

�

��{�"��5�'�)@���[� Q�R�R��u�g�%J�3�%�q�Q�R�R����y�y�{���&�=��<�<��<��}�}�H�
�G�
G��}�}�H��(�(�7�$�7�K��{�{�4�(�H�.�R�.�.��
�����#�

��
�
��"�#�#�#���
�3�
3�+-�J�{�a�'�(��&�&�z�2�2rx)ra�
np.ndarrayrgr!�returnrD)rg�Dtype | Noner�zbool | lib.NoDefaultrSr�re�None)rer!)re�int)rgrfrSr�rerD)rgrArerD).)rgz
npt.DTypeLikerSr�rerd)rgr#rSr�rer1)rgr>rSr�rer=�Tr)rerD)NNNT)rgrfr�r�rerD)rerI)rerC)rerd)F)r�r�rerg)rgr!rerD)rr�rerD)NF)rr�)r=z(Literal['ignore'] | None | lib.NoDefault)rgr!rerd)NN)rgzNpDtype | NonerSzbool | Nonererd)rSznp.ufuncrTr��rerg)rbr�rerh)re�npt.NDArray[np.bool_])r)r�rerJ)r�rErgr!rerD)rer=)r�r�r�rF)r�zLiteral[False]r�r�r�r�rerD)r�z
Literal[True]r�r�r�r�rerg)r�r�r�r�r�r�rezSelf | None)
r�r?rTr�r�r�r�r�r�r�)r�r�r�r�r�r�reznpt.NDArray[np.uint64])rrrh)rer;�rer�)r�r�)rez	list[str])rer�)rez$dict[Hashable, npt.NDArray[np.intp]])rlr�rr�rr�)rr�)r)r�rer\)rrdrerd)rlr�rer�)r)r*zSequence[Self]r�r?rerD)rlr\rer\)rlr\rer�)rerH)r�r=rerk)r�r�)rKr�)�|)r�r�)
rPr�rQr�rYrhrZrhraznpt.NDArray[np.intp])kry�
__module__�__qualname__�__doc__�__array_priority__r3�
_hidden_attrs�	frozenset�_typ�__annotations__�classmethodr�rr�r��propertyrgr�r�r�r
r�r�r�r�r^rYrar	r�r
rrrrrr%r,r3r7r|rz�eqrUr{rV�ltrN�gtrO�lerP�gerQrCrBr�r0rKrWr\r`rcr'�isnullrf�notnullrvrzr|r~r�r�r�r�r�rbr�rdr�r�r�r�r�r�r�rArrrIrHrr(rr_r(r�r]r6r�rDrir8rIrMrc�
__classcell__)r�s@rvr\r\�s���e�R�� �.�.��H�:�1F�F�M��D����1��1�'7�1�	
�1�	�1���"�),����y%�
�y%�'�
y%��y%�
�y%�v�
��
������/3�%�4� ,�4�;?�4�	
�4��4�
�
��
�������������9�v��EI�B3�	
�B3��B3�H���"��
B3�
�B3��
B3�
�B3��B3�N�7%��7%�r�%"��%"�N�#��#�J#3�J:� �&�>'�@h�TF�PID�VC�J:W�x/�l?B�n�n�m4�<�m4�^�X�[�[�
)�F�
�X�[�[�
)�F�
�X�[�[�
)�F�
�X�[�[�
)�F�
�X�[�[�
)�F�
�X�[�[�
)�F�
0��:�
��
�"�@D��#��2=��	����>
�:$��H��H�R�2!�&�F��(�G�(
�Z�/��/�"2�/�	
�/��/�(�.�$(�+�3I� �3I�/7�3I�j�#&���� ���	�
��
�
����;>�SV��'��48��MP��	
������!�S��S��	S�
�S�
�
S�p�����
��
��	
�
�
��

��
�0�@)��)�*-�)�;?�)�	�)�\����"�
�7�:���4I�:�&�:=�B .�L,0�%�
��
�$(�
�;?�
�%)�:�@%)�:�@
� �>�
�&����:.�"
5��*,8�\>C�/�h�&�&������(:�D�;�EI�;�K�@3��@3��	@3�
�@3��
@3�"�@3rxr\r^rYrw)�delegate�	accessors�typ)rrr%r,r3rrrrTc�R�eZdZdZd	d�Zed��Zd
d�Zd
d�Ze	dd��Z
d
d�Zy)�CategoricalAccessoraP
    Accessor object for categorical properties of the Series values.

    Parameters
    ----------
    data : Series or CategoricalIndex

    Examples
    --------
    >>> s = pd.Series(list("abbccc")).astype("category")
    >>> s
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (3, object): ['a', 'b', 'c']

    >>> s.cat.categories
    Index(['a', 'b', 'c'], dtype='object')

    >>> s.cat.rename_categories(list("cba"))
    0    c
    1    b
    2    b
    3    a
    4    a
    5    a
    dtype: category
    Categories (3, object): ['c', 'b', 'a']

    >>> s.cat.reorder_categories(list("cba"))
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (3, object): ['c', 'b', 'a']

    >>> s.cat.add_categories(["d", "e"])
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (5, object): ['a', 'b', 'c', 'd', 'e']

    >>> s.cat.remove_categories(["a", "c"])
    0    NaN
    1      b
    2      b
    3    NaN
    4    NaN
    5    NaN
    dtype: category
    Categories (1, object): ['b']

    >>> s1 = s.cat.add_categories(["d", "e"])
    >>> s1.cat.remove_unused_categories()
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (3, object): ['a', 'b', 'c']

    >>> s.cat.set_categories(list("abcde"))
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (5, object): ['a', 'b', 'c', 'd', 'e']

    >>> s.cat.as_ordered()
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (3, object): ['a' < 'b' < 'c']

    >>> s.cat.as_unordered()
    0    a
    1    b
    2    b
    3    c
    4    c
    5    c
    dtype: category
    Categories (3, object): ['a', 'b', 'c']
    c��|j|�|j|_|j|_|j
|_|j�yr)�	_validater��_parentrk�_indexrl�_name�_freeze)rk�datas  rvr�zCategoricalAccessor.__init__Qs8�����t���{�{����j�j����Y�Y��
����rxc�N�t|jt�std��y)Nz2Can only use .cat accessor with a 'category' dtype)r[rgr!�AttributeError)r�s rvr�zCategoricalAccessor._validateXs"���$�*�*�&6�7� �!U�V�V�8rxc�.�t|j|�Sr)rhr�)rkrls  rv�_delegate_property_getz*CategoricalAccessor._delegate_property_get]s���t�|�|�T�*�*rxc�0�t|j||�Sr)�setattrr�)rkrl�
new_valuess   rv�_delegate_property_setz*CategoricalAccessor._delegate_property_setbs���t�|�|�T�:�6�6rxc�^�ddlm}||jj|j��S)a@
        Return Series of codes as well as the index.

        Examples
        --------
        >>> raw_cate = pd.Categorical(["a", "b", "c", "a"], categories=["a", "b"])
        >>> ser = pd.Series(raw_cate)
        >>> ser.cat.codes
        0   0
        1   1
        2  -1
        3   0
        dtype: int8
        rr!)rk)r�rJr�rar�)rkrJs  rvrazCategoricalAccessor.codeses"�� 	"��d�l�l�(�(����<�<rxc��ddlm}t|j|�}||i|��}|�|||j|j
��Sy)Nrr!)rkrl)r�rJrhr�r�r�)rkrl�argsrVrJrTr�s       rv�_delegate_methodz$CategoricalAccessor._delegate_methodysE��!�����t�,���d�%�f�%���?��#�T�[�[�t�z�z�B�B�rxNrj)rlr�)rerJ)ryrnrorpr��staticmethodr�r�r�rwrar�r�rxrvr�r��
sH��$g�R��W��W�+�
7��=��=�&Crxr�c�<�|j|�}t||�S)z�
    utility routine to turn values into codes given the specified categories

    If `values` is known to be a Categorical, use recode_for_categories instead.
    )r8r)r�r^ras   rvr�r��s!��
�&�&�v�.�E���z�2�2rxc���t|�dk(r|r|j�S|S|j|�r|r|j�S|St|j	|�|�}t||d��}|S)a#
    Convert a set of codes for to a new set of categories

    Parameters
    ----------
    codes : np.ndarray
    old_categories, new_categories : Index
    copy: bool, default True
        Whether to copy if the codes are unchanged.

    Returns
    -------
    new_codes : np.ndarray[np.int64]

    Examples
    --------
    >>> old_cat = pd.Index(['b', 'a', 'c'])
    >>> new_cat = pd.Index(['a', 'b'])
    >>> codes = np.array([0, 1, 1, 2])
    >>> recode_for_categories(codes, old_cat, new_cat)
    array([ 1,  0,  0, -1], dtype=int8)
    rrTr�)rWrSr_rr8r.)ra�old_categoriesrrS�indexerr2s      rvr`r`�sw��2�>��a����:�:�<����	�	�	�~�	.���:�:�<����"��&�&�~�6���G����2�6�I��rxc���ddlm}t|�std��t	|dd�}t|t�r�t|�}tjt|j�|jj��}tj||jd��}||�}|j}||fSt|d�	�}|j}|j}||fS)
az
    Factorize an input `values` into `categories` and `codes`. Preserves
    categorical dtype in `categories`.

    Parameters
    ----------
    values : list-like

    Returns
    -------
    codes : ndarray
    categories : Index
        If `values` has a categorical dtype, then `categories` is
        a CategoricalIndex keeping the categories and order of `values`.
    r)rhzInput must be list-likergNr�Fr6r�)r�rhrrZrhr[r!r4rirmrWr^rargr\r�)r�rhr��	cat_codesr�r^ras       rv�factorize_from_iterabler��s��� (�����1�2�2��V�W�d�
+�F��&�*�+��v�&���I�I�c�&�"3�"3�4�F�L�L�<N�<N�O�	��$�$�Y�f�l�l�U�$�S��%�c�*�
������*����&�%�0���^�^�
��	�	���*��rxc�v�t|�dk(rggfStd�|D��\}}t|�t|�fS)a$
    A higher-level wrapper over `factorize_from_iterable`.

    Parameters
    ----------
    iterables : list-like of list-likes

    Returns
    -------
    codes : list of ndarrays
    categories : list of Indexes

    Notes
    -----
    See `factorize_from_iterable` for more info.
    rc3�2K�|]}t|����y�wr)r�)r��its  rvr�z+factorize_from_iterables.<locals>.<genexpr>�s����N�I�b�5�b�9�I�s�)rWr�r�)�	iterablesrar^s   rv�factorize_from_iterablesr��sB��"�9�~����2�v�
��N�I�N�O��E�:���;��Z�(�(�(rxrl)r�z,Index | Series | ExtensionArray | np.ndarrayr^rIrerdri)rardrSr�rerd)reztuple[np.ndarray, Index])rez$tuple[list[np.ndarray], list[Index]])w�
__future__r�csvr�	functoolsrrz�shutilr�typingrrr	r
r��numpyri�pandas._configr�pandas._libsrr
r�r�pandas._libs.arraysr�pandas.compat.numpyrr
�pandas.util._exceptionsr�pandas.util._validatorsr�pandas.core.dtypes.castrr�pandas.core.dtypes.commonrrrrrrrrrrr�pandas.core.dtypes.dtypesr r!r"r#�pandas.core.dtypes.genericr$r%�pandas.core.dtypes.missingr&r'�pandas.corer(r)r*�pandas.core.accessorr+r,�pandas.core.algorithmsr-r.�pandas.core.arrays._mixinsr/r0�pandas.core.baser1r2r3�pandas.core.common�core�commonr��pandas.core.constructionr4r5�pandas.core.ops.commonr6�pandas.core.sortingr7� pandas.core.strings.object_arrayr8r�r9�collections.abcr:r;r<�pandas._typingr=r>r?r@rArBrCrDrErFrGr�rHrIrJr|r�r\r�r�r`r�r�r�rxrv�<module>r�s���"� ���$�����%���
.�.�4�7����������
��
�����
!� ��<�(�C�%����������F�R06�fZ'3�-�|�=S�Z'3�@O�
�\�9�$=�:���
�	�	�
�XC�.�,�8L�XC�
��"XC�|
3�8�
3��
3��
3�EI�(��(�=A�(��(�V(�V)rx

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists