a
    DBìj)D  ã                2   @  sÐ  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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Œd0d+d1d-œd2d/„ƒZ<d�d0d+d1d-œd3d/„Z<dŽd4d+d,d5œd6d7„Z=d8d9„ Z>d:d;d<œd=d>„Z?ddœd?d@„Z@ddœdAdB„ZAdCdœdDdE„ZBd&ddFœdGdH„ZCdIdJ„ ZDdKdL„ ZEdMdN„ ZFedOdOdPœdQdR„ƒZGedSdTdPœdUdR„ƒZGd�dVdWœdXdR„ZGdYdZd[œd\d]„ZHd^d_„ ZId`dadbœdcdd„ZJejKd�dfddgdhœdidj„ƒZLdkdldmœdndo„ZMejNejNejOejPjQejRejSjQiZTejNdpejOdqejRdriZUejNdsejOdtejRduejVdvejWdwejNdsejXdsejYdxejZdxej[dyej\dyej]dzej^dzej_d{ej`d{ejad|ejbd|ejOdtejcdtejRduejdduejed}ejfd}ejgd~ejhd~iZidd€d�œd‚dƒ„Zjd„d…„ Zkd†d‡dˆœd‰dŠ„ZldS )‘zh
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                 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/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/common.pyr    >   s    r    c              	   C  sJ   | d j }| dd … D ].}z|j |kr*d }W q tyB   d }Y q0 q|S )Nr   é   )ÚnameÚ
ValueError)Zobjsr&   Úobjr#   r#   r$   Úconsensus_name_attrV   s    

