o
    Û­j)D  ã                   @  sr  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„Z4dd„ Z5dd„ Z6dŒdd„Z7dŒd d!„Z8dŒd"d#„Z9dŒd$d%„Z:d�d'd(„Z;e	)dŽd�d/d0„ƒZ<edŽd�d3d0„ƒZ<d‘d�d4d0„Z<	d‘d’d7d8„Z=d9d:„ Z>d“d>d?„Z?dŒd@dA„Z@dŒdBdC„ZAd”dEdF„ZBd•dHdI„ZCdJdK„ ZDdLdM„ ZEdNdO„ ZFed–dRdS„ƒZGed—dVdS„ƒZGd‘d˜dXdS„ZGd™d\d]„ZHd^d_„ ZIdšdbdc„ZJejK	dd›dœdidj„ƒZLd�dndo„ZMejNejNejOejPjQejRejSjQiZTejNdpejOdqejRdriZUi ejNds“ejOdt“ejRdu“ejVdv“ejWdw“ejNds“ejXds“ejYdx“ejZdx“ej[dy“ej\dy“ej]dz“ej^dz“e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ƒ„Zjd„d…„ ZkdŸ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#   úO/var/www/html/CropPilot/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_attrV   s   

€ÿr*   Úkeyr   ÚreturnÚboolc                 C  sª   t | ttjttfƒr9t | tƒs9| jtjkr0t 	| ¡}t
 |¡s.d}t
j|dd�r,t|ƒ‚dS dS t| jƒr7dS dS t | tƒrSt| ƒdkrSt| ƒturNt| ƒ} 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)ÚskipnaFr   )Ú
isinstancer   ÚnpÚndarrayr   r   r   ÚdtypeÚobject_Úasarrayr   Úis_bool_arrayr'   r   ÚlistÚlenÚtypeÚis_bool_list)r+   Ú	key_arrayÚna_msgr#   r#   r$   Úis_bool_indexera   s,   ÿþ


	
ø
r<   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   Úis_floatr   Ú
IndexError)Úvalr#   r#   r$   Úcast_scalar_indexer˜   s
   þr@   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®   ó   rI   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 rB   r#   rC   r#   r#   r$   rF   ¹   ó   € zany_none.<locals>.<genexpr>©ÚanyrG   r#   r#   r$   Úany_noneµ   ó   rP   c                  G  rK   )zA
    Returns a boolean indicating if all arguments are None.
    c                 s  rL   rB   r#   rC   r#   r#   r$   rF   À   rM   zall_none.<locals>.<genexpr>©ÚallrG   r#   r#   r$   Úall_none¼   rQ   rT   c                  G  rK   )zC
    Returns a boolean indicating if any argument is not None.
    c                 s  ó   � | ]}|d uV  qd S rB   r#   rC   r#   r#   r$   rF   Ç   rM   zany_not_none.<locals>.<genexpr>rN   rG   r#   r#   r$   Úany_not_noneÃ   rQ   rV   c                  G  rK   )zE
    Returns a boolean indicating if all arguments are not None.
    c                 s  rU   rB   r#   rC   r#   r#   r$   rF   Î   rM   zall_not_none.<locals>.<genexpr>rR   rG   r#   r#   r$   Úall_not_noneÊ   rQ   rW   Úintc                  G  rK   )z;
    Returns the count of arguments that are not None.
    c                 s  rU   rB   r#   ©rD   Úxr#   r#   r$   rF   Õ   rM   z!count_not_none.<locals>.<genexpr>)ÚsumrG   r#   r#   r$   Úcount_not_noneÑ   rQ   r\   .ÚvaluesúArrayLike | list | tuple | zipr2   úNpDtype | Noneú
np.ndarrayc                 C  ó   d S rB   r#   ©r]   r2   r#   r#   r$   Úasarray_tuplesafeØ   s   rc   r   r   c                 C  ra   rB   r#   rb   r#   r#   r$   rc   â   ó   c                 C  s
  t | ttfƒst| dƒst| ƒ} nt | tƒr| jS t | tƒr!| jS t | tƒr1|tjt	fv r1t
| ƒS z&t ¡ � ts@t dtj¡ tj| |d�}W d   ƒ n1 sQw   Y  W n tyd   t
| ƒ Y S w t|jjtƒrstj| t	d�}|jdkrƒdd„ | D ƒ} t
| ƒ}|S )NÚ	__array__Úignore©r2   é   c                 S  s   g | ]}t |ƒ‘qS r#   )ÚtuplerY   r#   r#   r$   Ú
<listcomp>  s    z%asarray_tuplesafe.<locals>.<listcomp>)r/   r6   ri   Úhasattrr   Ú_valuesr   r0   r3   Úobjectr   ÚwarningsÚcatch_warningsr   ÚsimplefilterÚVisibleDeprecationWarningr4   r'   Ú
issubclassr2   r8   ÚstrÚndim)r]   r2   Úresultr#   r#   r$   rc   ç   s0   



