
    Owg)D                       d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	m
Z
mZmZ ddlZddlm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 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' ddl(m)Z) erddl*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 d Z2d Z3dAdZ4d Z5d Z6dBdZ7dBdZ8dBdZ9dBdZ:dCdZ;e	 dD	 	 	 	 	 dEd       Z<edDdFd       Z<dGdFdZ<	 dG	 	 	 	 	 dHdZ=d Z>dIdZ?dBd Z@dBd!ZAdJd"ZBdKd#ZCd$ ZDd% ZEd& ZFedLd'       ZGe	 	 	 	 dMd(       ZGdGdNd)ZG	 	 	 	 dOd*ZHd+ ZI	 	 	 	 dPd,ZJej                  	 dQ	 	 	 	 	 dRd-       ZLdSd.ZMej                  ej                  ej                  ej                  j                  ej                  ej                  j                  iZTej                  d/ej                  d0ej                  d1iZUi ej                  d2ej                  d3ej                  d4ej                  d5ej                  d6ej                  d2ej                  d2ej                  d7ej                  d7ej                  d8ej                  d8ej                  d9ej                  d9ej                  d:ej                  d:ej                  d;ej                  d;ej                  d3ej                  d3ej                  d4ej                  d4ej                  d<ej                  d<ej                  d=ej                  d=iZidTd>Zjd? ZkdUd@Zly)Vzh
Misc tools for implementing data structures

Note: pandas.core.common is *not* part of the public API.
    )annotationsN)abcdefaultdict)
Collection	GeneratorHashableIterableSequence)partial)TYPE_CHECKINGAnyCallablecastoverload)lib)np_version_gte1p24)'construct_1d_object_array_from_listlike)is_bool_dtype
is_integer)ABCExtensionArrayABCIndexABCMultiIndex	ABCSeries)iterable_not_string)AnyArrayLike	ArrayLikeNpDtypeRandomStateT)Indexc              #  d   K   | D ]%  }t        |      rt        |      E d{    "| ' y7 w)a  
    Flatten an arbitrarily nested sequence.

    Parameters
    ----------
    line : sequence
        The non string sequence to flatten

    Notes
    -----
    This doesn't consider strings sequences.

    Returns
    -------
    flattened : generator
    N)r   flatten)lineelements     I/var/www/horilla/myenv/lib/python3.12/site-packages/pandas/core/common.pyr"   r"   >   s5     "  w'w'''M	's   0.0c                ~    | d   j                   }| dd  D ]  }	 |j                   |k7  rd } |S # t        $ r d }Y &w xY w)Nr      )name
ValueError)objsr(   objs      r%   consensus_name_attrr,   V   sX    7<<DABx 	xx4 K  	D	s   .<<c                   t        | t        t        j                  t        t
        f      rt        | t              s| j                  t        j                  k(  rPt        j                  |       }t        j                  |      s%d}t        j                  |d      rt        |      yyt        | j                        ryyt        | t              r?t        |       dkD  r1t!        |       t        urt        |       } t        j"                  |       S y)a  
    Check whether `key` is a valid boolean indexer.

    Parameters
    ----------
    key : Any
        Only list-likes may be considered boolean indexers.
        All other types are not considered a boolean indexer.
        For array-like input, boolean ndarrays or ExtensionArrays
        with ``_is_boolean`` set are considered boolean indexers.

    Returns
    -------
    bool
        Whether `key` is a valid boolean indexer.

    Raises
    ------
    ValueError
        When the array is an object-dtype ndarray or ExtensionArray
        and contains missing values.

    See Also
    --------
    check_array_indexer : Check that `key` is a valid array to index,
        and convert to an ndarray.
    z=Cannot mask with non-boolean array containing NA / NaN valuesT)skipnaFr   )
isinstancer   npndarrayr   r   r   dtypeobject_asarrayr   is_bool_arrayr)   r   listlentypeis_bool_list)key	key_arrayna_msgs      r%   is_bool_indexerr=   a   s    8 iX/@Am,99

"

3I$$Y/X$$Yt< %V,,399%  
C	s8a<Cy$3i##C((    c                f    t        j                  |       r| j                         rt        d      | S )z
    Disallow indexing with a float key, even if that key is a round number.

    Parameters
    ----------
    val : scalar

    Returns
    -------
    outval : scalar
    zYIndexing with a float is no longer supported. Manually convert to an integer key instead.)r   is_floatr   
