
    Pi                    Z   d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
 ddlZddlZddlmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZmZ ddlmZ dd	l m!Z! dd
l"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZFmGZG ddlHmIZI ddlJmKZLmMZMmNZN ddlOmPZP er/ddlmQZQmRZRmSZS ddlTmUZUmVZVmWZW ddlXmYZYmZZZ  edeVeUz  eZz            Z[ddZ\dd"Z]dd%Z^ej_        ej`        eja        ejb        ejc        ejd        eje        ejf        ejg        ejh        eji        ejj        ejk        ejl        d&Zmdd(Zndd)Zoe
dd*            Zpe
dd,            Zp ed-          d.             Zpdd0Zqddd3ZrepZsd4Ztdd8Zu	 	 	 	 dddAZv ed-          	 	 	 dddE            Zw	 	 	 	 	 dddJZx	 dddLZy	 	 dddPZz	 dddRZ{	 	 	 	 	 dddZZ| ed[          	 	 	 ddd_            Z}	 	 dddiZ~h djZdddmZ	 	 	 	 dddtZdduZddvZddzZ	 ddd~ZdS )zl
Generic data algorithms. This module is experimental at the moment and not
intended for public consumption
    )annotationsN)TYPE_CHECKINGLiteralTypeVarcastoverload)algos	hashtableiNaTlibNA)AnyArrayLike	ArrayLike
ArrayLikeTAxisIntDtypeObjTakeIndexernpt)
set_module)find_stack_level)'construct_1d_object_array_from_listlikenp_find_common_type)ensure_float64ensure_objectensure_platform_intis_bool_dtypeis_complex_dtypeis_dict_likeis_dtype_equalis_extension_array_dtypeis_floatis_float_dtype
is_integeris_integer_dtypeis_list_likeis_object_dtypeis_signed_integer_dtypeneeds_i8_conversion)concat_compat)BaseMaskedDtypeCategoricalDtypeExtensionDtypeNumpyEADtype)ABCDatetimeArrayABCExtensionArrayABCIndexABCMultiIndexABCNumpyExtensionArray	ABCSeriesABCTimedeltaArray)isnana_value_for_dtype)take_nd)arrayensure_wrapped_if_datetimelikeextract_array)validate_indices)ListLikeNumpySorterNumpyValueArrayLike)CategoricalIndexSeries)BaseMaskedArrayExtensionArrayT)boundvaluesr   return
np.ndarrayc                   t          | t                    st          | d          } t          | j                  r!t          t          j        |                     S t          | j        t                    r?t          d|           } | j
        st          | j                  S t          j        |           S t          | j        t                    rt          d|           } | j        S t          | j                  rjt          | t          j                  r't          j        |                               d          S t          j        |                               dd          S t'          | j                  rt          j        |           S t)          | j                  r1| j        j        dv rt-          |           S t          j        |           S t/          | j                  rt          t          j        |           S t1          | j                  r1|                     d	          }t          t          j        |          }|S t          j        | t2          
          } t          |           S )a  
    routine to ensure that our data is of the correct
    input dtype for lower-level routines

    This will coerce:
    - ints -> int64
    - uint -> uint64
    - bool -> uint8
    - datetimelike -> i8
    - datetime64tz -> i8 (in local tz)
    - categorical -> codes

    Parameters
    ----------
    values : np.ndarray or ExtensionArray

    Returns
    -------
    np.ndarray
    Textract_numpyrC   r@   uint8Fcopy)         i8dtype)
isinstancer2   r;   r'   rU   r   npasarrayr+   r   _hasna_ensure_data_datar,   codesr   ndarrayviewastyper%   r#   itemsizer   r   r)   object)rG   npvaluess     j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/core/algorithms.pyrZ   rZ   r   s!   , fm,, ;vT:::v|$$ -RZ//000	FL/	2	2 *'00} 	.  ---z&!!!	FL"2	3	3 ! mV,,|	v|	$	$ fbj)) 	B:f%%**7333 :f%%,,W5,AAA	&,	'	' z&!!!		%	%  < K//!&)))z&!!!	&,	'	' BJ''' 
V\	*	* ;;t$$
H-- Zf---F       r   rU   r   originalr   c                    t          | t                    r| j        |k    r| S t          |t          j                  s+|                                }|                    | |          S |                     |d          S )z
    reverse of _ensure_data

    Parameters
    ----------
    values : np.ndarray or ExtensionArray
    dtype : np.dtype or ExtensionDtype
    original : AnyArrayLike

    Returns
    -------
    ExtensionArray or np.ndarray
    rT   FrN   )rV   r0   rU   rW   construct_array_type_from_sequencer_   )rG   rU   re   clss       rc   _reconstruct_datarj      s      &+,, 1F1FeRX&& 7 ((**
 !!&!666
 ==U=+++rd   	func_namestrc                   t          | t          t          t          t          j        t          f          s|dk    r't          | dt          |           j	         d          t          j        | d          }|dv r4t          | t                    rt          |           } t          |           } nt	          j        |           } | S )z5
    ensure that we are arraylike if not already
    isin-targetszQ requires a Series, Index, ExtensionArray, np.ndarray or NumpyExtensionArray got .Fskipna)mixedstringmixed-integer)rV   r1   r4   r0   rW   r]   r3   	TypeErrortype__name__r   infer_dtypetuplelistr   rX   )rG   rk   inferreds      rc   _ensure_arrayliker|      s     	9/=ST  (
 && 0 0F||,0 0 0   ?6%888;;;&%(( &f<VDDFFZ''FMrd   )
complex128	complex64float64float32uint64uint32uint16rM   int64int32int16int8rs   ra   )tuple[type[htable.HashTable], np.ndarray]c                `    t          |           } t          |           }t          |         }|| fS )z
    Parameters
    ----------
    values : np.ndarray

    Returns
    -------
    htable : HashTable subclass
    values : ndarray
    )rZ   _check_object_for_strings_hashtables)rG   ndtyper
   s      rc   _get_hashtable_algor     s3     &!!F&v..FF#Ifrd   c                Z    | j         j        }|dk    rt          j        | d          rd}|S )z
    Check if we can use string hashtable instead of object hashtable.

    Parameters
    ----------
    values : ndarray

    Returns
    -------
    str
    ra   Frp   rs   )rU   namer   is_string_array)rG   r   s     rc   r   r   &  s=     \F ve444 	FMrd   c                    d S N rG   s    rc   uniquer   A  s    Crd   np.ndarray | Seriesc                    d S r   r   r   s    rc   r   r   C  s    7:srd   pandasc                     t          |           S )a  
    Return unique values based on a hash table.

    Uniques are returned in order of appearance. This does NOT sort.

    Significantly faster than numpy.unique for long enough sequences.
    Includes NA values.

    Parameters
    ----------
    values : 1d array-like
        The input array-like object containing values from which to extract
        unique values.

    Returns
    -------
    numpy.ndarray, ExtensionArray or NumpyExtensionArray

        The return can be:

        * Index : when the input is an Index
        * Categorical : when the input is a Categorical dtype
        * ndarray : when the input is a Series/ndarray

        Return numpy.ndarray, ExtensionArray or NumpyExtensionArray.

    See Also
    --------
    Index.unique : Return unique values from an Index.
    Series.unique : Return unique values of Series object.

    Examples
    --------
    >>> pd.unique(pd.Series([2, 1, 3, 3]))
    array([2, 1, 3])

    >>> pd.unique(pd.Series([2] + [1] * 5))
    array([2, 1])

    >>> pd.unique(pd.Series([pd.Timestamp("20160101"), pd.Timestamp("20160101")]))
    array(['2016-01-01T00:00:00.000000'], dtype='datetime64[us]')

    >>> pd.unique(
    ...     pd.Series(
    ...         [
    ...             pd.Timestamp("20160101", tz="US/Eastern"),
    ...             pd.Timestamp("20160101", tz="US/Eastern"),
    ...         ],
    ...         dtype="M8[ns, US/Eastern]",
    ...     )
    ... )
    <DatetimeArray>
    ['2016-01-01 00:00:00-05:00']
    Length: 1, dtype: datetime64[ns, US/Eastern]

    >>> pd.unique(
    ...     pd.Index(
    ...         [
    ...             pd.Timestamp("20160101", tz="US/Eastern"),
    ...             pd.Timestamp("20160101", tz="US/Eastern"),
    ...         ],
    ...         dtype="M8[ns, US/Eastern]",
    ...     )
    ... )
    DatetimeIndex(['2016-01-01 00:00:00-05:00'],
            dtype='datetime64[ns, US/Eastern]',
            freq=None)

    >>> pd.unique(np.array(list("baabc"), dtype="O"))
    array(['b', 'a', 'c'], dtype=object)

    An unordered Categorical will return categories in the
    order of appearance.

    >>> pd.unique(pd.Series(pd.Categorical(list("baabc"))))
    ['b', 'a', 'c']
    Categories (3, str): ['a', 'b', 'c']

    >>> pd.unique(pd.Series(pd.Categorical(list("baabc"), categories=list("abc"))))
    ['b', 'a', 'c']
    Categories (3, str): ['a', 'b', 'c']

    An ordered Categorical preserves the category ordering.

    >>> pd.unique(
    ...     pd.Series(
    ...         pd.Categorical(list("baabc"), categories=list("abc"), ordered=True)
    ...     )
    ... )
    ['b', 'a', 'c']
    Categories (3, str): ['a' < 'b' < 'c']

    An array of tuples

    >>> pd.unique(pd.Series([("a", "b"), ("b", "a"), ("a", "c"), ("b", "a")]).values)
    array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object)

    A NumpyExtensionArray of complex

    >>> pd.unique(pd.array([1 + 1j, 2, 3]))
    <NumpyExtensionArray>
    [(1+1j), (2+0j), (3+0j)]
    Length: 3, dtype: complex128
    )unique_with_maskr   s    rc   r   r   G  s    T F###rd   intc                    t          |           dk    rdS t          |           } t          j        |                                                     d                    dk                                    }|S )aH  
    Return the number of unique values for integer array-likes.

    Significantly faster than pandas.unique for long enough sequences.
    No checks are done to ensure input is integral.

    Parameters
    ----------
    values : 1d array-like

    Returns
    -------
    int : The number of unique values in ``values``
    r   intp)lenrZ   rW   bincountravelr_   sum)rG   results     rc   nunique_intsr     sb     6{{aq&!!Fk&,,..//7788A=BBDDFMrd   masknpt.NDArray[np.bool_] | Nonec                   t          | d          } t          | j        t                    r|                                 S t          | t
                    r|                                 S | }t          |           \  }}  |t          |                     }|-|                    |           }t          ||j        |          }|S |                    | |          \  }}t          ||j        |          }|J ||	                    d          fS )z?See algorithms.unique for docs. Takes a mask for masked arrays.r   rk   Nr   bool)
