a
    DBìjº(  ã                   @  s~  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	m
Z
 d dlmZ d dlm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 d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ eràd dl%m&Z& e 'd¡Z(g d¢Z)d3ddddddœdd„Z*dddœdd„Z+d4ddddddœd d!„Z,ddd"œd#d$„Z-d5d%dd&œd'd(„Z.d)d*„ Z/dd+œd,d-„Z0d.d/d0œd1d2„Z1dS )6é    )ÚannotationsN)ÚTYPE_CHECKINGÚcast)ÚNaTÚlib)ÚInvalidIndexError©Úfind_common_type)Ú	safe_sort)ÚIndexÚ
_new_IndexÚensure_indexÚensure_index_from_sequencesÚget_unanimous_names)ÚCategoricalIndex)ÚDatetimeIndex)ÚIntervalIndex)Ú
MultiIndex)ÚPeriodIndex)Ú
RangeIndex)ÚTimedeltaIndex)ÚAxiszôSorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.
)r   r   r   r   r   r   r   r   r   r   r   r   r   Úget_objs_combined_axisÚunion_indexesr   Úall_indexes_sameÚdefault_indexÚsafe_sort_indexFTÚboolr   r   )Ú	intersectÚaxisÚsortÚcopyÚreturnc                   s"   ‡ fdd„| D ƒ}t ||||d�S )añ  
    Extract combined index: return intersection or union (depending on the
    value of "intersect") of indexes on given axis, or None if all objects
    lack indexes (e.g. they are numpy arrays).

    Parameters
    ----------
    objs : list
        Series or DataFrame objects, may be mix of the two.
    intersect : bool, default False
        If True, calculate the intersection between indexes. Otherwise,
        calculate the union.
    axis : {0 or 'index', 1 or 'outer'}, default 0
        The axis to extract indexes from.
    sort : bool, default True
        Whether the result index should come out sorted or not.
    copy : bool, default False
        If True, return a copy of the combined index.

    Returns
    -------
    Index
    c                   s   g | ]}|  ˆ ¡‘qS © )Z	_get_axis)Ú.0Úobj©r   r#   úV/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/indexes/api.pyÚ
<listcomp>f   ó    z*get_objs_combined_axis.<locals>.<listcomp>)r   r    r!   )Ú_get_combined_index)Úobjsr   r   r    r!   Z	obs_idxesr#   r&   r'   r   H   s    r   zlist[Index])r+   r"   c                 C  s<   t ƒ }g }| D ](}t|ƒ|vr| t|ƒ¡ | |¡ q|S )z^
    Return a list with distinct elements of "objs" (different ids).
    Preserves order.
    )ÚsetÚidÚaddÚappend)r+   ZidsÚresr%   r#   r#   r'   Ú_get_distinct_objsj   s    r1   )Úindexesr   r    r!   r"   c                 C  sŽ   t | ƒ} t| ƒdkrtg ƒ}nTt| ƒdkr4| d }n>|r^| d }| dd… D ]}| |¡}qLnt| dd�}t|ƒ}|r~t|ƒ}|rŠ| ¡ }|S )a  
    Return the union or intersection of indexes.

    Parameters
    ----------
    indexes : list of Index or list objects
        When intersect=True, do not accept list of lists.
    intersect : bool, default False
        If True, calculate the intersection between indexes. Otherwise,
        calculate the union.
    sort : bool, default False
        Whether the result index should come out sorted or not.
    copy : bool, default False
        If True, return a copy of the combined index.

    Returns
    -------
    Index
    r   é   NF©r    )r1   Úlenr   Úintersectionr   r   r   r!   )r2   r   r    r!   ÚindexÚotherr#   r#   r'   r*   x   s     