IndexError)vals    r%   cast_scalar_indexerrC      s2     ||CS^^-)
 	

 Jr>   c                     d | D        S )zL
    Returns a generator consisting of the arguments that are not None.
    c              3  &   K   | ]	  }||  y wN .0args     r%   	<genexpr>znot_none.<locals>.<genexpr>   s     3C3?C3s   rG   argss    r%   not_nonerN      s     4433r>   c                 &    t        d | D              S )z?
    Returns a boolean indicating if any argument is None.
    c              3  $   K   | ]  }|d u  
 y wrF   rG   rH   s     r%   rK   zany_none.<locals>.<genexpr>        +ssd{+   anyrL   s    r%   any_nonerU           +d+++r>   c                 &    t        d | D              S )zA
    Returns a boolean indicating if all arguments are None.
    c              3  $   K   | ]  }|d u  
 y wrF   rG   rH   s     r%   rK   zall_none.<locals>.<genexpr>   rQ   rR   allrL   s    r%   all_noner[      rV   r>   c                 &    t        d | D              S )zC
    Returns a boolean indicating if any argument is not None.
    c              3  $   K   | ]  }|d u 
 y wrF   rG   rH   s     r%   rK   zany_not_none.<locals>.<genexpr>        /3s$/rR   rS   rL   s    r%   any_not_noner_           /$///r>   c                 &    t        d | D              S )zE
    Returns a boolean indicating if all arguments are not None.
    c              3  $   K   | ]  }|d u 
 y wrF   rG   rH   s     r%   rK   zall_not_none.<locals>.<genexpr>   r^   rR   rY   rL   s    r%   all_not_nonerc      r`   r>   c                 &    t        d | D              S )z;
    Returns the count of arguments that are not None.
    c              3  $   K   | ]  }|d u 
 y wrF   rG   )rI   xs     r%   rK   z!count_not_none.<locals>.<genexpr>   s     +q}+rR   )sumrL   s    r%   count_not_nonerh      rV   r>   c                     y rF   rG   valuesr2   s     r%   asarray_tuplesaferl      s     r>   c                     y rF   rG   rj   s     r%   rl   rl          r>   c                .   t        | t        t        f      st        | d      st        |       } n8t        | t              r| j
                  S t        | t              r| j
                  S t        | t              r#|t        j                  t        fv rt        |       S 	 t        j                         5  t        s$t        j                  dt        j                         t        j                   | |      }d d d        t%        j&                  j(                  t*              rt        j                   | t              }|j,                  dk(  r#| D cg c]  }t        |       } }t        |       }|S # 1 sw Y   |xY w# t"        $ r t        |       cY S w xY wc c}w )N	__array__ignorer2      )r/   r6   tuplehasattrr   _valuesr   r0   r3   objectr   warningscatch_warningsr   simplefilterVisibleDeprecationWarningr4   r)   
issubclassr2   r8   strndim)rk   r2   resultrf   s       r%   rl   rl      s7   ve}-1Mf	FH	%~~	FI	&~~&$Ebjj&-A$A6v>>?$$& 	5%%%h0L0LMZZe4F		5 &,,##S)F&1{{a$*+q%(++8@M)	5 	5
  ?
 7v>>? ,s1   E8 /AE,1E8 F,E51E8 8FFc                    t        | t        t        f      r| g} t        | t        t        j
                  f      s	 t        |       } t        | |      } | S # t        $ r | g} Y w xY w)z
    Transform label or iterable of labels to array, for use in Index.

    Parameters
    ----------
    dtype : dtype
        If specified, use as dtype of the resulting array, otherwise infer.

    Returns
    -------
    array
    rr   )r/   r}   rt   r6   r0   r1   	TypeErrorrl   )labelsr2   s     r%   index_labels_to_arrayr   
  se     &3,'ftRZZ01	&\F vU3FM  	XF	s   A A$#A$c                <    | t        | t        t        f      s| gS | S rF   )r/   rt   r6   r+   s    r%   maybe_make_listr   '  s     
z#t}=uJr>   c                    t        | t        j                        r%t        | t        j                        st	        |       S t        t        |       } | S )zB
    If obj is Iterable but not list-like, consume into list.
    )r/   r   r	   Sizedr6   r   r   r   s    r%   maybe_iterable_to_listr   -  s:     #s||$ZSYY-GCy
z3
CJr>   c                    t        | t              xr. | j                  du xr | j                  du xr | j                  du S )z
    We have a null slice.
    Nr/   slicestartstopstepr   s    r%   is_null_slicer   7  sH    
 	3 	II	HH	 HH	r>   c                    t        | t              xr9 | j                  duxr) | j                  duxr | j                  | j                  k(  S )z>
    We have an empty slice, e.g. no values are selected.
    N)r/   r   r   r   r   s    r%   is_empty_slicer   C  sL    
 	3 	"IIT!	"HHD 	" II!	r>   c                d    | D cg c]   }t        |t              xr t        |       " c}S c c}w )zX
    Find non-trivial slices in "line": return a list of booleans with same length.
    )r/   r   r   )r#   ks     r%   is_true_slicesr   O  s1     DHHaJq% 9q)9%99HHHs   %-c                    t        | t              xr0 | j                  dk(  xr | j                  |k(  xr | j                  du S )z&
    We have a full length slice.
    r   Nr   )r+   r#   s     r%   is_full_slicer   W  sG    
 	3 	IIN	HH	 HH	r>   c                    t        | d      rt        | d      S t        | t              rt	        | j
                        S t        |       rt        |       j                  S y )N__name__)	ru   getattrr/   r   get_callable_namefunccallabler8   r   r   s    r%   r   r   c  sN    sJsJ''#w **}Cy!!!
 r>   c                .    t        |       r	 | |fi |S | S )z
    Evaluate possibly callable input using obj and kwargs if it is callable,
    otherwise return as it is.

    Parameters
    ----------
    maybe_callable : possibly a callable
    obj : NDFrame
    **kwargs
    )r   )maybe_callabler+   kwargss      r%   apply_if_callabler   t  s"     c,V,,r>   c                   t        j                  |       s5t        | t              rt	        t        | j
                        S t        |       } t        | t        j                        st        d|        | t        k(  rt        d      | S )a  
    Helper function to standardize a supplied mapping.

    Parameters
    ----------
    into : instance or subclass of collections.abc.Mapping
        Must be a class, an initialized collections.defaultdict,
        or an instance of a collections.abc.Mapping subclass.

    Returns
    -------
    mapping : a collections.abc.Mapping subclass or other constructor
        a callable object that can accept an iterator to create
        the desired Mapping.

    See Also
    --------
    DataFrame.to_dict
    Series.to_dict
    zunsupported type: z/to_dict() only accepts initialized defaultdicts)inspectisclassr/   r   r   default_factoryr8   r|   r   Mappingr   )intos    r%   standardize_mappingr     sp    * ??4 dK(;(<(<==DzdCKK(,TF344{IJJKr>   c                     y rF   rG   states    r%   random_stater     rn   r>   c                     y rF   rG   r   s    r%   r   r     s     r>   c                   t        |       s4t        | t        j                  t        j                  j
                  f      rt        j                  j                  |       S t        | t        j                  j                        r| S t        | t        j                  j                        r| S | t        j                  S t        d      )aq  
    Helper function for processing random_state arguments.

    Parameters
    ----------
    state : int, array-like, BitGenerator, Generator, np.random.RandomState, None.
        If receives an int, array-like, or BitGenerator, passes to
        np.random.RandomState() as seed.
        If receives an np.random RandomState or Generator, just returns that unchanged.
        If receives `None`, returns np.random.
        If receives anything else, raises an informative ValueError.

        Default None.

    Returns
    -------
    np.random.RandomState or np.random.Generator. If state is None, returns np.random

    zdrandom_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None)	r   r/   r0   r1   randomBitGeneratorr   r   r)   r   s    r%   r   r     s    ( %Jurzz299;Q;Q.RSyy$$U++	E29900	1	E299..	/	yy+
 	
r>   c                    t        |t              r&|\  }}||v r| d}t        |      | ||<    ||i |S  || g|i |S )as  
    Apply a function ``func`` to object ``obj`` either by passing obj as the
    first argument to the function or, in the case that the func is a tuple,
    interpret the first element of the tuple as a function and pass the obj to
    that function as a keyword argument whose key is the value of the second
    element of the tuple.

    Parameters
    ----------
    func : callable or tuple of (callable, str)
        Function to apply to this object or, alternatively, a
        ``(callable, data_keyword)`` tuple where ``data_keyword`` is a
        string indicating the keyword of ``callable`` that expects the
        object.
    *args : iterable, optional
        Positional arguments passed into ``func``.
    **kwargs : dict, optional
        A dictionary of keyword arguments passed into ``func``.

    Returns
    -------
    object : the return type of ``func``.
    z/ is both the pipe target and a keyword argument)r/   rt   r)   )r+   r   rM   r   targetmsgs         r%   piper     sd    4 $fVHKLCS/!vT$V$$C)$)&))r>   c                V      fd}t         t        j                  t        f      r|S  S )zv
    Returns a function that will map names/labels, dependent if mapper
    is a dict, Series or just a function.
    c                    | v r|    S | S rF   rG   )rf   mappers    r%   fzget_rename_function.<locals>.f  s    ;!9Hr>   )r/   r   r   r   )r   r   s   ` r%   get_rename_functionr     s'     6CKK#;<1H&Hr>   c                    t        | t        t        j                  t        t
        t        f      r| S t        | t        j                        rt        | t              st        |       S | gS )z
    Convert list-like or scalar input to list-like. List, numpy and pandas array-like
    inputs are returned unmodified whereas others are converted to list.
    )