r|   rV   rU   r-   r   r1   r   r   rj   r_   )rG   r   re   r
   tableuniquess         rc   r   r     s   v:::F&,// }}&(## }}H+F33IvIc&kk""E|,,v&&#GX^XFF V$77#GX^XFFF++++rd   i@B compsr=   npt.NDArray[np.bool_]c                   t          |           s%t          dt          |           j         d          t          |          s%t          dt          |          j         d          t	          |t
          t          t          t          j	        f          spt          |          }t          |d          }t          |          dk    r<|j        j        dv r.t          |           st!          ||           st#          |          }n<t	          |t$                    rt          j        |          }nt)          |dd          }t          | d	          }t)          |d
          }t	          |t          j	                  s|                    |          S t-          |j                  r"t/          |                              |          S t-          |j                  r4t1          |j                  s t          j        |j        t6                    S t-          |j                  r(t+          ||                    t:                              S t	          |j        t<                    r4t+          t          j        |          t          j        |                    S t          |          t@          k    ret          |          dk    rR|j        t:          k    rBtC          d |D                       s)tE          |          !                                rd }nXd }nTtG          |j        |j                  }|                    |d          }|                    |d          }tH          j%        } |||          S )z
    Compute the isin boolean array.

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

    Returns
    -------
    ndarray[bool]
        Same length as `comps`.
    zIonly list-like objects are allowed to be passed to isin(), you passed a ``rn   r   r   iufcbT)rL   extract_rangeisinrK   rT      c              3  (   K   | ]}|t           u V  d S r   r   ).0vs     rc   	<genexpr>zisin.<locals>.<genexpr>9  s&      ,,AG,,,,,,rd   c                    t          j        t          j        | |                                          t          j        |                     S r   )rW   
