U
    ¿mœdøE  ã                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$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ e�rddl0m1Z1 dd„ Z2dd„ Z3dddœdd„Z4dd„ Z5dd„ Z6ddœdd„Z7ddœdd „Z8ddœd!d"„Z9ddœd#d$„Z:d%dœd&d'„Z;ed†d)d*d+d,œd-d.„ƒZ<ed‡d/d*d0d,œd1d.„ƒZ<dˆd/d*d0d,œd2d.„Z<d‰d3d*d+d4œd5d6„Z=d7d8„ Z>d9d:d;œd<d=„Z?ddœd>d?„Z@ddœd@dA„ZAdBdœdCdD„ZBd%ddEœdFdG„ZCdHdI„ ZDdJdK„ ZEdLdM„ ZFedNdNdOœdPdQ„ƒZGedRdSdOœdTdQ„ƒZGdŠdUdVœdWdQ„ZGdXdYdZœd[d\„ZHd]d^„ ZId_d`daœdbdc„ZJejKdddedfœdgdh„ƒZLdidjdkœdldm„ZMejNejNejOejPjQejRejSjQiZTejNdnejOdoejRdpejUdqejVdrejNdnejWdnejXdsejYdsejZdtej[dtej\duej]duej^dvej_dvej`dwejadwejOdoejbdoejRdpejcdpejddxejedxejfdyejgdyiZhdzd{d|œd}d~„Zidd€„ Zjd�d‚dƒœd„d…„Zkd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Ú	GeneratorÚHashableÚIterableÚSequenceÚcastÚoverload)Úlib)ÚAnyArrayLikeÚ	ArrayLikeÚNpDtypeÚRandomStateÚT)Ú'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$   úK/home/sam/Atlas/atlas_env/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_attrT   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*    ÿÿ




r9   c                 C  s   t  | ¡r|  ¡ 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   Zis_floatr   Ú
IndexError)Úvalr$   r$   r%   Úcast_scalar_indexer–   s
    þr<   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    rD   )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 r=   r$   r>   r$   r$   r%   rA   ·   s     zany_none.<locals>.<genexpr>©r5   rB   r$   r$   r%   Úany_none³   s    rF   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 r=   r$   r>   r$   r$   r%   rA   ¾   s     zall_none.<locals>.<genexpr>©ÚallrB   r$   r$   r%   Úall_noneº   s    rI   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 r=   r$   r>   r$   r$   r%   rA   Å   s     zany_not_none.<locals>.<genexpr>rE   rB   r$   r$   r%   Úany_not_noneÁ   s    rJ   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 r=   r$   r>   r$   r$   r%   rA   Ì   s     zall_not_none.<locals>.<genexpr>rG   rB   r$   r$   r%   Úall_not_noneÈ   s    rK   Úintc                  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 r=   r$   ©r?   Úxr$   r$   r%   rA   Ó   s     z!count_not_none.<locals>.<genexpr>)ÚsumrB   r$   r$   r%   Úcount_not_noneÏ   s    rP   .zArrayLike | list | tuple | zipzNpDtype | Nonez
np.ndarray)Úvaluesr2   r-   c                 C  s   d S r=   r$   ©rQ   r2   r$   r$   r%   Úasarray_tuplesafeÖ   s    rS   r   r   c                 C  s   d S r=   r$   rR   r$   r$   r%   rS   à   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 z4t
 ¡ �" t
 dtj¡ tj| |d�}W 5 Q R X W n tk
r¢   t	| ƒ Y S X t|jjtƒrÀtj| td�}|jdkràdd„ | D ƒ} t	| ƒ}|S )NZ	__array__Úignore©r2   é   c                 S  s   g | ]}t |ƒ‘qS r$   )ÚtuplerM   r$   r$   r%   Ú
<listcomp>ÿ   s     z%asarray_tuplesafe.<locals>.<listcomp>)r/   r6   rW   Úhasattrr   Z_valuesr0   r3   Úobjectr   ÚwarningsÚcatch_warningsÚsimplefilterZVisibleDeprecationWarningr4   r(   Ú
issubclassr2   r8   ÚstrÚndim)rQ   r2   Úresultr$   r$   r%   rS   å   s$    



