
    i                       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[d7dZ\	 	 	 	 	 	 	 	 d8dZ]d9dZ^ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  dZm	 	 	 	 d:dZnd;dZoe
d<d       Zpe
d=d       Zp ed       d!        Zpd>d"Zqd?d@d#ZrepZsd$ZtdAd%Zu	 	 	 	 dB	 	 	 	 	 	 	 	 	 	 	 dCd&Zv ed       	 	 	 dD	 	 	 	 	 	 	 dEd'       Zw	 	 	 	 	 dF	 	 	 	 	 	 	 	 	 dGd(Zx	 d?	 	 	 	 	 	 	 dHd)Zy	 	 dI	 	 	 	 	 	 	 dJd*Zz	 dK	 	 	 	 	 	 	 dLd+Z{	 	 	 	 	 dM	 	 	 	 	 	 	 	 	 	 	 	 	 dNd,Z| ed-      	 	 	 dO	 	 	 	 	 dPd.       Z}	 	 dQ	 	 	 	 	 	 	 	 	 dRd/Z~h d0ZdSdTd1Z	 	 	 	 dU	 	 	 	 	 	 	 	 	 	 	 dVd2ZdWd3ZdXd4Z	 	 	 	 	 	 dYd5Z	 d?	 	 	 	 	 dZd6Zy)[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)boundc                   t        | t              st        | d      } t        | j                        rt        t        j                  |             S t        | j                  t              rBt        d|       } | j                  st        | j                        S t        j                  |       S t        | j                  t              rt        d|       } | j                  S t        | j                        rdt        | t        j                         r$t        j                  |       j#                  d      S t        j                  |       j%                  dd      S t'        | j                        rt        j                  |       S t)        | j                        r8| j                  j*                  dv rt-        |       S t        j                  |       S t/        | j                        rt        t        j                   |       S t1        | j                        r-| j#                  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'   rR   r   npasarrayr+   r   _hasna_ensure_data_datar,   codesr   ndarrayviewastyper%   r#   itemsizer   r   r)   object)valuesnpvaluess     S/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pandas/core/algorithms.pyrW   rW   r   s   , fm,vT:v||$RZZ/00	FLL/	2'0}}  --zz&!!	FLL"2	3 mV,||	v||	$fbjj)::f%**733 ::f%,,W5,AA	&,,	'zz&!!		% <<  K/!&))zz&!!	&,,	'BJJ'' 
V\\	*;;t$

H- ZZf-F      c                    t        | t              r| j                  |k(  r| S t        |t        j                        s#|j	                         }|j                  | |      S | j                  |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
    rQ   FrK   )rS   r0   rR   rT   construct_array_type_from_sequencer\   )r_   rR   originalclss       ra   _reconstruct_datarh      sj      &+,1FeRXX& ((*
 !!&!66
 ==U=++rb   c                f   t        | t        t        t        t        j
                  t        f      s|dk7  r$t        | dt        |       j                   d      t        j                  | d      }|dv r(t        | t              rt        |       } t        |       } | S 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)rS   r1   r4   r0   rT   rZ   r3   	TypeErrortype__name__r   infer_dtypetuplelistr   rU   )r_   	func_nameinferreds      ra   _ensure_arraylikery      s     	9/=ST
 &+ F|,,-Q0  ??6%8;;&%(f<VDF M ZZ'FMrb   )
complex128	complex64float64float32uint64uint32uint16rJ   int64int32int16int8ro   r^   c                H    t        |       } t        |       }t        |   }|| fS )z
    Parameters
    ----------
    values : np.ndarray

    Returns
    -------
    htable : HashTable subclass
    values : ndarray
    )rW   _check_object_for_strings_hashtables)r_   ndtyper
   s      ra   _get_hashtable_algor     s-     &!F&v.FF#Ifrb   c                n    | 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
    r^   Frl   ro   )rR   namer   is_string_array)r_   r   s     ra   r   r   &  s7     \\F ve4FMrb   c                     y N r_   s    ra   uniquer   A  s    rb   c                     y r   r   r   s    ra   r   r   C  s    7:rb   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    ra   r   r   G  s    T F##rb   c                    t        |       dk(  ryt        |       } t        j                  | j	                         j                  d            dk7  j                         }|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)lenrW   rT   bincountravelr\   sum)r_   results     ra   nunique_intsr     sO     6{a&!Fkk&,,.//78A=BBDFMrb   c                   t        | d      } t        | j                  t              r| j	                         S t        | t
              r| j	                         S | }t        |       \  }}  |t        |             }|*|j	                  |       }t        ||j                  |      }|S |j	                  | |      \  }}t        ||j                  |      }|J ||j                  d      fS )z?See algorithms.unique for docs. Takes a mask for masked arrays.r   rw   maskbool)