logical_orr   r   isnan)cr   s     rc   fzisin.<locals>.f?  s2    }RWQ]]%8%8%:%:BHQKKHHHrd   c                P    t          j        | |                                          S r   )rW   r   r   )abs     rc   <lambda>zisin.<locals>.<lambda>C  s    RWQ]]0022 rd   FrN   )&r&   ru   rv   rw   rV   r1   r4   r0   rW   r]   rz   r|   r   rU   kindr(   r    r   r2   r9   r;   r   r)   pd_arrayr'   zerosshaper   r_   ra   r-   rX   _MINIMUM_COMP_ARR_LENanyr6   r   htableismember)r   rG   orig_valuescomps_arrayr   commons         rc   r   r     sn     
@(,U(<@ @ @
 
 	
  
A(,V(=A A A
 
 	

 fx4ErzRSS O6ll";.III KK!OO!W,,+E22 -"6511 - =[IIF	FM	*	* O&!!vTNNN#EV<<<K4@@@Kk2:.. A'''	[.	/	/ A$$))&111	V\	*	* A?;CT3U3U Ax)6666	V\	*	* AKv!6!6777	FL.	1	1 ABJ{++RZ-?-?@@@ 	K000KK2'',,V,,,,, (
 << 	3I I I I 32AA %V\;3DEEvE22!((e(<<O1[&!!!rd   Tuse_na_sentinelr   	size_hint
int | Nonena_valuera   'tuple[npt.NDArray[np.intp], np.ndarray]c                   | }| j         j        dv rt          }t          |           \  }}  ||pt	          |                     }|                    | d|||          \  }}	t          ||j         |          }t          |	          }	|	|fS )a(  
    Factorize a numpy array to codes and uniques.

    This doesn't do any coercion of types or unboxing before factorization.

    Parameters
    ----------
    values : ndarray
    use_na_sentinel : bool, default True
        If True, the sentinel -1 will be used for NaN values. If False,
        NaN values will be encoded as non-negative integers and will not drop the
        NaN from the uniques of the values.
    size_hint : int, optional
        Passed through to the hashtable's 'get_labels' method
    na_value : object, optional
        A value in `values` to consider missing. Note: only use this
        parameter when you know that you don't have any values pandas would
        consider missing in the array (NaN for float data, iNaT for
        datetimes, etc.).
    mask : ndarray[bool], optional
        If not None, the mask is used as indicator for missing values
        (True = missing, False = valid) instead of `na_value` or
        condition "val != val".

    Returns
    -------
    codes : ndarray[np.intp]
    uniques : ndarray
    mM)na_sentinelr   r   	ignore_na)rU   r   r   r   r   	factorizerj   r   )
rG   r   r   r   r   re   
hash_klassr   r   r\   s
             rc   factorize_arrayr   N  s    H H|D  
 ,V44JJy/CKK00E__! %  NGU  BBG&&E'>rd   Fsort%tuple[np.ndarray, np.ndarray | Index]c                   t          | t          t          f          r|                     ||          S t	          | d          } | }t          | t
          t          f          r$| j        |                     |          \  }}||fS t          | t          j	                  s|                     |          \  }}nt          j
        |           } |s_| j        t          k    rOt          |           }|                                r,t          | j        d          }t          j        |||           } t#          | ||	          \  }}|r*t%          |          d
