U
    `»{j›K  ã                2   @  sÞ  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�rddl2m3Z3 dd„ Z4dd„ Z5dddœdd„Z6d�ddœdd„Z7dd„ Z8ddœd d!„Z9ddœd"d#„Z:ddœd$d%„Z;ddœd&d'„Z<d(dœd)d*„Z=ed�d,d-d.d/œd0d1„ƒZ>ed‘d2d-d3d/œd4d1„ƒZ>d’d2d-d3d/œd5d1„Z>d“d6d-d.d7œd8d9„Z?d:d;„ Z@d<d=d>œd?d@„ZAddœdAdB„ZBdCdœdDdE„ZCd(ddFœdGdH„ZDdIdJ„ ZEdKdL„ ZFdMdN„ ZGedOdOdPœdQdR„ƒZHedSdTdPœdUdR„ƒZHd”dVdWœdXdR„ZHdYdZd[œd\d]„ZId^d_„ ZJd`dadbœdcdd„ZKejLdedfdgœdhdi„ƒZMdjdkdlœdmdn„ZNejOejOejPejQjRejSejTjRiZUejOdoejPdpejSdqejVdrejWdsejOdoejXdoejYdtejZdtej[duej\duej]dvej^dvej_dwej`dwejadxejbdxejPdpejcdpejSdqejddqejedyejfdyejgdzejhdziZid{d|d}œd~d„Zjd€d�„ Zkd‚dƒd„œd…d†„Zld‡ddˆœd‰dŠ„Zmd•d‹deddkdŒœd�dŽ„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%   úQ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/pandas/core/common.pyr"   =   s    r"   c              	   C  sL   | d j }| dd … D ]0}z|j |kr*d }W q tk
rD   d }Y qX q|S )Nr   é   )ÚnameÚ
ValueError)Zobjsr(   Úobjr%   r%   r&   Úconsensus_name_attrU   s    

r+   r   Úbool)ÚkeyÚreturnc                 C  sº   t | ttjtfƒs$t| ƒr‚t| jƒr‚| jtjkrrt 	| ¡}t
 |¡snd}t
 |¡dkrjt|ƒ ¡ rjt|ƒ‚dS dS t| jƒr¶dS n4t | tƒr¶t| ƒdkr¶t| ƒtk	r¬t| ƒ} 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   Zis_bool_arrayZinfer_dtyper    Úanyr)   r   ÚlistÚlenÚtypeZis_bool_list)r-   Z	key_arrayZna_msgr%   r%   r&   Úis_bool_indexer`   s*    ÿÿ




r:   F)Ú
warn_floatc                 C  s:   t  | ¡r6|  ¡ r6|r.tjdt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
    z„Indexing 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   Zis_floatr   ÚwarningsÚwarnÚFutureWarningr   ÚinspectÚcurrentframeÚint)Úvalr;   r%   r%   r&   Úcast_scalar_indexer—   s    
ürE   c                  G  s   dd„ | D ƒS )zL
    Returns a generator consisting of the arguments that are not None.
    c                 s  s   | ]}|d k	r|V  qd S ©Nr%   ©Ú.0Úargr%   r%   r&   Ú	<genexpr>¶   s      znot_none.<locals>.<genexpr>r%   ©Úargsr%   r%   r&   Únot_none²   s    rM   )r.   c                  G  s   t dd„ | D ƒƒS )z?
    Returns a boolean indicating if any argument is None.
    c                 s  s   | ]}|d kV  qd S rF   r%   rG   r%   r%   r&   rJ   ½   s     zany_none.<locals>.<genexpr>©r6   rK   r%   r%   r&   Úany_none¹   s    rO   c                  G  s   t dd„ | D ƒƒS )zA
    Returns a boolean indicating if all arguments are None.
    c                 s  s   | ]}|d kV  qd S rF   r%   rG   r%   r%   r&   rJ   Ä   s     zall_none.<locals>.<genexpr>©ÚallrK   r%   r%   r&   Úall_noneÀ   s    rR   c                  G  s   t dd„ | D ƒƒS )zC
    Returns a boolean indicating if any argument is not None.
    c                 s  s   | ]}|d k	V  qd S rF   r%   rG   r%   r%   r&   rJ   Ë   s     zany_not_none.<locals>.<genexpr>rN   rK   r%   r%   r&   Úany_not_noneÇ   s    rS   c                  G  s   t dd„ | D ƒƒS )zE
    Returns a boolean indicating if all arguments are not None.
    c                 s  s   | ]}|d k	V  qd S rF   r%   rG   r%   r%   r&   rJ   Ò   s     zall_not_none.<locals>.<genexpr>rP   rK   r%   r%   r&   Úall_not_noneÎ   s    rT   rC   c                  G  s   t dd„ | D ƒƒS )z;
    Returns the count of arguments that are not None.
    c                 s  s   | ]}|d k	V  qd S rF   r%   ©rH   Úxr%   r%   r&   rJ   Ù   s     z!count_not_none.<locals>.<genexpr>)ÚsumrK   r%   r%   r&   Úcount_not_noneÕ   s    rX   .zArrayLike | list | tuple | zipzNpDtype | Nonez