r*   )r7   r"   c                 C  sv   | j r
| S zt| ƒ}W n ty(   Y nJ0 t|tƒr8|S ttj|ƒ}t| tƒr`tj	|| j
d�} nt|| j| jd�} | S )zª
    Returns the sorted index

    We keep the dtypes and the name attributes.

    Parameters
    ----------
    index : an Index

    Returns
    -------
    Index
    )Únames)ÚnameÚdtype)Zis_monotonic_increasingr
   Ú	TypeErrorÚ
isinstancer   r   ÚnpZndarrayr   Úfrom_tuplesr9   r:   r;   )r7   Zarray_sortedr#   r#   r'   r   ¨   s    

r   zbool | None)r    r"   c                   sº  t ˆƒdkrtdƒ‚t ˆƒdkrPˆd }t|tƒrLˆs@t|ƒ}ntt|ƒƒ}|S tˆƒ\‰}ddœ‡fdd„}‡fdd	„}|d
k�r:ˆd }dd„ ˆD ƒ}dd„ |D ƒ}t |ƒdt |ƒfvrÄtdƒ‚t |ƒt ˆƒkrâd‰ˆd }n(t |ƒdk�r
d‰dd„ ˆD ƒ‰ˆd }ˆdd… D ]}|j|ˆ�r*dndd�}�q|S |dk�r¤|ˆƒ}	ˆd ‰ t	‡ fdd„ˆdd… D ƒƒ�s~|ˆ|	ƒ‰ t
ˆŽ d }
|
ˆ jk�r ˆ  |
¡‰ ˆ S |ˆƒ}	|ˆ|	ƒS dS )a)  
    Return the union of indexes.

    The behavior of sort and names is not consistent.

    Parameters
    ----------
    indexes : list of Index or list objects
    sort : bool, default True
        Whether the result index should come out sorted or not.

    Returns
    -------
    Index
    r   z#Must have at least 1 Index to unionr3   r   ©r"   c                   sª   t dd„ | D ƒƒr~‡fdd„| D ƒ} | d  ¡ }| d  | dd… ¡}|| |¡d	k }t|ƒrn| | ¡ ¡}ˆrz| ¡ }|S d
d„ ‰ ttj‡ fdd„| D ƒˆd�ˆd�S )zô
        Concatenate indices and remove duplicates.

        Parameters
        ----------
        inds : list of Index or list objects
        dtype : dtype to set for the resulting Index

        Returns
        -------
        Index
        c                 s  s   | ]}t |tƒV  qd S ©N)r=   r   ©r$   Úindr#   r#   r'   Ú	<genexpr>ô   r)   z9union_indexes.<locals>._unique_indices.<locals>.<genexpr>c                   s   g | ]}|j ˆ d d�‘qS ©F)r!   )ÚastyperB   ©r;   r#   r'   r(   õ   r)   z:union_indexes.<locals>._unique_indices.<locals>.<listcomp>r   r3   é   Néÿÿÿÿc                 S  s   t | tƒr|  ¡ } | S rA   )r=   r   Útolist)Úir#   r#   r'   Úconvÿ   s    
z4union_indexes.<locals>._unique_indices.<locals>.convc                   s   g | ]}ˆ |ƒ‘qS r#   r#   )r$   rK   )rL   r#   r'   r(     r)   r4   rG   )	ÚallÚuniquer/   Zget_indexer_forr5   Zsort_valuesr   r   Zfast_unique_multiple_list)Úindsr;   Úresultr8   Údiffr4   )rL   r;   r'   Ú_unique_indicesç   s    þz&union_indexes.<locals>._unique_indicesc                   s$   dd„ ˆ D ƒ}|rt |ƒ}nd}|S )a  
        Finds a common type for the indexes to pass through to resulting index.

        Parameters
        ----------
        inds: list of Index or list objects

        Returns
        -------
        The common type or None if no indexes were given
        c                 S  s   g | ]}t |tƒr|j‘qS r#   )r=   r   r;   )r$   Úidxr#   r#   r'   r(     r)   zCunion_indexes.<locals>._find_common_index_dtype.<locals>.<listcomp>Nr   )rO   Zdtypesr;   )r2   r#   r'   Ú_find_common_index_dtype	  s
    
z/union_indexes.<locals>._find_common_index_dtypeÚspecialc                 S  s   g | ]}t |tƒr|‘qS r#   )r=   r   ©r$   Úxr#   r#   r'   r(      r)   z!union_indexes.<locals>.<listcomp>c                 S  s   g | ]}|j d ur|‘qS rA   )ÚtzrV   r#   r#   r'   r(   !  r)   z0Cannot join tz-naive with tz-aware DatetimeIndexTFc                 S  s   g | ]}|j td d�‘qS rE   )rF   ÚobjectrV   r#   r#   r'   r(   4  r)   Nr4   Úarrayc                 3  s   | ]}ˆ   |¡V  qd S rA   ©Úequals)r$   r8   )r7   r#   r'   rD   >  r)   z union_indexes.<locals>.<genexpr>)r5   ÚAssertionErrorr=   Úlistr   ÚsortedÚ_sanitize_and_checkr<   ÚunionrM   r   r:   Úrename)r2   r    rP   ÚkindrR   rT   ZdtisZdti_tzsr8   r;   r:   r#   )r7   r2   r    r'   r   Ê   sN    

"


 

r   c                 C  sp   t dd„ | D ƒƒ}t |v rHt|ƒdkr@dd„ | D ƒ} | t ¡ n| dfS t|ƒdks\t|vrd| dfS | dfS d	S )
aN  
    Verify the type of indexes and convert lists to Index.

    Cases:

    - [list, list, ...]: Return ([list, list, ...], 'list')
    - [list, Index, ...]: Return _sanitize_and_check([Index, Index, ...])
        Lists are sorted and converted to Index.
    - [Index, Index, ...]: Return ([Index, Index, ...], TYPE)
        TYPE = 'special' if at least one special type, 'array' otherwise.

    Parameters
    ----------
    indexes : list of Index or list objects

    Returns
    -------
    sanitized_indexes : list of Index or list objects
    type : {'list', 'array', 'special'}
    c                 S  s   h | ]}t |ƒ’qS r#   )Útype©r$   r7   r#   r#   r'   Ú	<setcomp>_  r)   z&_sanitize_and_check.<locals>.<setcomp>r3   c                 S  s&   g | ]}t |tƒstt|ƒƒn|‘qS r#   )r=   r   r^   rV   r#   r#   r'   r(   c  s   z'_sanitize_and_check.<locals>.<listcomp>r^   rU   rZ   N)r^   r5   Úremover   )r2   Úkindsr#   r#   r'   r`   J  s    ÿr`   r@   c                   s&   t | ƒ}t|ƒ‰ t‡ fdd„|D ƒƒS )zî
    Determine if all indexes contain the same elements.

    Parameters
    ----------
    indexes : iterable of Index objects

    Returns
    -------
    bool
        True if all indexes contain the same elements, False otherwise.
    c                 3  s   | ]}ˆ   |¡V  qd S rA   r[   re   ©Úfirstr#   r'   rD     r)   z#all_indexes_same.<locals>.<genexpr>)ÚiterÚnextrM   )r2   Úitrr#   ri   r'   r   p  s    r   Úintr   )Únr"   c                 C  s   t | ƒ}tj|d d�S )N)r:   )Úranger   Z_simple_new)ro   Úrngr#   r#   r'   r   ‚  s    r   )Fr   TF)FFF)T)2Ú
__future__r   ÚtextwrapÚtypingr   r   Únumpyr>   Zpandas._libsr   r   Zpandas.errorsr   Zpandas.core.dtypes.castr	   Zpandas.core.algorithmsr
   Zpandas.core.indexes.baser   r   r   r   r   Zpandas.core.indexes.categoryr   Zpandas.core.indexes.datetimesr   Zpandas.core.indexes.intervalr   Zpandas.core.indexes.multir   Zpandas.core.indexes.periodr   Zpandas.core.indexes.ranger   Zpandas.core.indexes.timedeltasr   Zpandas._typingr   ÚdedentZ	_sort_msgÚ__all__r   r1   r*   r   r   r`   r   r   r#   r#   r#   r'   Ú<module>   sJ   ÿ    û"   ü0" &