k    rt'          |||dd          \  }}t)          ||j        |          }||fS )a1  
    Encode the object as an enumerated type or categorical variable.

    This method is useful for obtaining a numeric representation of an
    array when all that matters is identifying distinct values. `factorize`
    is available as both a top-level function :func:`pandas.factorize`,
    and as a method :meth:`Series.factorize` and :meth:`Index.factorize`.

    Parameters
    ----------
    values : sequence
        A 1-D sequence. Sequences that aren't pandas objects are
        coerced to ndarrays before factorization.
    sort : bool, default False
        Sort `uniques` and shuffle `codes` to maintain the
        relationship.
    use_na_sentinel : bool, default True
        If True, the sentinel -1 will be used for NaN values. If False,
        NaN values will be encoded as non-negative integers and will not drop the
        NaN from the uniques of the values.
    size_hint : int, optional
        Hint to the hashtable sizer.

    Returns
    -------
    codes : ndarray
        An integer ndarray that's an indexer into `uniques`.
        ``uniques.take(codes)`` will have the same values as `values`.
    uniques : ndarray, Index, or Categorical
        The unique valid values. When `values` is Categorical, `uniques`
        is a Categorical. When `values` is some other pandas object, an
        `Index` is returned. Otherwise, a 1-D ndarray is returned.

        .. note::

           Even if there's a missing value in `values`, `uniques` will
           *not* contain an entry for it.

    See Also
    --------
    cut : Discretize continuous-valued array.
    unique : Find the unique value in an array.

    Notes
    -----
    Reference :ref:`the user guide <reshaping.factorize>` for more examples.

    Examples
    --------
    These examples all show factorize as a top-level method like
    ``pd.factorize(values)``. The results are identical for methods like
    :meth:`Series.factorize`.

    >>> codes, uniques = pd.factorize(np.array(["b", "b", "a", "c", "b"], dtype="O"))
    >>> codes
    array([0, 0, 1, 2, 0])
    >>> uniques
    array(['b', 'a', 'c'], dtype=object)

    With ``sort=True``, the `uniques` will be sorted, and `codes` will be
    shuffled so that the relationship is the maintained.

    >>> codes, uniques = pd.factorize(
    ...     np.array(["b", "b", "a", "c", "b"], dtype="O"), sort=True
    ... )
    >>> codes
    array([1, 1, 0, 2, 1])
    >>> uniques
    array(['a', 'b', 'c'], dtype=object)

    When ``use_na_sentinel=True`` (the default), missing values are indicated in
    the `codes` with the sentinel value ``-1`` and missing values are not
    included in `uniques`.

    >>> codes, uniques = pd.factorize(np.array(["b", None, "a", "c", "b"], dtype="O"))
    >>> codes
    array([ 0, -1,  1,  2,  0])
    >>> uniques
    array(['b', 'a', 'c'], dtype=object)

    Thus far, we've only factorized lists (which are internally coerced to
    NumPy arrays). When factorizing pandas objects, the type of `uniques`
    will differ. For Categoricals, a `Categorical` is returned.

    >>> cat = pd.Categorical(["a", "a", "c"], categories=["a", "b", "c"])
    >>> codes, uniques = pd.factorize(cat)
    >>> codes
    array([0, 0, 1])
    >>> uniques
    ['a', 'c']
    Categories (3, str): ['a', 'b', 'c']

    Notice that ``'b'`` is in ``uniques.categories``, despite not being
    present in ``cat.values``.

    For all other pandas objects, an Index of the appropriate type is
    returned.

    >>> cat = pd.Series(["a", "a", "c"])
    >>> codes, uniques = pd.factorize(cat)
    >>> codes
    array([0, 0, 1])
    >>> uniques
    Index(['a', 'c'], dtype='str')

    If NaN is in the values, and we want to include NaN in the uniques of the
    values, it can be achieved by setting ``use_na_sentinel=False``.

    >>> values = np.array([1, 2, 1, np.nan])
    >>> codes, uniques = pd.factorize(values)  # default: use_na_sentinel=True
    >>> codes
    array([ 0,  1,  0, -1])
    >>> uniques
    array([1., 2.])

    >>> codes, uniques = pd.factorize(values, use_na_sentinel=False)
    >>> codes
    array([0, 1, 0, 2])
    >>> uniques
    array([ 1.,  2., nan])
    )r   r   r   r   N)r   )r   F)compat)r   r   r   T)r   assume_uniqueverify)rV   r1   r4   r   r|   r/   r5   freqrW   r]   rX   rU   ra   r6   r   r7   wherer   r   	safe_sortrj   )	rG   r   r   r   re   r\   r   	null_maskr   s	            rc   r   r     s   P &8Y/00 LT?KKKv===FH 	6,.?@AA
K#  ))t)44wg~
++ 
))/)JJww F## 		?6<6#9#9
 VI}} ?-fl5III)Xv>>(+
 
 
w  
Gq  "+
 
 
  BBG'>rd   	ascending	normalizedropnarB   c                   ddl m}m}m}m}	 t          | dd           }
|rdnd}|ddlm} t          | |          r| j	        } 	  || |d          }n"# t          $ r}t          d	          |d }~ww xY w|                    |
          }||_        ||j                                                 }|j                            d          |_        |                                }|r,|j	        dk                                    r|j        dd         }t'          |          }nd }t)          |           r; || d          j	                            |
          }||_        |
|j        _        n4t          | t*                    rgt-          t/          | j                            } || |                              ||                                          }| j        |j        _        nt9          | d          } t;          | |          \  }}}|j        t>          j         k    r|                    t>          j!                  } |||j        |
d          }|s:t          | ||	f          r(|"                    |           r| j#        | j#        |_$         ||||d          }|r|%                    |d          }|r|||z  }n||&                                z  }|S )Nr   )DatetimeIndexrA   rB   TimedeltaIndexr   
proportioncount)cutT)include_lowestz+bins argument only works with numeric data.r   intervalFrN   )indexr   )levelr   value_countsr   )rU   r   rO   )r   r   rO   stable)r   r   )'r   r   rA   rB   r   getattrpandas.core.reshape.tiler   rV   _valuesru   r   r   r   notnar_   
sort_indexallilocr   r!   r2   rz   rangenlevelsgroupbysizenamesr|   value_counts_arraylikerU   rW   float16r   equalsinferred_freqr   sort_valuesr   )rG   r   r   r   binsr   r   rA   rB   r   
index_namer   r   iierrr   normalize_denominatorlevelskeyscounts_idxs                         rc   value_counts_internalr  I  se               ..J$1<<'D000000ff%% 	$^F	TVT$777BB 	T 	T 	TIJJPSS	T //**,,-|**:66""$$  	&v~*//11 	&[1%F !$B !%#F++ #	FVF///7DDFDSSFFK *FL.. 	F%//00FV$///vf55 
 "(FL 'vHHHF4VVDDOD&!zRZ''{{2:.. %DJZeLLLC 0v~'FGG0 JJv&&0 (4 "/VF#DuEEEF H##ih#GG + ,33FFfjjll*FMs   A 
A2A--A2,tuple[ArrayLike, npt.NDArray[np.int64], int]c                    | }t          |           } t          j        | ||          \  }}}t          |j                  r|r|t
          k    }||         ||         }}t          ||j        |          }|||fS )z
    Parameters
    ----------
    values : np.ndarray
    dropna : bool
    mask : np.ndarray[bool] or None, default None

    Returns
    -------
    uniques : np.ndarray
    counts : np.ndarray[np.int64]
    r   )rZ   r   value_countr)   rU   r   rj   )rG   r   r   re   r
  r  