np.ndarray)Úvaluesr3   r.   c                 C  s   d S rF   r%   ©rY   r3   r%   r%   r&   Úasarray_tuplesafeÜ   s    r[   r   r   c                 C  s   d S rF   r%   rZ   r%   r%   r&   r[   æ   s    c                 C  s    t | ttfƒs"t| dƒs"t| ƒ} nt | tƒr2| jS t | tƒrR|tjtfkrRt	| ƒS tj
| |d�}t|jjtƒr|tj
| td�}|jdkrœdd„ | D ƒ} t	| ƒ}|S )NZ	__array__©r3   é   c                 S  s   g | ]}t |ƒ‘qS r%   )ÚtuplerU   r%   r%   r&   Ú
<listcomp>ü   s     z%asarray_tuplesafe.<locals>.<listcomp>)r0   r7   r^   Úhasattrr   Z_valuesr1   r4   Úobjectr   r5   Ú
issubclassr3   r9   ÚstrÚndim)rY   r3   Úresultr%   r%   r&   r[   ë   s    


znp.ndarray | Iterable)Úlabelsr3   r.   c                 C  s\   t | ttfƒr| g} t | ttjfƒsLzt| ƒ} W n tk
rJ   | g} Y nX 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
    r\   )r0   rc   r^   r7   r1   r2   Ú	TypeErrorr[   )rf   r3   r%   r%   r&   Úindex_labels_to_array  s    rh   c                 C  s    | d k	rt | ttfƒs| gS | S rF   )r0   r^   r7   ©r*   r%   r%   r&   Úmaybe_make_list  s    rj   zIterable[T] | TzCollection[T] | T)r*   r.   c                 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.
    )r0   r   r   ÚSizedr7   r   r	   ri   r%   r%   r&   Úmaybe_iterable_to_list%  s    
rl   c                 C  s(   t | tƒo&| jdko&| jdko&| jdkS )z
    We have a null slice.
    N©r0   ÚsliceÚstartÚstopÚstepri   r%   r%   r&   Úis_null_slice/  s    
ÿþürr   z
list[bool]c                 C  s   dd„ | D ƒS )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%   )r0   rn   rr   )rH   Úkr%   r%   r&   r_   ?  s     z"is_true_slices.<locals>.<listcomp>r%   )r#   r%   r%   r&   Úis_true_slices;  s    rt   )r#   r.   c                 C  s(   t | tƒo&| jdko&| j|ko&| jdkS )z&
    We have a full length slice.
    r   Nrm   )r*   r#   r%   r%   r&   Úis_full_sliceC  s    
ÿþüru   c                 C  s>   t | dƒrt| dƒS t| tƒr(t| jƒS t| ƒr:t| ƒjS d S )NÚ__name__)	r`   Úgetattrr0   r   Úget_callable_nameÚfuncÚcallabler9   rv   ri   r%   r%   r&   rx   O  s    




rx   c                 K  s   t | ƒr| |f|Ž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
    )rz   )Zmaybe_callabler*   Úkwargsr%   r%   r&   Úapply_if_callable`  s    r|   c                 C  sX   t  | ¡s(t| tƒr tt| jƒS t| ƒ} t| tj	ƒsDt
d| › �ƒ‚n| tkrTt
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)rA   Úisclassr0   r   r   Údefault_factoryr9   rb   r   ÚMappingrg   )Zintor%   r%   r&   Ústandardize_mappingq  s    

