o
    iM                     @  sr  d Z ddlmZ ddlZddl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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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'm(Z(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 erddl2m3Z3 dd Z4dd Z5dddZ6ddddZ7dd Z8dd d!Z9dd"d#Z:dd$d%Z;dd&d'Z<dd)d*Z=e	+ddd1d2Z>eddd5d2Z>ddd6d2Z>	ddd9d:Z?d;d< Z@dd@dAZAddBdCZBddEdFZCddHdIZDdJdK ZEdLdM ZFdNdO ZGeddRdSZHeddVdSZHdddXdSZHdd\d]ZId^d_ ZJddbdcZKejLddgdhZMddldmZNejOejOejPejQjRejSejTjRiZUi ejOdnejPdoejSdpejVdqejWdrejOdnejXdnejYdsejZdsej[dtej\dtej]duej^duej_dvej`dvejadwejbdwejPdoejcdoejSdpejddpejedxejfdxejgdyejhdyiZidd}d~Zjdd ZkdddZldddZm	ddddZndS )zh
Misc tools for implementing data structures

Note: pandas.core.common is *not* part of the public API.
    )annotationsN)abcdefaultdict)partial)
TYPE_CHECKINGAnyCallable
CollectionHashableIterableIteratorSequencecastoverload)lib)AnyArrayLike	ArrayLikeNpDtypeRandomStateT)find_stack_level)'construct_1d_object_array_from_listlike)is_array_likeis_bool_dtypeis_extension_array_dtype
is_integer)ABCExtensionArrayABCIndex	ABCSeries)iterable_not_string)isna)Indexc                 c  s.    | D ]}t |rt|E dH  q|V  qdS )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element r%   M/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/pandas/core/common.pyr"   =   s   r"   c              	   C  sH   | d j }| dd  D ]}z	|j |krd }W q ty!   d }Y qw |S )Nr      )name
ValueError)objsr(   objr%   r%   r&   consensus_name_attrU   s   

r,   keyr   returnboolc                 C  s   t | ttjtfst| rBt| jrB| jtjkr9t	| }t
|s7d}t
|dkr5t| r5t|dS dS t| jr@dS dS t | tr\t| dkr\t| turWt| } t
| S dS )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 valuesbooleanFTr   )
isinstancer   npndarrayr   r   r   dtypeobject_asarrayr   is_bool_arrayinfer_dtyper    anyr)   r   listlentypeis_bool_list)r-   	key_arrayna_msgr%   r%   r&   is_bool_indexer`   s,   


	

r@   F
warn_floatc                 C  s4   t | r|  r|rtjdtt d t| S | S )a  
    To avoid numpy DeprecationWarnings, cast float to integer where valid.

    Parameters
    ----------
    val : scalar
    warn_float : bool, default False
        If True, issue deprecation warning for a float indexer.

    Returns
    -------
    outval : scalar
    zIndexing with a float is deprecated, and will raise an IndexError in pandas 2.0. You can manually convert to an integer key instead.