r/   r6   r0   r1   r   r   r   r   r	   r}   )rk   s    r%   convert_to_list_liker     sK     &4XyBSTU	FCLL	)*VS2IF|8Or>   c              #     K   |rt        | |      }t        | ||       	 |  |rt        | |       yy# |rt        | |       w w xY ww)a  
    Temporarily set attribute on an object.

    Parameters
    ----------
    obj : object
        Object whose attribute will be modified.
    attr : str
        Attribute to modify.
    value : Any
        Value to temporarily set attribute to.
    condition : bool, default True
        Whether to set the attribute. Provided in order to not have to
        conditionally use this context manager.

    Yields
    ------
    object : obj with modified attribute.
    N)r   setattr)r+   attrvalue	condition	old_values        r%   temp_setattrr     sU     . C&	T5!*	Cy) 9Cy) s   A	4 A	AA	c                z    t        |       t        |      k7  r$t        dt        |        dt        |       d      y)zC
    Check the length of data matches the length of the index.
    zLength of values (z") does not match length of index ()N)r7   r)   )dataindexs     r%   require_length_matchr   8  sI     4yCJD	{ E
|1
 	
 r>   znp.sumznp.maximum.reduceznp.minimum.reducerg   maxminrZ   rT   meanprodstdvarmediancumprodcumsumc                ,    t         j                  |       S )zH
    if we define an internal function for this argument, return it
    )_cython_tablegetrJ   s    r%   get_cython_funcr   r  s     S!!r>   c                .    t         j                  | |       S )zd
    if we define a builtin function for this argument, return it,
    otherwise return the arg
    )_builtin_tabler   r   s    r%   is_builtin_funcr   y  s    
 c3''r>   c                X    t        |       D cg c]  \  }}|d| n| c}}S c c}}w )a,  
    If a name is missing then replace it by level_n, where n is the count

    .. versionadded:: 1.4.0

    Parameters
    ----------
    names : list-like
        list of column names or None values.

    Returns
    -------
    list
        list of column names with the None values replaced.
    level_)	enumerate)namesir(   s      r%   fill_missing_namesr     s2      CLEBRSwq$DLfQCLd2SSSs   &)r:   r   returnbool)r   r   )r   int).)rk   zArrayLike | list | tuple | zipr2   NpDtype | Noner   