na_counterres_keyss           rc   r   r     s     H&!!F%1&&tLLLD&*8>** 4  	44<D:vd|&D x~x@@HVZ''rd   firstkeepLiteral['first', 'last', False]c                N    t          |           } t          j        | ||          S )ax  
    Return boolean ndarray denoting duplicate values.

    Parameters
    ----------
    values : np.ndarray or ExtensionArray
        Array over which to check for duplicate values.
    keep : {'first', 'last', False}, default 'first'
        - ``first`` : Mark duplicates as ``True`` except for the first
          occurrence.
        - ``last`` : Mark duplicates as ``True`` except for the last
          occurrence.
        - False : Mark all duplicates as ``True``.
    mask : ndarray[bool], optional
        array indicating which elements to exclude from checking

    Returns
    -------
    duplicated : ndarray[bool]
    )r  r   )rZ   r   
duplicated)rG   r  r   s      rc   r  r    s)    2 &!!FV$T::::rd   9tuple[np.ndarray, npt.NDArray[np.bool_]] | ExtensionArrayc                2   t          | d          } | }t          | j                  r5t          |           } t	          d|           } |                     |          S t          |           } t          j        | ||          \  }}|&t          j
        |j        t          j                  }n||fS 	 t          |          }n<# t          $ r/}t          j        d| t#                      	           Y d}~nd}~ww xY wt%          ||j        |          }||fS )
a@  
    Returns the mode(s) of an array.

    Parameters
    ----------
    values : array-like
        Array over which to check for duplicate values.
    dropna : bool, default True
        Don't consider counts of NaN/NaT.

    Returns
    -------
    Union[Tuple[np.ndarray, npt.NDArray[np.bool_]], ExtensionArray]
    moder   rD   r   )r   r   NrT   zUnable to sort modes: )
stacklevel)r|   r)   rU   r:   r   _moderZ   r   r  rW   r   r   bool_r   ru   warningswarnr   rj   )rG   r   r   re   npresultres_maskr  r   s           rc   r  r    sJ   " v888FH6<(( +/77&//||6|***&!!FVFFFFHh8HN"(;;;!!
X&& 
 
 
*S**'))	
 	
 	
 	
 	
 	
 	
 	
 	

 xBBF8s   3C 
C<%C77C<averageaxisr   method	na_optionpctnpt.NDArray[np.float64]c           	        t          | j                  }t          |           } | j        dk    rt	          j        | |||||          }n6| j        dk    rt	          j        | ||||||          }nt          d          |S )a  
    Rank the values along a given axis.

    Parameters
    ----------
    values : np.ndarray or ExtensionArray
        Array whose values will be ranked. The number of dimensions in this
        array must not exceed 2.
    axis : int, default 0
        Axis over which to perform rankings.
    method : {'average', 'min', 'max', 'first', 'dense'}, default 'average'
        The method by which tiebreaks are broken during the ranking.
    na_option : {'keep', 'top'}, default 'keep'
        The method by which NaNs are placed in the ranking.
        - ``keep``: rank each NaN value with a NaN ranking
        - ``top``: replace each NaN with either +/- inf so that they
                   there are ranked at the top
    ascending : bool, default True
        Whether or not the elements should be ranked in ascending order.
    pct : bool, default False
        Whether or not to the display the returned rankings in integer form
        (e.g. 1, 2, 3) or in percentile form (e.g. 0.333..., 0.666..., 1).
       )is_datetimeliketies_methodr   r&  r'  rP   )r$  r+  r,  r   r&  r'  z&Array with ndim > 2 are not supported.)r)   rU   rZ   ndimr	   rank_1drank_2dru   )rG   r$  r%  r&  r   r'  r+  rankss           rc   rankr1    s    > *&,77O&!!F{a+
 
 
 
		+
 
 
 @AAALrd   zpandas.api.extensionsindicesr   
allow_fillc                f   t          | t          j        t          t          t
          t          f          s%t          dt          |           j	         d          t          |          }|r0t          || j        |                    t          | ||d|          }n|                     ||          }|S )a  
    Take elements from an array.

    Parameters
    ----------
    arr : numpy.ndarray, ExtensionArray, Index, or Series
        Input array.
    indices : sequence of int or one-dimensional np.ndarray of int
        Indices to be taken.
    axis : int, default 0
        The axis over which to select values.
    allow_fill : bool, default False
        How to handle negative values in `indices`.

        * False: negative values in `indices` indicate positional indices
          from the right (the default). This is similar to :func:`numpy.take`.

        * True: negative values in `indices` indicate
          missing values. These values are set to `fill_value`. Any other
          negative values raise a ``ValueError``.

    fill_value : any, optional
        Fill value to use for NA-indices when `allow_fill` is True.
        This may be ``None``, in which case the default NA value for
        the type (``self.dtype.na_value``) is used.

        For multi-dimensional `arr`, each *element* is filled with
        `fill_value`.

    Returns
    -------
    ndarray or ExtensionArray
        Same type as the input.

    Raises
    ------
    IndexError
        When `indices` is out of bounds for the array.
    ValueError
        When the indexer contains negative values other than ``-1``
        and `allow_fill` is True.

    Notes
    -----
    When `allow_fill` is False, `indices` may be whatever dimensionality
    is accepted by NumPy for `arr`.

    When `allow_fill` is True, `indices` should be 1-D.

    See Also
    --------
    numpy.take : Take elements from an array along an axis.

    Examples
    --------
    >>> import pandas as pd

    With the default ``allow_fill=False``, negative numbers indicate
    positional indices from the right.

    >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1])
    array([10, 10, 30])

    Setting ``allow_fill=True`` will place `fill_value` in those positions.

    >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True)
    array([10., 10., nan])

    >>> pd.api.extensions.take(
    ...     np.array([10, 20, 30]), [0, 0, -1], allow_fill=True, fill_value=-10
    ... )
    array([ 10,  10, -10])
    zkpd.api.extensions.take requires a numpy.ndarray, ExtensionArray, Index, Series, or NumpyExtensionArray got ro   T)r$  r3  