ry   rS   rR   r-   r   r1   r   r   rh   r\   )r_   r   rf   r
   tableuniquess         ra   r   r     s    v:F&,,/}}&(#}}H+F3Ivc&k"E|,,v&#GX^^XF V$7#GX^^XFF+++rb   i@B c                D   t        |       s"t        dt        |       j                   d      t        |      s"t        dt        |      j                   d      t	        |t
        t        t        t        j                  f      sat        |      }t        |d      }t        |      dkD  ro|j                  j                  dv rWt        |       sLt!        ||       s@t#        |      }n4t	        |t$              rt        j&                  |      }nt)        |dd      }t        | d	      }t)        |d
      }t	        |t        j                        s|j+                  |      S t-        |j                        rt/        |      j+                  |      S t-        |j                        r:t1        |j                        s%t        j2                  |j4                  t6              S t-        |j                        rt+        ||j9                  t:                    S t	        |j                  t<              r2t+        t        j>                  |      t        j>                  |            S t        |      t@        kD  rTt        |      dk  rF|j                  t:        k7  r3tC        d |D              s!tE        |      jC                         rd }nZd }nVtG        |j                  |j                        }|j9                  |d      }|j9                  |d      }tH        jJ                  } |||      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 ``rj   r   r   iufcbT)rI   extract_rangeisinrH   rQ      c              3  ,   K   | ]  }|t         u   y wr   r   ).0vs     ra   	<genexpr>zisin.<locals>.<genexpr>9  s     ,AG,s   c                    t        j                  t        j                  | |      j                         t        j                  |             S r   )rT   
logical_orr   r   isnan)cr   s     ra   fzisin.<locals>.f?  s.    }}RWWQ]%8%8%:BHHQKHHrb   c                J    t        j                  | |      j                         S r   )rT   r   r   )abs     ra   <lambda>zisin.<locals>.<lambda>C  s    RWWQ]002 rb   FrK   )&r&   rq   rr   rs   rS   r1   r4   r0   rT   rZ   rv   ry   r   rR   kindr(   r    r   r2   r9   r;   r   r)   pd_arrayr'   zerosshaper   r\   r^   r-   rU   _MINIMUM_COMP_ARR_LENanyr6   r   htableismember)compsr_   orig_valuescomps_arrayr   commons         ra   r   r     s    ((,U(<(<'=Q@
 	
 ((,V(=(='>aA
 	

 fx4ErzzRS6l";.I K!O!!W,+E2"651 =[IF	FM	*&!vTN#EV<K4@Kk2::.''	[..	/$))&11	V\\	*?;CTCT3Uxx))66	V\\	*Kv!677	FLL.	1BJJ{+RZZ-?@@ 	K00K2',V,, <I 3A %V\\;3D3DEvE2!((e(<OO[&!!rb   c                   | }| j                   j                  dv rt        }t        |       \  }}  ||xs t	        |             }|j                  | 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_sentinelna_valuer   	ignore_na)rR   r   r   r   r   	factorizerh   r   )
r_   use_na_sentinel	size_hintr   r   rf   
hash_klassr   r   rY   s
             ra   factorize_arrayr   N  s    H H||D 
 ,V4Jy/CK0E__! % NGU  BG&E'>rb   c                   t        | t        t        f      r| j                  ||      S t	        | d      } | }t        | t
        t        f      r%| j                  | j                  |      \  }}||fS t        | t        j                        s| j                  |      \  }}nt        j                  |       } |s\| j                  t        k(  rIt        |       }|j                         r.t        | j                  d      }t        j                   |||       } t#        | ||      \  }}|r!t%        |      d	kD  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])
    )sortr   r   r   )r   )r   F)compat)r   r   r   T)r   assume_uniqueverify)rS   r1   r4   r   ry   r/   r5   freqrT   rZ   rU   rR   r^   r6   r   r7   wherer   r   	safe_sortrh   )	r_   r   r   r   rf   rY   r   	null_maskr   s	            ra   r   r     sV   P &8Y/0T?KKv=FH 	6,.?@AKK#  ))t)4wg~