stacklevel)r   is_floatr   warningswarnFutureWarningr   int)valrA   r%   r%   r&   cast_scalar_indexer   s   rJ   c                  G     dd | D S )zL
    Returns a generator consisting of the arguments that are not None.
    c                 s  s    | ]	}|d ur|V  qd S Nr%   .0argr%   r%   r&   	<genexpr>   s    znot_none.<locals>.<genexpr>r%   argsr%   r%   r&   not_none      rS   c                  G     t dd | D S )z?
    Returns a boolean indicating if any argument is None.
    c                 s      | ]}|d u V  qd S rL   r%   rM   r%   r%   r&   rP          zany_none.<locals>.<genexpr>r9   rQ   r%   r%   r&   any_none      rY   c                  G  rU   )zA
    Returns a boolean indicating if all arguments are None.
    c                 s  rV   rL   r%   rM   r%   r%   r&   rP      rW   zall_none.<locals>.<genexpr>allrQ   r%   r%   r&   all_none   rZ   r]   c                  G  rU   )zC
    Returns a boolean indicating if any argument is not None.
    c                 s      | ]}|d uV  qd S rL   r%   rM   r%   r%   r&   rP      rW   zany_not_none.<locals>.<genexpr>rX   rQ   r%   r%   r&   any_not_none   rZ   r_   c                  G  rU   )zE
    Returns a boolean indicating if all arguments are not None.
    c                 s  r^   rL   r%   rM   r%   r%   r&   rP      rW   zall_not_none.<locals>.<genexpr>r[   rQ   r%   r%   r&   all_not_none   rZ   r`   rH   c                  G  rU   )z;
    Returns the count of arguments that are not None.
    c                 s  r^   rL   r%   rN   xr%   r%   r&   rP      rW   z!count_not_none.<locals>.<genexpr>)sumrQ   r%   r%   r&   count_not_none   rZ   rd   .valuesArrayLike | list | tuple | zipr4   NpDtype | None
np.ndarrayc                 C     d S rL   r%   re   r4   r%   r%   r&   asarray_tuplesafe   s   rk   r   r   c                 C  ri   rL   r%   rj   r%   r%   r&   rk         c                 C  s   t | ttfst| dst| } nt | tr| jS t | tr)|tjtfv r)t	| S z$t
  t
dtj tj| |d}W d    n1 sGw   Y  W n tyZ   t	|  Y S w t|jjtritj| td}|jdkrydd | D } t	| }|S )N	__array__ignorer4      c                 S  s   g | ]}t |qS r%   )tuplera   r%   r%   r&   
<listcomp>  s    z%asarray_tuplesafe.<locals>.<listcomp>)r1   r:   rq   hasattrr   _valuesr2   r5   objectr   rE   catch_warningssimplefilterVisibleDeprecationWarningr6   r)   
issubclassr4   r<   strndim)re   r4   resultr%   r%   r&   rk      s*   



labelsnp.ndarray | Iterablec                 C  sZ   t | ttfr
| g} t | ttjfs%zt| } W n ty$   | g} Y nw t| |d} | S )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
    ro   )r1   rz   rq   r:   r2   r3   	TypeErrorrk   )r}   r4   r%   r%   r&   index_labels_to_array  s   
r   c                 C  s    | d urt | ttfs| gS | S rL   )r1   rq   r:   r+   r%   r%   r&   maybe_make_list)  s   r   r+   Iterable[T] | TCollection[T] | Tc                 C  s.   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.
    )r1   r   r   Sizedr:   r   r	   r   r%   r%   r&   maybe_iterable_to_list/  s   
r   c                 C  s(   t | to| jdu o| jdu o| jdu S )z
    We have a null slice.
    Nr1   slicestartstopstepr   r%   r%   r&   is_null_slice9     
r   
list[bool]c                 C  rK   )zX
    Find non-trivial slices in "line": return a list of booleans with same length.
    c                 S  s    g | ]}t |tot| qS r%   )r1   r   r   )rN   kr%   r%   r&   rr   I  s     z"is_true_slices.<locals>.<listcomp>r%   )r#   r%   r%   r&   is_true_slicesE  rT   r   r#   c                 C  s(   t | to| jdko| j|ko| jdu S )z&
    We have a full length slice.
    r   Nr   )r+   r#   r%   r%   r&   is_full_sliceM  r   r   c                 C  s>   t | dr
t| dS t| trt| jS t| rt| jS d S )N__name__)	rs   getattrr1   r   get_callable_namefunccallabler<   r   r   r%   r%   r&   r   Y  s   




r   c                 K  s   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+   kwargsr%   r%   r&   apply_if_callablej  s   r   c                 C  sV   t | st| 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isclassr1   r   r   default_factoryr<   ry   r   Mappingr   )intor%   r%   r&   standardize_mapping{  s   

r   statenp.random.Generatorc                 C  ri   rL   r%   r   r%   r%   r&   random_state  rl   r   Gint | ArrayLike | np.random.BitGenerator | np.random.RandomState | Nonenp.random.RandomStatec                 C  ri   rL   r%   r   r%   r%   r&   r     s   RandomState | Nonec                 C  sd   t | st| st| tjjrtj| S t| tjjr| S t| tjjr'| S | du r.tjS td)a  
    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.

        .. versionchanged:: 1.1.0

            array-like and BitGenerator object now passed to np.random.RandomState()
            as seed

        Default None.

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

    Nzdrandom_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None)	r   r   r1   r2   randomBitGeneratorr   	Generatorr)   r   r%   r%   r&   r     s    r   /Callable[..., T] | tuple[Callable[..., T], str]r   c                 O  sX   t |tr!|\}}||v r| d}t|| ||< ||i |S || g|R 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)r1   rq   r)   )r+   r   rR   r   targetmsgr%   r%   r&   pipe  s   

r   c                   s$    fd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                   s   |  v r |  S | S rL   r%   )rb   mapperr%   r&   f
  s   zget_rename_function.<locals>.f)r1   r   r   r   )r   r   r%   r   r&   get_rename_function  s   r   "Hashable | Iterable | AnyArrayLikelist | AnyArrayLikec                 C  s>   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.
    )
r1   r:   r2   r3   r   r   r   r   r   rz   )re   r%   r%   r&   convert_to_list_like  s
   r   attrrz   Iterator[None]c              
   c  s@    t | |}t| || z| V  W t| || dS t| || w )zTemporarily set attribute on an object.

    Args:
        obj: Object whose attribute will be modified.
        attr: Attribute to modify.
        value: Value to temporarily set attribute to.

    Yields:
        obj with modified attribute.
    N)r   setattr)r+   r   value	old_valuer%   r%   r&   temp_setattr"  s   
r   indexr!   Nonec                 C  s2   t | t |krtdt |  dt | ddS )zC
    Check the length of data matches the length of the index.
    zLength of values (z") does not match length of index ()N)r;   r)   )datar   r%   r%   r&   require_length_match6  s   r   rc   maxminr\   r9   meanprodstdvarmediancumprodcumsumrO   r   
str | Nonec                 C  s
   t | S )zH
    if we define an internal function for this argument, return it
    )_cython_tablegetrO   r%   r%   r&   get_cython_funci  s   
r   c                 C  s   t | | S )zd
    if we define a builtin function for this argument, return it,
    otherwise return the arg
    )_builtin_tabler   r   r%   r%   r&   is_builtin_funcp  s   r   namesSequence[Hashable | None]list[Hashable]c                 C  s   dd t | D S )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.
    c                 S  s&   g | ]\}}|d u rd| n|qS )Nlevel_r%   )rN   ir(   r%   r%   r&   rr     s   & z&fill_missing_names.<locals>.<listcomp>)	enumerate)r   r%   r%   r&   fill_missing_namesx  s   r   numeric_onlybool | None | lib.NoDefaultc                 C  s*   | t ju r	d}|S | du rd}|S | }|S )a  Determine the Boolean value of numeric_only.

    See GH#46560 for details on the deprecation.

    Parameters
    ----------
    numeric_only : bool, None, or lib.no_default
        Value passed to the method.

    Returns
    -------
    Resolved value of numeric_only.
    TNF)r   
no_default)r   r|   r%   r%   r&   resolve_numeric_only  s   
r   clsr<   r(   deprecate_nonec                 C  sf   |dv rd}nd}d| d| j  d| d}|r |d| d	7 }|d
| d7 }tj|tt d dS )a  Emit FutureWarning message for deprecation of numeric_only.

    See GH#46560 for details on the deprecation.

    Parameters
    ----------
    cls : type
        pandas type that is generating the warning.
    name : str
        Name of the method that is generating the warning.
    deprecate_none : bool, default False
        Whether to also warn about the deprecation of specifying ``numeric_only=None``.
    )r\   r9   	bool_onlyr   zThe default value of z in .z? is deprecated. In a future version, it will default to False. zIn addition, specifying 'z=None' is deprecated. z2Select only valid columns or specify the value of z to silence this warning.rB   N)r   rE   rF   rG   r   )r   r(   r   arg_namer   r%   r%   r&   deprecate_numeric_only_default  s   
r   )r-   r   r.   r/   )F)rA   r/   )r.   r/   )r.   rH   ).)re   rf   r4   rg   r.   rh   )re   r   r4   rg   r.   r   rL   )r}   r~   r4   rg   r.   rh   )r+   r   r.   r   )r.   r   )r#   rH   r.   r/   )r   r   r.   r   )r   r   r.   r   )r   r   )r   r   r.   r   )re   r   r.   r   )r   rz   r.   r   )r   r!   r.   r   )rO   r   r.   r   )r   r   r.   r   )r   r   r.   r/   )r   r<   r(   rz   r   r/   r.   r   )o__doc__
__future__r   builtinscollectionsr   r   
contextlib	functoolsr   r   typingr   r   r   r	   r
   r   r   r   r   r   rE   numpyr2   pandas._libsr   pandas._typingr   r   r   r   r   pandas.util._exceptionsr   pandas.core.dtypes.castr   pandas.core.dtypes.commonr   r   r   r   pandas.core.dtypes.genericr   r   r   pandas.core.dtypes.inferencer   pandas.core.dtypes.missingr    pandasr!   r"   r,   r@   rJ   rS   rY   r]   r_   r`   rd   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   contextmanagerr   r   rc   r   maximumreducer   minimumr   r\   r9   nansumr   nanmeanr   nanprodr   nanstdr   nanvarr   	nanmediannanmaxnanminr   
nancumprodr   	nancumsumr   r   r   r   r   r   r%   r%   r%   r&   <module>   s    0
7




	"




 
8%



	