fill_value)r$  )rV   rW   r]   r0   r1   r4   r3   ru   rv   rw   r   r<   r   r8   take)arrr2  r$  r3  r5  r   s         rc   r6  r6  R  s    b 	&)=ST  

 O9=c9KO O O
 
 	

 "'**G .#)D/222 !
 
 
 '--Mrd   leftr7  value$NumpyValueArrayLike | ExtensionArraysideLiteral['left', 'right']sorterNumpySorter | Nonenpt.NDArray[np.intp] | np.intpc                   |t          |          }t          | t          j                  r)| j        j        dv rt          |          st          |          rt          j        | j        j	                  }t          |          rt          j
        |g          nt          j
        |          }||j        k                                    r%||j        k                                    r| j        }n|j        }t          |          r)t          t          |	                    |                    }n4t!          t          t"          |          |          }nt%          |           } |                     |||          S )a  
    Find indices where elements should be inserted to maintain order.

    Find the indices into a sorted array `arr` (a) such that, if the
    corresponding elements in `value` were inserted before the indices,
    the order of `arr` would be preserved.

    Assuming that `arr` is sorted:

    ======  ================================
    `side`  returned index `i` satisfies
    ======  ================================
    left    ``arr[i-1] < value <= self[i]``
    right   ``arr[i-1] <= value < self[i]``
    ======  ================================

    Parameters
    ----------
    arr: np.ndarray, ExtensionArray, Series
        Input array. If `sorter` is None, then it must be sorted in
        ascending order, otherwise `sorter` must be an array of indices
        that sort it.
    value : array-like or scalar
        Values to insert into `arr`.
    side : {'left', 'right'}, optional
        If 'left', the index of the first suitable location found is given.
        If 'right', return the last such index.  If there is no suitable
        index, return either 0 or N (where N is the length of `self`).
    sorter : 1-D array-like, optional
        Optional array of integer indices that sort array a into ascending
        order. They are typically the result of argsort.

    Returns
    -------
    array of ints or int
        If value is array-like, array of insertion points.
        If value is scalar, a single integer.

    See Also
    --------
    numpy.searchsorted : Similar method from NumPy.
    NiurT   )r;  r=  )r   rV   rW   r]   rU   r   r$   r%   iinforv   r9   minr   maxr   r   r   r   r:   searchsorted)r7  r9  r;  r=  rB  	value_arrrU   s          rc   rE  rE    s\   ` $V,, 	3
##2INd"" #"25"9"9 # (()3E):):OBHeW%%%	"'')) 	$yEI/E.J.J.L.L 	$ IEEOEe 	Bejj//00EET)U335AAAEE -S11 EV<<<rd   >   r   r   r   r   r   r   n&int | float | np.integer | np.floatingc                0   t          j        |          sAt          |          r|                                st          d          t	          |          }t
          j        }| j        }t          |          }|rt          j
        }nt          j        }t          |t                    r|                                 } | j        }t          | t
          j                  st!          | d|j         d          rL|dk    r't          dt%          |           j         d|            || |                     |                    S t)          t%          |           j         d          d}| j        j        dv r+t
          j        }|                     d	          } t0          }d
}n?|rt
          j        }n0|j        dv r'| j        j        dv rt
          j        }nt
          j        }| j        }|dk    r|                     dd          } t          j        |          }t          j        | j         |          }	tC          d          gdz  }
|dk    rtC          d|          ntC          |d          |
|<   ||	tE          |
          <   | j        j        tF          v r'tI          j%        | |	t	          |          ||           ntC          d          gdz  }|dk    rtC          |d          ntC          d|          ||<   tE          |          }tC          d          gdz  }|dk    rtC          d|           ntC          | d          ||<   tE          |          } || |         | |                   |	|<   |r|	                    d          }	|dk    r|	dddf         }	|	S )aQ  
    difference of n between self,
    analogous to s-s.shift(n)

    Parameters
    ----------
    arr : ndarray or ExtensionArray
    n : int
        number of periods
    axis : {0, 1}
        axis to shift on
    stacklevel : int, default 3
        The stacklevel for the lost dtype warning.

    Returns
    -------
    shifted
    zperiods must be an integer__r   zcannot diff z	 on axis=zK has no 'diff' method. Convert to a suitable dtype prior to calling 'diff'.Fr   rS   TrA  )r   r   r*  r   rT   NrP   )datetimelikeztimedelta64[ns])&r   r$   r"   
ValueErrorr   rW   nanrU   r   operatorxorsubrV   r.   to_numpyr]   hasattrrw   rv   shiftru   r   r   r^   r   object_r   r   r   r-  reshapeemptyr   slicery   _diff_specialr	   diff_2d)r7  rG  r$  narU   is_boolopis_timedelta	orig_ndimout_arr
na_indexer_res_indexerres_indexer_lag_indexerlag_indexers                  rc   diffre  #  s   , >!  	; 	;9:::FF	BIEE""G \\%&& llnn	c2:&& 
3,R[,,,-- 	qyy !SS		0B!S!ST!S!STTT2c399Q<<(((99% G G G  
 L
y~hhtnn	 
	t		
 9>...JEEJEIA~~kk"a   HUOOEhsy...G++"J)*auT1~~~U1d^^Jt!#GE*
y~&& 	c7CFFD|LLLLL d}q(/0AvvU1d^^^5q>>TL))d}q(01AU4!___5!T??TL))!r#k"2C4DEE 2,,011A~~!!!Q$-Nrd   Index | ArrayLiker\   npt.NDArray[np.intp] | Noner   r   .AnyArrayLike | tuple[AnyArrayLike, np.ndarray]c                   t          | t          j        t          t          f          st          d          d}t          | j        t                    s*t          j	        | d          dk    rt          |           }n	 |                                 }|                     |          }n]# t
          t          j        f$ rD | j        r+t          | d         t                     rt#          |           }nt          |           }Y nw xY w||S t%          |          st          d          t'          t          j        |                    }|s<t+          t-          |                     t+          |           k    st/          d          |at1          |           \  }}  |t+          |                     }|                    |            t'          |                    |                    }|rV|                                }	|r-|t+          |            k     |t+          |           k    z  }
d	||
<   t7          |	|d	
          }ntt          j        t+          |          t:                    }|                    |t          j        t+          |                               |                    |d          }|t'          |          fS )a  
    Sort ``values`` and reorder corresponding ``codes``.

    ``values`` should be unique if ``codes`` is not None.
    Safe for use with mixed types (int, str), orders ints before strs.

    Parameters
    ----------
    values : list-like
        Sequence; must be unique if ``codes`` is not None.
    codes : np.ndarray[intp] or None, default None
        Indices to ``values``. All out of bound indices are treated as
        "not found" and will be masked with ``-1``.
    use_na_sentinel : bool, default True
        If True, the sentinel -1 will be used for NaN values. If False,
        NaN values will be encoded as non-negative integers and will not drop the
        NaN from the uniques of the values.
    assume_unique : bool, default False
        When True, ``values`` are assumed to be unique, which can speed up
        the calculation. Ignored when ``codes`` is None.
    verify : bool, default True
        Check if codes are out of bound for the values and put out of bound
        codes equal to ``-1``. If ``verify=False``, it is assumed there
        are no out of bound codes. Ignored when ``codes`` is None.

    Returns
    -------
    ordered : AnyArrayLike
        Sorted ``values``
    new_codes : ndarray
        Reordered ``codes``; returned when ``codes`` is not None.

    Raises
    ------
    TypeError
        * If ``values`` is not list-like or if ``codes`` is neither None
        nor list-like
        * If ``values`` cannot be sorted
    ValueError
        * If ``codes`` is not None and ``values`` contain duplicates.
    zbOnly np.ndarray, ExtensionArray, and Index objects are allowed to be passed to safe_sort as valuesNFrp   rt   r   zMOnly list-like objects or None are allowed to be passed to safe_sort as codesz,values should be unique if codes is not Noner   r5  rT   wrap)r  ) rV   rW   r]   r0   r1   ru   rU   r-   r   rx   _sort_mixedargsortr6  decimalInvalidOperationr   ry   _sort_tuplesr&   r   rX   r   r   rL  r   map_locationslookupr8   rV  r   putarange)rG   r\   r   r   r   r=  orderedr   torder2r   	new_codesreverse_indexers                rc   r   r     s   ` frz+<hGHH 