+))/)Jw F#6<<6#9
 VI}}-fll5I)Xv>(+
w Gq "+
  BG'>rb   c                l   ddl m}m}m}m}	 t        | dd       }
|rdnd}|ddlm} t        | |      r| j                  } 	  || |d      }|j                  |
      }||_        ||j                  j                            }|j                  j                  d      |_        |j!                         }|r,|j                  dk(  j#                         r|j$                  dd }t'        |      }nwd }t)        |       r> || d      j                  j                  |
      }||_        |
|j                  _        n,t        | t*              rct-        t/        | j0                              } || |      j3                  ||      j5                         }| j6                  |j                  _        nt9        | d      } t;        | |      \  }}}|j<                  t>        j@                  k(  r|j                  t>        jB                        } |||j<                  |
d      }|s<t        | ||	f      r.|jE                  |       r| jF                  | jF                  |_$         ||||d      }|r|jK                  |d      }|r|||z  }|S ||jM                         z  }|S # t        $ r}t        d	      |d }~ww xY w)Nr   )DatetimeIndexrA   rB   TimedeltaIndexr   
proportioncount)cutT)include_lowestz+bins argument only works with numeric data.dropnaintervalFrK   )indexr   )levelr   value_countsr   )rR   r   rL   )r   r   rL   stable)	ascendingr   )'r   r   rA   rB   r   getattrpandas.core.reshape.tiler   rS   _valuesrq   r   r   r   notnar\   
sort_indexallilocr   r!   r2   rv   rangenlevelsgroupbysizenamesry   value_counts_arraylikerR   rT   float16r}   equalsinferred_freqr   sort_valuesr   )r_   r   r   	normalizebinsr   r   rA   rB   r   
index_namer   r   iierrr   normalize_denominatorlevelskeyscounts_idxs                         ra   value_counts_internalr  I  s     .J$<'D0ff%^^F	TVT$7B
 /**,-||**:6""$ v~~*//1[[1%F !$B !%#F+F/77DDFDSFFK *FLL.%/0FV$/vf5 
 "(FLL 'vHF4VVDOD&!zzRZZ'{{2::. DJJZeLC v~'FGJJv&((4 "//F#DuEF##ih#G ,33F M fjjl*FMC  	TIJPSS	Ts   J 	J3"J..J3c                    | }t        |       } t        j                  | ||      \  }}}t        |j                        r|r|t
        k7  }||   ||   }}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   )rW   r   value_countr)   rR   r   rh   )r_   r   r   rf   r   r   
na_counterres_keyss           ra   r   r     sx     H&!F%11&&tLD&*8>>* 4<D:vd|&D x~~x@HVZ''rb   c                H    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]
    )keepr   )rW   r   
