U
    f/e#                     @  sZ  d dl mZ d dlZd dlmZ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mZmZmZ d dlmZ d dlmZ d dlm Z  e!dZ"dddddddddddddddddd d!d"d#gZ#d6d&d&d&dd'd(d Z$d)d)d*d+d,Z%d7d)d&d&d&dd-d.d/Z&d8d&dd0d1d!Z'd2d3 Z(d&d4d5d#Z)dS )9    )annotationsN)NaTlib)InvalidIndexError)Index
_new_Indexensure_indexensure_index_from_sequencesget_unanimous_names)CategoricalIndex)DatetimeIndex)IntervalIndex)
MultiIndex)Float64Index
Int64IndexNumericIndexUInt64Index)PeriodIndex)
RangeIndex)TimedeltaIndexzSorting 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   r   r   r   r	   get_objs_combined_axisunion_indexesr
   all_indexes_sameFTbool)	intersect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axisr   ;/tmp/pip-unpacked-wheel-tiezk1ph/pandas/core/indexes/api.py
<listcomp>_   s     z*get_objs_combined_axis.<locals>.<listcomp>)r   r   r   )_get_combined_index)objsr   r"   r   r   Z	obs_idxesr   r!   r#   r   E   s    zlist[Index])r&   r   c                 C  s<   t  }g }| D ](}t||kr|t| || q|S )z^
    Return a list with distinct elements of "objs" (different ids).
    Preserves order.
    )setidaddappend)r&   idsresr    r   r   r#   _get_distinct_objsc   s    r-   )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}t|}|rz| }W n tk
r   Y nX |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      Nr   )	r-   lenr   intersectionr   r   Zsort_values	TypeErrorr   )r.   r   r   r   indexotherr   r   r#   r%   q   s&    

r%   )r   r   c                   s  t | dkrtdt | dkrB| d }t|tr>tt|}|S t| \} }ddfdd}|dkr| d }t|d	r|| dd
 S | dd
 D ]}|	|}q|S nf|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 unionr/   r   r   c                   s(   dd  t tj fdd| D dS )z
        Convert indexes to lists and concatenate them, removing duplicates.

        The final dtype is inferred.

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

        Returns
        -------
        Index
        c                 S  s   t | tr|  } | S N)
isinstancer   tolist)ir   r   r#   conv   s    
z4union_indexes.<locals>._unique_indices.<locals>.convc                   s   g | ]} |qS r   r   )r   r:   r;   r   r#   r$      s     z:union_indexes.<locals>._unique_indices.<locals>.<listcomp>r0   )r   r   Zfast_unique_multiple_list)Zindsr0   r<   r#   _unique_indices   s    z&union_indexes.<locals>._unique_indicesspecial
union_manyNarrayc                 3  s   | ]}  |V  qd S r7   equals)r   r5   )r4   r   r#   	<genexpr>   s     z union_indexes.<locals>.<genexpr>)r1   AssertionErrorr8   listr   sorted_sanitize_and_checkhasattrr?   unionallr
   namerename)r.   r   resultkindr=   r5   rK   r   )r4   r   r#   r      s2    



c                 C  sp   t dd | D }t |krHt|dkr@dd | D } |t  n| dfS t|dks\t|k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   r4   r   r   r#   	<setcomp>  s     z&_sanitize_and_check.<locals>.<setcomp>r/   c                 S  s&   g | ]}t |tstt|n|qS r   )r8   r   rE   )r   xr   r   r#   r$     s    z'_sanitize_and_check.<locals>.<listcomp>rE   r>   r@   N)rE   r1   remover   )r.   kindsr   r   r#   rG      s    rG   r6   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 r7   rA   rP   firstr   r#   rC   !  s     z#all_indexes_same.<locals>.<genexpr>)iternextrJ   )r.   itrr   rU   r#   r     s    )Fr   TF)FFF)T)*
__future__r   textwrapZpandas._libsr   r   Zpandas.errorsr   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.numericr   r   r   r   Zpandas.core.indexes.periodr   Zpandas.core.indexes.ranger   Zpandas.core.indexes.timedeltasr   dedentZ	_sort_msg__all__r   r-   r%   r   rG   r   r   r   r   r#   <module>   sd             4G&