/
 
 	

 F v|^44.OF5111_DDf%%	.^^%%Fkk&))GG734 
	. 
	. 
	. { .z&)U;; . 'v..%f--
	. } 
.
 
 	
  
5 1 122E IVF^^!4!4F!C!CGHHH~
 188
FJs6{{##	 %QXXg%6%677 =!! 	S[[L(Uc&kk-ABDE$KFEb999		(3v;;c:::FBIc&kk$:$:;;; $((V(<<	'	2222s   >)B( (ADDc                L   t          j        d | D             t                    }t          j        d | D             t                    }| | z  }t          j        | |                   }t          j        | |                   }|                                d                             |          }|                                d                             |          }|                                d         }t          j        |||g          }	|                     |	          S )z3order ints before strings before nulls in 1d arraysc                8    g | ]}t          |t                    S r   )rV   rl   r   xs     rc   
<listcomp>z_sort_mixed.<locals>.<listcomp>  s"    ;;;q
1c**;;;rd   rT   c                ,    g | ]}t          |          S r   )r6   r|  s     rc   r~  z_sort_mixed.<locals>.<listcomp>  s    111Qa111rd   r   )rW   r9   r   rm  nonzeror6  concatenate)
rG   str_posnull_posnum_posstr_argsortnum_argsortstr_locsnum_locs	null_locslocss
             rc   rl  rl    s    h;;F;;;4HHHGx11&111>>>Hh("G*VG_--K*VG_--K  #((55H  #((55H  ""1%I>8Xy9::D;;trd   c                b    ddl m} ddlm}  || d          \  }} ||d          }| |         S )a  
    Convert array of tuples (1d) to array of arrays (2d).
    We need to keep the columns separately as they contain different types and
    nans (can't use `np.sort` as it may fail when str and nan are mixed in a
    column as types cannot be compared).
    r   )	to_arrays)lexsort_indexerNT)orders)"pandas.core.internals.constructionr  pandas.core.sortingr  )rG   r  r  arraysr  indexers         rc   rp  rp  "  s[     =<<<<<333333	&$''IFAofT222G'?rd   lvalsArrayLike | Indexrvalsc                   ddl m} t          | d          }t          |d          }|                    |d          \  }}t	          j        |j        |j                  } |||j        dd          }t          | t                    r=t          |t                    r(| 
                    |                                          }ngt          | t                    r| j        } t          |t                    r|j        }t          | |g          }t          |          }t          |          }|                    |          j        }t	          j        ||          S )a  
    Extracts the union from lvals and rvals with respect to duplicates and nans in
    both arrays.

    Parameters
    ----------
    lvals: np.ndarray or ExtensionArray
        left values which is ordered in front.
    rvals: np.ndarray or ExtensionArray
        right values ordered after lvals.

    Returns
    -------
    np.ndarray or ExtensionArray
        Containing the unsorted union of both arrays.

    Notes
    -----
    Caller is responsible for ensuring lvals.dtype == rvals.dtype.
    r   rB   Fr   rj  r   )r   rU   rO   )r   rB   r  alignrW   maximumrG   r   rV   r2   appendr   r1   r   r*   r:   reindexrepeat)	r  r  rB   l_countr_countfinal_countunique_valscombinedrepeatss	            rc   union_with_duplicatesr  1  sN   . #E%888G#E%888G}}W};;GW*W^W^<<K&GMUSSSK%'' BJum,L,L Bll5))0022eX&& 	"MEeX&& 	"ME !%00X&&4[AA!!+..5G9['***rd   	na_actionLiteral['ignore'] | None#np.ndarray | ExtensionArray | Indexc                  	 ddl m} |dvrd| d}t          |          t          |          rt	          |t
                    rt          |d          r|		fd}nddl m} t          |          dk    r ||t          j
        	          }n]t	          |t
                    r= ||                                 ||                                d
                    }n ||          }t	          |t                    rV|dk    r||j                                                 }|j                            |           }t#          |j        |          }|S t          |           s|                                 S |                     t*          d
          }|t-          j        ||          S t-          j        ||t3          |                              t          j                            S )a!  
    Map values using an input mapping or function.

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

    Returns
    -------
    Union[ndarray, Index, ExtensionArray]
        The output of the mapping function applied to the array.
        If the function returns a tuple with more than one element
        a MultiIndex will be returned.
    r   )rA   )Nignorez+na_action must either be 'ignore' or None, z was passed__missing__c                ~    t          | t                    r t          j        |           rt          j        n|          S r   )rV   floatrW   r   rM  )r}  dict_with_defaults    rc   r   zmap_array.<locals>.<lambda>  s2    0$Q..E28A;;EA  rd   r  rT   F)tupleize_cols)r   r  rN   Nr   )r   rA   rL  r   rV   dictrR  rB   r   rW   r   rG   r
  r4   r   r   get_indexerr8   r   rO   r_   ra   r   	map_infermap_infer_maskr6   r^   rM   )