r€   znp.random.Generator)Ústater.   c                 C  s   d S rF   r%   ©r�   r%   r%   r&   Úrandom_state‘  s    rƒ   zGint | ArrayLike | np.random.BitGenerator | np.random.RandomState | Noneznp.random.RandomStatec                 C  s   d S rF   r%   r‚   r%   r%   r&   rƒ   –  s    zRandomState | Noner‚   c                 C  sh   t | ƒst| ƒst| tjjƒr*tj | ¡S t| tjjƒr<| S t| tjjƒrN| S | dkr\tjS tdƒ‚dS )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   r0   r1   ÚrandomZBitGeneratorr   Ú	Generatorr)   r‚   r%   r%   r&   rƒ   �  s     ÿþýÿz/Callable[..., T] | tuple[Callable[..., T], str]r   )ry   r.   c                 O  sR   t |tƒr>|\}}||kr,|› d�}t|ƒ‚| ||< |||ŽS || f|ž|ŽS d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 argumentN)r0   r^   r)   )r*   ry   rL   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   | ˆ krˆ |  S | S d S rF   r%   )rV   ©Úmapperr%   r&   Úf   s    zget_rename_function.<locals>.f)r0   r   r   r   )rŠ   r‹   r%   r‰   r&   Úget_rename_functionú  s    rŒ   z"Hashable | Iterable | AnyArrayLikezlist | AnyArrayLike)rY   r.   c                 C  s>   t | ttjtttfƒr| S t | tjƒr8t | t	ƒs8t| ƒ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.
    )
r0   r7   r1   r2   r   r   r   r   r   rc   )rY   r%   r%   r&   Úconvert_to_list_like	  s
    r�   rc   zIterator[None])Úattrr.   c              
   c  s4   t | |ƒ}t| ||ƒ z
| V  W 5 t| ||ƒ X dS )zøTemporarily 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)rw   Úsetattr)r*   rŽ   ÚvalueÚ	old_valuer%   r%   r&   Útemp_setattr  s
    

r’   r!   ÚNone)Úindexr.   c                 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)r8   r)   )Údatar”   r%   r%   r&   Úrequire_length_match,  s    ÿr—   rW   ÚmaxÚminrQ   r6   ÚmeanÚprodÚstdÚvarÚmedianÚcumprodÚcumsumr   z
str | None)rI   r.   c                 C  s
   t  | ¡S )zH
    if we define an internal function for this argument, return it
    )Ú_cython_tableÚget©rI   r%   r%   r&   Úget_cython_func_  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_funcf  s    r¦   zSequence[Hashable | None]zlist[Hashable])Únamesr.   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 krd|› �n|‘qS )NZlevel_r%   )rH   Úir(   r%   r%   r&   r_   ~  s     z&fill_missing_names.<locals>.<listcomp>)Ú	enumerate)r§   r%   r%   r&   Úfill_missing_namesn  s    rª   zbool | None | lib.NoDefault)Únumeric_onlyr.   c                 C  s&   | t jkrd}n| dkrd}n| }|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   Z
no_default)r«   re   r%   r%   r&   Úresolve_numeric_only�  s    
r¬   r9   )Úclsr(   Údeprecate_noner.   c                 C  sl   |dkrd}nd}d|› d| j › d|› d�}|r@|d|› d	�7 }|d
|› d�7 }tj|t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``.
    )rQ   r6   Z	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.r<   N)rv   r>   r?   r@   r   rA   rB   )r­   r(   r®   Zarg_namer‡   r%   r%   r&   Údeprecate_numeric_only_defaultœ  s    ÿ
ÿ  
ÿr°   )F).).)N)N)N)F)oÚ__doc__Ú
__future__r   ÚbuiltinsÚcollectionsr   r   Ú
contextlibÚ	functoolsr   rA   Útypingr   r   r   r	   r
   r   r   r   r   r   r>   Únumpyr1   Zpandas._libsr   Zpandas._typingr   r   r   r   r   Zpandas.util._exceptionsr   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr   r   r   r   Zpandas.core.dtypes.genericr   r   r   Zpandas.core.dtypes.inferencer   Zpandas.core.dtypes.missingr    Zpandasr!   r"   r+   r:   rE   rM   rO   rR   rS   rT   rX   r[   rh   rj   rl   rr   rt   ru   rx   r|   r€   rƒ   rˆ   rŒ   r�   Úcontextmanagerr’   r—   rW   r˜   ÚmaximumÚreducer™   Úminimumr¥   rQ   r6   Znansumrš   Znanmeanr›   Znanprodrœ   Znanstdr�   Znanvarrž   Z	nanmedianZnanmaxZnanminrŸ   Z
nancumprodr    Z	nancumsumr¡   r¤   r¦   rª   r¬   r°   r%   r%   r%   r&   Ú<module>   sð   07 ÿ	 ÿ
 8%   ý                         ç ÿ