ü€û
Ú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
    rg   )r/   rs   ri   r6   r0   r1   Ú	TypeErrorrc   )rv   r2   r#   r#   r$   Úindex_labels_to_array
  s   
ÿry   c                 C  s    | d urt | ttfƒs| gS | S rB   )r/   ri   r6   ©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.
    )r/   r   r   ÚSizedr6   r   r   rz   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©r/   ÚsliceÚstartÚstopÚsteprz   r#   r#   r$   Úis_null_slice7  ó   
ÿþür…   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/   r�   r‚   rƒ   rz   r#   r#   r$   Úis_empty_sliceC  s   
ÿþ
ür‡   ú
list[bool]c                 C  rA   )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/   r�   r…   )rD   Úkr#   r#   r$   rj   S  s     z"is_true_slices.<locals>.<listcomp>r#   )r!   r#   r#   r$   Úis_true_slicesO  rJ   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_sliceW  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__)	rk   Úgetattrr/   r
   Úget_callable_nameÚfuncÚcallabler8   rŒ   rz   r#   r#   r$   rŽ   c  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_callablet  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Úisclassr/   r   r
   Údefault_factoryr8   rr   r   ÚMappingrx   )Úintor#   r#   r$   Ústandardize_mapping…  s   

r™   Ústateúnp.random.Generatorc                 C  ra   rB   r#   ©rš   r#   r#   r$   Úrandom_state¥  rd   r�   úHint | np.ndarray | np.random.BitGenerator | np.random.RandomState | Noneúnp.random.RandomStatec                 C  ra   rB   r#   rœ   r#   r#   r$   r�   ª  s   úRandomState | Nonec                 C  sb   t | ƒst| tjtjjfƒrtj | ¡S t| tjjƒr| S t| tjjƒr&| S | du r-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

    Nzdrandom_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None)	r   r/   r0   r1   ÚrandomÚBitGeneratorr   r   r'   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)r/   ri   r'   )r)   r�   rH   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 rB   r#   )rZ   ©Úmapperr#   r$   Úfÿ  s   zget_rename_function.<locals>.f)r/   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.
    )
r/   r6   r0   r1   r   r   r   r   r   rs   )r]   r#   r#   r$   Úconvert_to_list_like  s
   r­   TÚattrrs   Ú	conditionúGenerator[None, None, None]c              
   c  sR   � |rt | |ƒ}t| ||ƒ z| V  W |rt| ||ƒ dS dS |r(t| ||ƒ 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)   r®   Úvaluer¯   Ú	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)r7   r'   )Údatarµ   r#   r#   r$   Úrequire_length_match8  s   ÿýÿÿr¹   znp.sumznp.maximum.reduceznp.minimum.reducer[   ÚmaxÚminrS   rO   ÚmeanÚprodÚstdÚvarÚmedianÚcumprodÚcumsumrE   r   ú
str | Nonec                 C  s
   t  | ¡S )zH
    if we define an internal function for this argument, return it
    )Ú_cython_tableÚget©rE   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É   Ú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#   )rD   Úir&   r#   r#   r$   rj   ‘  s   & z&fill_missing_names.<locals>.<listcomp>)Ú	enumerate)rÊ   r#   r#   r$   Úfill_missing_names�  s   rÐ   )r+   r   r,   r-   )r,   r-   )r,   rX   ).)r]   r^   r2   r_   r,   r`   )r]   r   r2   r_   r,   r   rB   )rv   rw   r2   r_   r,   r`   )r)   r|   r,   r}   )r,   rˆ   )r!   rX   r,   r-   )rš   r›   r,   r›   )rš   rž   r,   rŸ   )rš   r    )r�   r£   r,   r   )r]   r«   r,   r¬   )T)r®   rs   r¯   r-   r,   r°   )rµ   r   r,   r¶   )rE   r   r,   rÃ   )rÊ   rË   r,   rÌ   )mÚ__doc__Ú
__future__r   ÚbuiltinsÚcollectionsr   r   Úcollections.abcr   r   r   r   r	   Ú
contextlibÚ	functoolsr
   r”   Útypingr   r   r   r   r   rn   Ú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<   r@   rI   rP   rT   rV   rW   r\   rc   ry   r{   r   r…   r‡   rŠ   r‹   rŽ   r“   r™   r�   r¦   rª   r­   Úcontextmanagerr´   r¹   r[   rº   ÚmaximumÚreducer»   ÚminimumrÈ   Ú_builtin_table_aliasrS   rO   Únansumr¼   Únanmeanr½   Únanprodr¾   Únanstdr¿   ÚnanvarrÀ   Ú	nanmedianÚnanmaxÚnanminrÁ   Ú
nancumprodrÂ   Ú	nancumsumrÄ   rÇ   rÉ   rÐ   r#   r#   r#   r$   Ú<module>   sä    
7




ÿ	$ÿ





 
#%
ÿ
 

ýýÿþýüûúùø	÷
öõôóòñðïç