r7  mapperr  rA   msgrB   r  
new_valuesrG   r  s
            @rc   	map_arrayr  `  s
   . (((RIRRRoo
 F (fd## 	((F(F 	( !'   FF &%%%%%6{{abj999FD)) (MMOO55e+T+T+T    &)$$ 	  FL..001F ,**3//V^W55
s88 xxzz ZZUZ++F}VV,,,!&&tF||7H7H7R7RSSSSrd   )rG   r   rH   rI   )rG   r   rU   r   re   r   rH   r   )rk   rl   rH   r   )rG   rI   rH   r   )rG   rI   rH   rl   )rG   rE   rH   rE   )rG   r   rH   rI   )rG   r   rH   r   r   )r   r   )r   r=   rG   r=   rH   r   )TNNN)rG   rI   r   r   r   r   r   ra   r   r   rH   r   )FTN)r   r   r   r   r   r   rH   r   )TFFNT)
r   r   r   r   r   r   r   r   rH   rB   )rG   rI   r   r   r   r   rH   r  )r  N)rG   r   r  r  r   r   rH   r   )TN)rG   r   r   r   r   r   rH   r  )r   r#  r  TF)rG   r   r$  r   r%  rl   r&  rl   r   r   r'  r   rH   r(  )r   FN)r2  r   r$  r   r3  r   )r8  N)
r7  r   r9  r:  r;  r<  r=  r>  rH   r?  )r   )rG  rH  r$  r   )NTFT)rG   rf  r\   rg  r   r   r   r   r   r   rH   rh  )rH   r   )rG   rI   rH   rI   )r  r  r  r  rH   r  )r7  r   r  r  rH   r  )__doc__
__future__r   rn  rN  typingr   r   r   r   r   r  numpyrW   pandas._libsr	   r
   r   r   r   pandas._libs.missingr   pandas._typingr   r   r   r   r   r   r   pandas.util._decoratorsr   pandas.util._exceptionsr   pandas.core.dtypes.castr   r   pandas.core.dtypes.commonr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   pandas.core.dtypes.concatr*   pandas.core.dtypes.dtypesr+   r,   r-   r.   pandas.core.dtypes.genericr/   r0   r1   r2   r3   r4   r5   pandas.core.dtypes.missingr6   r7   pandas.core.array_algos.taker8   pandas.core.constructionr9   r   r:   r;   pandas.core.indexersr<   r=   r>   r?   r   r@   rA   rB   pandas.core.arraysrC   rD   rE   rZ   rj   r|   Complex128HashTableComplex64HashTableFloat64HashTableFloat32HashTableUInt64HashTableUInt32HashTableUInt16HashTableUInt8HashTableInt64HashTableInt32HashTableInt16HashTableInt8HashTableStringHashTablePyObjectHashTabler   r   r   r   r   r   unique1dr   r   r   r   r  r   r  r  r1  r6  rE  rX  re  r   rl  rp  r  r  r   rd   rc   <module>r     s   
 # " " " " "                                $ # # # # #                  / . . . . . 4 4 4 4 4 4                                          $ 4 3 3 3 3 3                                   
 1 0 0 0 0 0         
 2 1 1 1 1 1 A                  
       
 	5;.?@@@AK! K! K! K!\!, !, !, !,H   : ,*&&$$$"""" $& $   (   6 
    
 	 : : : 
 : Hi$ i$ i$X   ., , , , ,8  " ^" ^" ^" ^"F ! )-; ; ; ; ;| H   	y y y y y| 	[ [ [ [ [@ LP( ( ( ( (B -4)-; ; ; ; ;< RV+ + + + +` 8 8 8 8 8@ #$$ m m m m %$mp &,!%	Q= Q= Q= Q= Q=p JIIl l l l lp *. w3 w3 w3 w3 w3t      ,+ ,+ ,+ ,+d +/PT PT PT PT PT PT PTrd   