duplicated)r_   r	  r   s      ra   r
  r
    s#    2 &!FV$T::rb   c                   t        | d      } | }t        | j                        r)t        |       } t	        d|       } | j                  |      S t        |       } t        j                  | ||      \  }}|0t        j                  |j                  t        j                        }n||fS 	 t        |      }t%        ||j                  |      }||fS # t        $ r,}t        j                   d| t#               	       Y d}~Kd}~ww xY w)
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   NrQ   zUnable to sort modes: )
stacklevel)ry   r)   rR   r:   r   _moderW   r   r  rT   r   r   bool_r   rq   warningswarnr   rh   )r_   r   r   rf   npresultres_maskr   r   s           ra   r  r    s    " v8FH6<<(/7&/||6|**&!FVFFHh88HNN"((;!!
X& xBF8  
$SE*')	
 	

s   +C 	D"DDc           	     
   t        | j                        }t        |       } | j                  dk(  rt	        j
                  | |||||      }|S | j                  dk(  rt	        j                  | ||||||      }|S t        d      )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   	na_optionpctrM   )axisr  r  r   r  r  z&Array with ndim > 2 are not supported.)r)   rR   rW   ndimr	   rank_1drank_2drq   )r_   r  methodr  r   r  r  rankss           ra   rankr     s    > *&,,7O&!F{{a+
* L 
	+
 L @AArb   zpandas.api.extensionsc                >   t        | t        j                  t        t        t
        t        f      s"t        dt        |       j                   d      t        |      }|r+t        || j                  |          t        | ||d|      }|S | j                  ||      }|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 rk   T)r  
allow_fill
fill_value)r  )rS   rT   rZ   r0   r1   r4   r3   rq   rr   rs   r   r<   r   r8   take)arrindicesr  r"  r#  r   s         ra   r$  r$  R  s    b 	&)=ST
 99=c9K9K8LAO
 	

 "'*G#))D/2 !
 M '-Mrb   c                   |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\  j                         r*||j                  k  j                         r| j                  }n|j                  }t        |      r t        t        |j                  |            }n't!        t        t"        |      |      }nt%        |       } | j'                  |||      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.
    iurQ   )sidesorter)r   rS   rT   rZ   rR   r   r$   r%   iinforr   r9   minr   maxr   intr   r   r:   searchsorted)r%  valuer)  r*  r+  	value_arrrR   s          ra   r/  r/    s   ` $V, 	3

#IINNd""25"9 ()3E):BHHeW%	"'')yEII/E.J.J.L IIEOOEeejj/0ET)U35AE -S1 EV<<rb   >   r   r   r   r   r}   r|   c                   t        j                  |      s1t        |      r|j                         st        d      t	        |      }t
        j                  }| j                  }t        |      }|rt        j                  }nt        j                  }t        |t              r| j                         } | j                  }t        | t
        j                        s|t!        | d|j"                   d      rA|dk7  r$t        dt%        |       j"                   d|        || | j'                  |            S t)        t%        |       j"                   d      d}| j                  j*                  dv r*t
        j,                  }| j/                  d	      } t0        }d
}nZ|rt
        j2                  }nG|j*                  dv r9| j                  j4                  dv rt
        j6                  }nt
        j8                  }| j:                  }|dk(  r| j=                  dd      } t        j                  |      }t        j>                  | j@                  |      }	tC        d      gdz  }
|dk\  rtC        d|      ntC        |d      |
|<   ||	tE        |
      <   | j                  j4                  tF        v r$tI        jJ                  | |	t	        |      ||       ntC        d      gdz  }|dk\  rtC        |d      ntC        d|      ||<   tE        |      }tC        d      gdz  }|dkD  rtC        d|       ntC        | d      ||<   tE        |      } || |   | |         |	|<   |r|	j/                  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   rP   Tr(  )r   r   r  r   rQ   NrM   )datetimelikeztimedelta64[ns])&r   r$   r"   
ValueErrorr.  rT   nanrR   r   operatorxorsubrS   r.   to_numpyrZ   hasattrrs   rr   shiftrq   r   r   r[   r   object_r   r}   r|   r  reshapeemptyr   sliceru   _diff_specialr	   diff_2d)r%  nr  narR   is_boolopis_timedelta	orig_ndimout_arr
na_indexer_res_indexerres_indexer_lag_indexerlag_indexers                  ra   diffrO  #  s   , >>!9::F	BIIEE"G\\\\%&lln		c2::&3"R[[M,-qy <S	0B0B/C9TF!STTc399Q<((9%%& 'G G 
 L
yy~~hhtn	

	t	
 99>>..JJEJJEIA~kk"a  HHUOEhhsyy.G+"J)*auT1~U1d^Jt!#GE*
yy~~& 	c7CFD|L d}q(/0AvU1d^5q>TL)d}q(01AU4!_5!T?TL)!#k"2C4DE,,01A~!Q$-Nrb   c                   t        | t        j                  t        t        f      st        d      d}t        | j                  t              s&t        j                  | d      dk(  rt        |       }n"	 | j                         }| j                  |      }||S t%        |      st        d      t'        t        j(                  |            }|s+t+        t-        |             t+        |       k(  st/        d      |Jt1        |       \  }}  |t+        |             }|j3                  |        t'        |j5                  |            }|rD|j                         }	|r#|t+        |        k  |t+        |       k\  z  }
d	||
<   t7        |	|d	
      }net        j8                  t+        |      t:              }|j=                  |t        j>                  t+        |                   |j                  |d      }|t'        |      fS # t
        t        j                  f$ r: | j                  rt        | d   t               rt#        |       }nt        |       }Y w xY w)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 valuesNFrl   rp   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   r#  rQ   wrap)r  ) rS   rT   rZ   r0   r1   rq   rR   r-   r   rt   _sort_mixedargsortr$  decimalInvalidOperationr   ru   _sort_tuplesr&   r   rU   r   r   r5  r   map_locationslookupr8   r?  r.  putarange)r_   rY   r   r   r   r*  orderedr   torder2r   	new_codesreverse_indexers                ra   r   r     s   ` frzz+<hGH/
 	

 F v||^4OOF51_Df%	.^^%Fkk&)G }.
 	
  