np.ndarray)rk   r	   r2   r   r   r   rF   )r   znp.ndarray | Iterabler2   r   r   r   )r+   zIterable[T] | Tr   zCollection[T] | T)r   z
list[bool])r#   r   r   r   )r   np.random.Generatorr   r   )r   zHint | np.ndarray | np.random.BitGenerator | np.random.RandomState | Noner   znp.random.RandomState)r   zRandomState | None)r   z/Callable[..., T] | tuple[Callable[..., T], str]r   r   )rk   z"Hashable | Iterable | AnyArrayLiker   zlist | AnyArrayLike)T)r   r}   r   r   r   zGenerator[None, None, None])r   r    r   None)rJ   r   r   z
str | None)r   zSequence[Hashable | None]r   zlist[Hashable])m__doc__
__future__r   builtinscollectionsr   r   collections.abcr   r   r   r	   r
   
contextlib	functoolsr   r   typingr   r   r   r   r   rx   numpyr0   pandas._libsr   pandas.compat.numpyr   pandas.core.dtypes.castr   pandas.core.dtypes.commonr   r   pandas.core.dtypes.genericr   r   r   r   pandas.core.dtypes.inferencer   pandas._typingr   r   r   r   r   pandasr    r"   r,   r=   rC   rN   rU   r[   r_   rc   rh   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   contextmanagerr   r   rg   r   maximumreducer   minimumr   _builtin_table_aliasrZ   rT   nansumr   nanmeanr   nanprodr   nanstdr   nanvarr   	nanmediannanmaxnanminr   
nancumprodr   	nancumsumr   r   r   r   rG   r>   r%   <module>r     s  
 #          2 K  =  04n,4,,00, 
DG*3A 
 
 
 H <@!*8:		I	""@ 
 
 
S 
 
F"*>"*"*JI. -1**&** * *@

" LL"&&LL"**##LL"**## LL(LL%LL% LL%LL% LL% FFE	
 FFE FFE IIu GGV JJ GGV JJ FFE IIu FFE IIu  IIx!" LL(#$ FFEIIuFFEIIuJJ	MM9IIxLL(3:"(Tr>   