znp.ndarray | Iterable)Úlabelsr2   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
    rU   )r/   r_   rW   r6   r0   r1   Ú	TypeErrorrS   )rb   r2   r$   r$   r%   Úindex_labels_to_array  s    rd   c                 C  s    | d k	rt | ttfƒs| gS | S r=   )r/   rW   r6   ©r)   r$   r$   r%   Úmaybe_make_list"  s    rf   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.
    )r/   r   r   ÚSizedr6   r   r	   re   r$   r$   r%   Úmaybe_iterable_to_list(  s    
rh   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©r/   ÚsliceÚstartÚstopÚstepre   r$   r$   r%   Úis_null_slice2  s    
ÿþürn   c                 C  s*   t | tƒo(| jdk	o(| jdk	o(| j| jkS )z>
    We have an empty slice, e.g. no values are selected.
    N)r/   rj   rk   rl   re   r$   r$   r%   Úis_empty_slice>  s    
ÿþ
üro   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$   )r/   rj   rn   )r?   Úkr$   r$   r%   rX   N  s     z"is_true_slices.<locals>.<listcomp>r$   )r"   r$   r$   r%   Úis_true_slicesJ  s    rq   )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   Nri   )r)   r"   r$   r$   r%   Úis_full_sliceR  s    
ÿþürr   c                 C  s>   t | dƒrt| dƒS t| tƒr(t| jƒS t| ƒr:t| ƒjS d S )NÚ__name__)	rY   Úgetattrr/   r   Úget_callable_nameÚfuncÚcallabler8   rs   re   r$   r$   r%   ru   ^  s    




ru   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
    )rw   )Zmaybe_callabler)   Úkwargsr$   r$   r%   Úapply_if_callableo  s    ry   c                 C  sV   t  | ¡s(t| tƒr tt| jƒS t| ƒ} t| tj	ƒsBt
d| › �ƒ‚| tkrRt
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   ÚMappingrc   )Zintor$   r$   r%   Ústandardize_mapping€  s    

r~   znp.random.Generator)Ústater-   c                 C  s   d S r=   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 r=   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   r/   r0   ÚrandomZBitGeneratorr   r
   r(   r€   r$   r$   r%   r�   ¬  s     ÿþýÿz/Callable[..., T] | tuple[Callable[..., T], str]r   )rv   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)r/   rW   r(   )r)   rv   rC   rx   Ú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 r=   r$   )rN   ©Úmapperr$   r%   Úf  s    zget_rename_function.<locals>.f)r/   r   r}   r   )r‡   rˆ   r$   r†   r%   Úget_rename_function	  s    r‰   z"Hashable | Iterable | AnyArrayLikezlist | AnyArrayLike)rQ   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.
    )
r/   r6   r0   r1   r   r   r   r   r   r_   )rQ   r$   r$   r%   Úconvert_to_list_like  s
    rŠ   r_   zGenerator[(None, None, 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)rt   Ú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)r7   r(   )Údatar‘   r$   r$   r%   Úrequire_length_match;  s    ÿr”   rO   ÚmaxÚminrH   r5   ÚmeanÚprodÚstdÚvarÚmedianÚcumprodÚcumsumr   z
str | None)r@   r-   c                 C  s
   t  | ¡S )zH
    if we define an internal function for this argument, return it
    )Ú_cython_tableÚget©r@   r$   r$   r%   Úget_cython_funcn  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_funcu  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$   )r?   Úir'   r$   r$   r%   rX   �  s     z&fill_missing_names.<locals>.<listcomp>)Ú	enumerate)r¤   r$   r$   r%   Úfill_missing_names}  s    r§   ).).)N)N)N)lÚ__doc__Ú
__future__r   ÚbuiltinsÚcollectionsr   r   Ú
contextlibÚ	functoolsr   rz   Útypingr   r   r   r	   r
   r   r   r   r   r   r[   Únumpyr0   Zpandas._libsr   Zpandas._typingr   r   r   r   r   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*   r9   r<   rD   rF   rI   rJ   rK   rP   rS   rd   rf   rh   rn   ro   rq   rr   ru   ry   r~   r�   r…   r‰   rŠ   Úcontextmanagerr�   r”   rO   r•   ÚmaximumÚreducer–   Úminimumr¢   rH   r5   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%   Ú<module>   sê   07 ÿ	! ÿ
 8%   ý                         ç