5 12EVF^!4F!CGHH~
 18
Fs6{#	 %QXXg%67!S[L(Uc&k-ABDE$KFEb9	((3v;c:FBIIc&k$:; $((V(<	'	222k 7334 
	. {{z&)U; 'v.%f-
	.s   4!G> >AIIc           	     V   t        j                  | D cg c]  }t        |t               c}t              }t        j                  | D cg c]  }t        |       c}t              }| | z  }t        j                  | |         }t        j                  | |         }|j                         d   j                  |      }|j                         d   j                  |      }|j                         d   }	t        j                  |||	g      }
| j                  |
      S c c}w c c}w )z3order ints before strings before nulls in 1d arraysrQ   r   )
rT   r9   rS   strr   r6   rT  nonzeror$  concatenate)r_   xstr_posnull_posnum_posstr_argsortnum_argsortstr_locsnum_locs	null_locslocss              ra   rS  rS    s    hhF;q
1c*;4HGxx&1Qa1>Hh("G**VG_-K**VG_-K #((5H #((5H  "1%I>>8Xy9:D;;t <1s   D!D&c                P    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.constructionrp  pandas.core.sortingrq  )r_   rp  rq  arraysr  indexers         ra   rW  rW  "  s0     =3&$'IFAfT2G'?rb   c                   ddl m} t        | d      }t        |d      }|j                  |d      \  }}t	        j
                  |j                  |j                        } |||j                  dd      }t        | t              r0t        |t              r | j                  |      j                         }n[t        | t              r| j                  } t        |t              r|j                  }t        | |g      }t        |      }t        |      }|j!                  |      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   rQ  r.  )r   rR   rL   )r   rB   r  alignrT   maximumr_   r   rS   r2   appendr   r1   r   r*   r:   reindexrepeat)	lvalsrvalsrB   l_countr_countfinal_countunique_valscombinedrepeatss	            ra   union_with_duplicatesr  1  s    . #E%8G#E%8G}}W};GW**W^^W^^<KGMMUSK%'Jum,Lll5)002eX&MMEeX&MME !%0X&4[A!!+.55G99['**rb   c                Z  	 ddl m} |dvrd| d}t        |      t        |      rt	        |t
              rt        |d      r|		fd}ntddl m} t        |      dk(  r ||t        j                  	      }nGt	        |t
              r/ ||j                          ||j                         d
            }n ||      }t	        |t              rU|dk(  r||j                  j                            }|j                  j!                  |       }t#        |j$                  |      }|S t        |       s| j'                         S | j)                  t*        d
      }|t-        j.                  ||      S t-        j0                  ||t3        |      j5                  t        j6                              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                     S |    S r   )rS   floatrT   r   r6  )re  dict_with_defaults    ra   r   zmap_array.<locals>.<lambda>  s1    0$Q.288A;  DE  rb   rx  rQ   F)tupleize_cols)r   r  rK   r   )r   rA   r5  r   rS   dictr;  rB   r   rT   r|   r_   r   r4   r   r   get_indexerr8   r   rL   r\   r^   r   	map_infermap_infer_maskr6   r[   rJ   )