r)   r   Úbool)ÚkeyÚreturnc                 C  s¨   t | ttjttfƒrpt | tƒsp| jtjkr`t 	| ¡}t
 |¡s\d}t
j|dd�rXt|ƒ‚dS dS t| jƒr¤dS n4t | tƒr¤t| ƒdkr¤t| ƒtu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 valuesT)ZskipnaFr   )Ú
isinstancer   ÚnpÚndarrayr   r   r   ÚdtypeÚobject_Úasarrayr   Zis_bool_arrayr'   r   ÚlistÚlenÚtypeZis_bool_list)r+   Z	key_arrayZna_msgr#   r#   r$   Úis_bool_indexera   s*    ÿþ




r6   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
    þr9   c                  G  s   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>²   ó    znot_none.<locals>.<genexpr>r#   ©Úargsr#   r#   r$   Únot_none®   s    rB   )r,   c                  G  s   t dd„ | D ƒƒS )z?
    Returns a boolean indicating if any argument is None.
    c                 s  s   | ]}|d u V  qd S r:   r#   r;   r#   r#   r$   r>   ¹   r?   zany_none.<locals>.<genexpr>©Úanyr@   r#   r#   r$   Úany_noneµ   s    rE   c                  G  s   t dd„ | D ƒƒS )zA
    Returns a boolean indicating if all arguments are None.
    c                 s  s   | ]}|d u V  qd S r:   r#   r;   r#   r#   r$   r>   À   r?   zall_none.<locals>.<genexpr>©Úallr@   r#   r#   r$   Úall_none¼   s    rH   c                  G  s   t dd„ | D ƒƒS )zC
    Returns a boolean indicating if any argument is not None.
    c                 s  s   | ]}|d uV  qd S r:   r#   r;   r#   r#   r$   r>   Ç   r?   zany_not_none.<locals>.<genexpr>rC   r@   r#   r#   r$   Úany_not_noneÃ   s    rI   c                  G  s   t dd„ | D ƒƒS )zE
    Returns a boolean indicating if all arguments are not None.
    c                 s  s   | ]}|d uV  qd S r:   r#   r;   r#   r#   r$   r>   Î   r?   zall_not_none.<locals>.<genexpr>rF   r@   r#   r#   r$   Úall_not_noneÊ   s    rJ   Úintc                  G  s   t dd„ | D ƒƒS )z;
    Returns the count of arguments that are not None.
    c                 s  s   | ]}|d uV  qd S r:   r#   ©r<   Úxr#   r#   r$   r>   Õ   r?   z!count_not_none.<locals>.<genexpr>)Úsumr@   r#   r#   r$   Úcount_not_noneÑ   s    rO   .zArrayLike | list | tuple | zipzNpDtype | Nonez
np.ndarray)Úvaluesr0   r,   c                 C  s   d S r:   r#   ©rP   r0   r#   r#   r$   Úasarray_tuplesafeØ   s    rR   r   r   c                 C  s   d S r:   r#   rQ   r#   r#   r$   rR   â   s    c                 C  s  t | ttfƒs"t| dƒs"t| ƒ} n t | tƒr2| jS t | tƒrB| jS t | tƒrb|tjt	fv rbt
| ƒS zLt ¡ �0 ts€t dtj¡ tj| |d�}W d   ƒ n1 s¢0    Y  W n tyÈ   t
| ƒ Y S 0 t|jjtƒrætj| t	d�}|jdk�rdd„ | D ƒ} t
| ƒ}|S )NZ	__array__Úignore©r0   é   c                 S  s   g | ]}t |ƒ‘qS r#   )ÚtuplerL   r#   r#   r$   Ú
<listcomp>  r?   z%asarray_tuplesafe.<locals>.<listcomp>)r-   r3   rV   Úhasattrr   Z_valuesr   r.   r1   Úobjectr   ÚwarningsÚcatch_warningsr   ÚsimplefilterZVisibleDeprecationWarningr2   r'   Ú
issubclassr0   r5   ÚstrÚndim)rP   r0   Úresultr#   r#   r$   rR   ç   s*    



0znp.ndarray | Iterable)Úlabelsr0   r,   c                 C  sZ   t | ttfƒr| g} t | ttjfƒsJzt| ƒ} W n tyH   | g} Y n0 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
    rT   )r-   r^   rV   r3   r.   r/   Ú	TypeErrorrR   )ra   r0   r#   r#   r$   Úindex_labels_to_array
  s    rc   c                 C  s    | d urt | ttfƒs| gS | S r:   )r-   rV   r3   ©r(   r#   r#   r$   Úmaybe_make_list'  s    re   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   ÚSizedr3   r   r   rd   r#   r#   r$   Úmaybe_iterable_to_list-  s    
rg   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©r-   ÚsliceÚstartÚstopÚsteprd   r#   r#   r$   Úis_null_slice7  s    
ÿþürm   c                 C  s*   t | tƒo(| jduo(| jduo(| j| jkS )z>
    We have an empty slice, e.g. no values are selected.
    N)r-   ri   rj   rk   rd   r#   r#   r$   Úis_empty_sliceC  s    
ÿþ
ürn   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-   ri   rm   )r<   Úkr#   r#   r$   rW   S  r?   z"is_true_slices.<locals>.<listcomp>r#   )r!   r#   r#   r$   Úis_true_slicesO  s    rp   )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   Nrh   )r(   r!   r#   r#   r$   Úis_full_sliceW  s    
ÿþürq   c                 C  s>   t | dƒrt| dƒS t| tƒr(t| jƒS t| ƒr:t| ƒjS d S )NÚ__name__)	rX   Úgetattrr-   r
   Úget_callable_nameÚfuncÚcallabler5   rr   rd   r#   r#   r$   rt   c  s    




rt   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
    )rv   )Zmaybe_callabler(   Úkwargsr#   r#   r$   Úapply_if_callablet  s    rx   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_factoryr5   r]   r   ÚMappingrb   )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€   zHint | np.ndarray | 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  sf   t | ƒst| tjtjjfƒr(tj | ¡S t| tjjƒr:| S t| tjjƒrL| S | du rZtjS tdƒ‚dS )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

    Nzdrandom_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None)	r   r-   r.   r/   ÚrandomZBitGeneratorr   r   r'   r   r#   r#   r$   r€   ±  s    ÿz/Callable[..., T] | tuple[Callable[..., T], str]r   )ru   r,   c                 O  s\   t |tƒrB|\}}||v r,|› d�}t|ƒ‚| ||< ||i |¤ŽS || g|¢R i |¤Ž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-   rV   r'   )r(   ru   rA   rw   Ú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 d S r:   r#   )rM   ©Ú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)rP   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-   r3   r.   r/   r   r   r   r   r   r^   )rP   r#   r#   r$   Úconvert_to_list_like  s
    r‰   Tr^   zGenerator[None, None, None])ÚattrÚ	conditionr,   c              
   c  sL   |rt | |ƒ}t| ||ƒ z| V  W |rHt| ||ƒ n|rFt| ||ƒ 0 dS )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)rs   Úsetattr)r(   rŠ   Úvaluer‹   Ú	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)r4   r'   )Údatar‘   r#   r#   r$   Úrequire_length_match8  s    ÿýÿr”   znp.sumznp.maximum.reduceznp.minimum.reducerN   ÚmaxÚminrG   rD   Ú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_funcr  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_funcy  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 u rd|› �n|‘qS )NZlevel_r#   )r<   Úir&   r#   r#   r$   rW   ‘  r?   z&fill_missing_names.<locals>.<listcomp>)Ú	enumerate)r¤   r#   r#   r$   Úfill_missing_names�  s    r§   ).).)N)N)N)T)mÚ__doc__Ú
__future__r   ÚbuiltinsÚcollectionsr   r   Úcollections.abcr   r   r   r   r	   Ú
contextlibÚ	functoolsr
   ry   Útypingr   r   r   r   r   rZ   Únumpyr.   Zpandas._libsr   Zpandas.compat.numpyr   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr   r   Zpandas.core.dtypes.genericr   r   r   r   Zpandas.core.dtypes.inferencer   Zpandas._typingr   r   r   r   r   Zpandasr   r    r)   r6   r9   rB   rE   rH   rI   rJ   rO   rR   rc   re   rg   rm   rn   rp   rq   rt   rx   r}   r€   r„   rˆ   r‰   Úcontextmanagerr�   r”   rN   r•   ÚmaximumÚreducer–   Úminimumr¢   Z_builtin_table_aliasrG   rD   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¾   7 ÿ	$ ÿ
 #% ÿ 

ýýç