r%  mapper	na_actionrA   msgrB   rv  
new_valuesr_   r  s
            @ra   	map_arrayr  `  sh   . ((;I;kRo
 Ffd#(F !'F &6{abjj9FD)MMO5e+T  &)$ FLL..01F ,,**3/V^^W5
s8xxz ZZUZ+F}}VV,,!!&&tF|7H7H7RSSrb   )r_   r   return
np.ndarray)r_   r   rR   r   rf   r   r  r   )rw   rb  r  r   )r_   r  r  z)tuple[type[htable.HashTable], np.ndarray])r_   r  r  rb  )r_   rE   r  rE   )r_   znp.ndarray | Seriesr  r  )r_   r   r  r.  r   )r   npt.NDArray[np.bool_] | None)r   r=   r_   r=   r  npt.NDArray[np.bool_])TNNN)r_   r  r   r   r   
int | Noner   r^   r   r  r  z'tuple[npt.NDArray[np.intp], np.ndarray])FTN)r   r   r   r   r   r  r  z%tuple[np.ndarray, np.ndarray | Index])TFFNT)
r   r   r   r   r   r   r   r   r  rB   )r_   r  r   r   r   r  r  z,tuple[ArrayLike, npt.NDArray[np.int64], int])firstN)r_   r   r	  zLiteral['first', 'last', False]r   r  r  r  )TN)r_   r   r   r   r   r  r  z9tuple[np.ndarray, npt.NDArray[np.bool_]] | ExtensionArray)r   averager	  TF)r_   r   r  r   r  rb  r  rb  r   r   r  r   r  znpt.NDArray[np.float64])r   FN)r&  r   r  r   r"  r   )leftN)
r%  r   r0  z$NumpyValueArrayLike | ExtensionArrayr)  zLiteral['left', 'right']r*  zNumpySorter | Noner  znpt.NDArray[np.intp] | np.intp)r   )rC  z&int | float | np.integer | np.floatingr  r   )NTFT)r_   zIndex | ArrayLikerY   znpt.NDArray[np.intp] | Noner   r   r   r   r   r   r  z.AnyArrayLike | tuple[AnyArrayLike, np.ndarray])r  r   )r_   r  r  r  )r~  ArrayLike | Indexr  r  r  r  )r%  r   r  zLiteral['ignore'] | Noner  z#np.ndarray | ExtensionArray | Index)__doc__
__future__r   rU  r7  typingr   r   r   r   r   r  numpyrT   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   rW   rh   ry   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  r   r$  r/  rA  rO  r   rS  rW  r  r  r   rb   ra   <module>r     s  
 #       $   / 4    $ 4   
 1 
 2  

 	5;.?@AK!\!,!,'!,3?!,!,H: ,,**&&&&$$$$$$""""""""  $$&&$.(6 
  
 	 : 
 : Hi$ i$X.,8  " ^"F ! )-;;; ; 	;
 '; -;| H   	y
y y 	y
 +y y| 	[
[ [ 	[ [ [@ LP(( $(,H(1(B -4)-;;
); '; 	;< RV++#+2N+>+` 88
8 8 	8
 8 
8 8@ #$ mm m 	m %mp &,!%	Q=	Q=/Q= #Q= 	Q=
 $Q=p Jlp *. w3w3&w3 w3 	w3
 w3 4w3t,+,+%6,+,+d +/PT	PT (PT )	PTrb   