a
    OfJ                     @  s  d dl mZ d dlmZmZ d dlZd dl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 d dlmZmZ d d	lmZmZmZ d d
lmZ d dlmZm Z  d dl!m"Z"m#Z#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. er>d dl/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8 d dl)m9Z9m:Z: dddddddZ;dddddddZ<dd dd!d"d#d$Z=d%d&dd'd(d)Z>d*d+d,d-d.d/Z?d!d0d1d2d3Z@d%d4d5d5d,d6d7d8ZAG d9d: d:ZBd;dd<d=d>d?ZCd&dd@dAdBZDdCdDdEdFdGZEd;ddEdHdIZFdS )J    )annotations)TYPE_CHECKINGcastN)NaTalgos	internalslib)NA)cache_readonly)find_stack_level)ensure_dtype_can_hold_nafind_common_type)is_1d_only_ea_dtype	is_scalarneeds_i8_conversion)concat_compat)ExtensionDtypeSparseDtype)is_valid_na_for_dtypeisnaisna_all)ensure_wrapped_if_datetimelike)ArrayManager)ensure_block_shapenew_block_2d)BlockManagermake_na_array)Sequence)	ArrayLikeAxisIntDtypeObj	Manager2DShape)Index)BlockBlockPlacementzlist[ArrayManager]zlist[Index]r   r!   )mgrsaxesconcat_axisreturnc                 C  s8   |dkr| d  | |S |dks$J | d | |S dS )z
    Concatenate array managers into one.

    Parameters
    ----------
    mgrs_indexers : list of (ArrayManager, {axis: indexer,...}) tuples
    axes : list of Index
    concat_axis : int

    Returns
    -------
    ArrayManager
       r   N)Zconcat_verticalconcat_horizontal)r&   r'   r(    r,   U/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/core/internals/concat.py_concatenate_array_managersF   s    r.   bool)r'   r(   copyr)   c                   s  |o
|dk}t | d d tr6t|| |}t|||S |dkrZt|| |}|d ||S t| dkr| d d jdkr| d d jd j  t	j
t	jfv rt fdd| D rt| dkrtdd |D }t| | }t|f|S t|| |}t|dkr |d }|jdd}	||	_|	S t|}
g }|
D ]\}}|d }|j}t|rdd	 |D }|jsvt	j|dd
}n4t|jrt|ddd}t|dd}nt|dd
}t|}|jj|jk}nt||d}d}|r|j||d}nt||d}|| q0tt||S )z
    Concatenate block managers into one.

    Parameters
    ----------
    mgrs_indexers : list of (BlockManager, {axis: indexer,...}) tuples
    axes : list of Index
    concat_axis : int
    copy : bool

    Returns
    -------
    BlockManager
    r   c                 3  s   | ]\}}t | V  qd S N)_is_homogeneous_mgr.0mgr_first_dtyper,   r-   	<genexpr>       z'concatenate_managers.<locals>.<genexpr>r*   c                 s  s   | ]}t |V  qd S r1   )lenr4   xr,   r,   r-   r9      r:   F)deepc                 S  s   g | ]}|j jqS r,   )blockvaluesr4   Zjur,   r,   r-   
<listcomp>   r:   z(concatenate_managers.<locals>.<listcomp>axisTrD   Zea_compat_axis   )ndim)r0   )	placement) 
isinstancer   _maybe_reindex_columns_na_proxyr.   r+   r;   nblocksblocksdtypenpfloat64Zfloat32alltuple_concat_homogeneous_fastpathr   r0   r'   _get_combined_planr?   _is_uniform_join_unitsis_extensionconcatenater   r   r   r   r@   _concatenate_join_unitsZmake_block_same_classr   append)mgrs_indexersr'   r(   r0   
needs_copyr&   shapenbr5   outZconcat_planrL   rH   
join_unitsunitblkvalsr@   Zfastpathbr,   r7   r-   concatenate_managers^   s^    


rc   z0list[tuple[BlockManager, dict[int, np.ndarray]]]zlist[BlockManager])r'   rY   rZ   r)   c                 C  sb   g }|D ]T\}}|  D ](\}}|j| | || |ddddd}q|rR|sR| }|| q|S )z
    Reindex along columns so that all of the BlockManagers being concatenated
    have matching columns.

    Columns added in this reindexing have dtype=np.void, indicating they
    should be ignored when choosing a column's final dtype.
    FT)rD   r0   Z
only_sliceZ
allow_dupsZuse_na_proxy)itemsZreindex_indexerr0   rX   )r'   rY   rZ   Znew_mgrsr5   indexersiindexerr,   r,   r-   rJ      s     	rJ   r   r    )r5   r8   r)   c                 C  s<   | j dkrdS | jd }|jjr.|jjjdks2dS |j|kS )zC
    Check if this Manager can be treated as a single ndarray.
    r*   Fr   )rK   rL   mgr_locsis_slice_likeas_slicesteprM   )r5   r8   r`   r,   r,   r-   r2      s    

r2   r"   znp.dtyper$   )r[   r8   r)   c                 C  s  t dd | D rLdd | D }t|j}tt|d }t||}|S tj||d}|tj	krlt
j}nt
j}d}| D ]p\}	}
|	jd }|| }d|
v r||	jd j|
d |dd||f  n|	jd j|dd||f< ||7 }qztt|d }t||}|S )	zl
    With single-Block managers with homogeneous dtypes (that can already hold nan),
    we avoid [...]
    c                 s  s   | ]\}}| V  qd S r1   r,   )r4   r6   re   r,   r,   r-   r9     r:   z/_concat_homogeneous_fastpath.<locals>.<genexpr>c                 S  s   g | ]\}}|j d  jjqS )r   )rL   r@   Tr3   r,   r,   r-   rB     r:   z0_concat_homogeneous_fastpath.<locals>.<listcomp>r   rM   r*   N)rP   rN   rV   rl   libinternalsr%   slicer   emptyrO   libalgosZtake_2d_axis0_float64_float64Ztake_2d_axis0_float32_float32r[   rL   r@   )rY   r[   r8   ZarrsZarrbpr\   Z	take_funcstartr5   re   Zmgr_lenendr,   r,   r-   rR      s2    






rR   z+list[tuple[BlockPlacement, list[JoinUnit]]])r&   r)   c                 C  s   g }| d j d }dd | D }t|}t|D ]Z\}\}}g }t| D ]2\}	}
||	 }t|
|||d}t|}|| qJ|||f q2|S )Nr   c                 S  s   g | ]
}|j qS r,   )blknos)r4   r5   r,   r,   r-   rB   /  r:   z&_get_combined_plan.<locals>.<listcomp>)max_len)r[   rn   Zget_concat_blkno_indexers	enumerate_get_block_for_concat_planJoinUnitrX   )r&   Zplanrv   Zblknos_listpairsindru   rr   Zunits_for_bpkr5   blknor\   r_   r,   r,   r-   rS   (  s    
rS   r%   int)r5   rr   r}   rv   r)   c                C  st   | j | }t|t|jkr8|jjr8|jjjdkr8|}n8| j|j }t	||}t
|trf||}n
||}|S )Nr*   )rL   r;   rh   ri   rj   rk   Zblklocsrg   r   Zmaybe_indices_to_slicerI   ro   Zslice_block_columnsZtake_block_columns)r5   rr   r}   rv   r`   r\   Zax0_blk_indexerZslcr,   r,   r-   rx   B  s    


rx   c                   @  sn   e Zd ZdddddZdddd	Zd
ddddZeddddZeddddZd
ddddZ	dS )ry   r$   None)r?   r)   c                 C  s
   || _ d S r1   )r?   )selfr?   r,   r,   r-   __init___  s    zJoinUnit.__init__str)r)   c                 C  s   t | j dt| j dS )N())type__name__reprr?   )r   r,   r,   r-   __repr__b  s    zJoinUnit.__repr__r    r/   )rM   r)   c                   s   | j s
dS | j}|jjdkr dS |jtkrN|j}t fdd|jddD S |j}|t	u rj|j krjdS |t
u r~t r~dS t| S )z
        Check that we are all-NA of a type/dtype that is compatible with this dtype.
        Augments `self.is_na` with an additional check of the type of NA values.
        FVTc                 3  s   | ]}t | V  qd S r1   )r   r<   rm   r,   r-   r9   s  r:   z,JoinUnit._is_valid_na_for.<locals>.<genexpr>K)order)is_nar?   rM   kindobjectr@   rP   Zravel
fill_valuer   r	   r   r   )r   rM   r`   r@   na_valuer,   rm   r-   _is_valid_na_fore  s    
zJoinUnit._is_valid_na_forc                 C  s   | j }|jjdkrdS |js dS |j}|jdkr4dS t|jtrDdS |jdkrr|d }t	|rft
|sjdS t|S |d d }t	|rt
|sdS tdd |D S d S )Nr   TFr   r*   c                 s  s   | ]}t |V  qd S r1   )r   )r4   rowr,   r,   r-   r9     r:   z!JoinUnit.is_na.<locals>.<genexpr>)r?   rM   r   Z_can_hold_nar@   sizerI   r   rG   r   r   r   rP   )r   r`   r@   valr,   r,   r-   r     s&    

zJoinUnit.is_nac                 C  s   | j }|jjdkrdS dS )z{
        Will self.is_na be True after values.size == 0 deprecation and isna_all
        deprecation are enforced?
        r   TFr?   rM   r   )r   r`   r,   r,   r-   )is_na_after_size_and_isna_all_deprecation  s    z2JoinUnit.is_na_after_size_and_isna_all_deprecationr   )empty_dtyper)   c                 C  s   |d u r| j jjdkr| j jS |}| |rx| j j}|tdkrhttj| j j}|jrh|d d u rhd }t	|| j j
|S | j jS d S )Nr   r   )r   r   )r?   rM   r   r@   r   rN   r   Zndarrayr   r   r[   )r   r   upcasted_nar   Z	blk_dtyper@   r,   r,   r-   get_reindexed_values  s    
zJoinUnit.get_reindexed_valuesN)
r   
__module____qualname__r   r   r   r
   r   r   r   r,   r,   r,   r-   ry   ^  s    
ry   zlist[JoinUnit]r   )r^   r0   r)   c                   s   t | \ }tdd | D }t | fdd| D }tdd |D rvdd |D }t|ddd	}t|d
}nt|dd} |kr |jkrtjdtt	 d |S )zB
    Concatenate values from several join units along axis=1.
    c                 s  s   | ]}|j jjd kV  qdS r   Nr   r4   r_   r,   r,   r-   r9     r:   z*_concatenate_join_units.<locals>.<genexpr>c                   s   g | ]}|j  d qS )r   r   )r   rA   r   r,   r-   rB     s   z+_concatenate_join_units.<locals>.<listcomp>c                 s  s   | ]}t |jV  qd S r1   r   rM   r4   tr,   r,   r-   r9     r:   c                 S  s*   g | ]"}t |jr|n|d ddf qS )r   Nr   r   r,   r,   r-   rB     s   r   TrE   rF   r*   rC   a  The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.)
stacklevel)
_get_empty_dtypeany_dtype_to_na_valuer   r   rM   warningswarnFutureWarningr   )r^   r0   Zempty_dtype_futurehas_none_blocksZ	to_concatZconcat_valuesr,   r   r-   rW     s*    

	rW   rM   r   c                 C  sv   t | tr| jS | jdv r$| dS | jdv r8| dS | jdkrFdS | jdv r^|sXdS tjS | jdkrntjS tdS )	z2
    Find the NA value to go with this dtype.
    ZmMr   fcNaNrb   NiuO)rI   r   r   r   r   rN   nanNotImplementedErrorr   r,   r,   r-   r     s    







r   zSequence[JoinUnit]ztuple[DtypeObj, DtypeObj])r^   r)   c                 C  s   t dd | D r(| d jj}||fS tdd | D }dd | D }t|s^dd | D }t|}|rrt|}|}t|t| krdd | D }t|sd	d | D }t|t|krt|}|rt|}||fS )
z
    Return dtype and N/A values to use when concatenating specified units.

    Returned N/A value may be None which means there was no casting involved.

    Returns
    -------
    dtype
    c                 S  s   g | ]}|j jqS r,   )r?   rM   rA   r,   r,   r-   rB     r:   z$_get_empty_dtype.<locals>.<listcomp>r   c                 s  s   | ]}|j jjd kV  qdS r   r   r   r,   r,   r-   r9     r:   z#_get_empty_dtype.<locals>.<genexpr>c                 S  s   g | ]}|j s|jjqS r,   )r   r?   rM   r   r,   r,   r-   rB     r:   c                 S  s"   g | ]}|j jjd kr|j jqS r   r   r   r,   r,   r-   rB      s   c                 S  s   g | ]}|j s|jjqS r,   )r   r?   rM   r   r,   r,   r-   rB   *  s   c                 S  s"   g | ]}|j jjd kr|j jqS r   r   r   r,   r,   r-   rB   0  s   )r   Zdtypes_all_equalr?   rM   r   r;   r   r   )r^   r   r   ZdtypesrM   Zdtype_futureZdtypes_futurer,   r,   r-   r     s4    
r   c                   sX   | d j   jjdkrdS t fdd| D oVt fdd| D oVtdd | D S )z
    Check if the join units consist of blocks of uniform type that can
    be concatenated using Block.concat_same_type instead of the generic
    _concatenate_join_units (which uses `concat_compat`).

    r   r   Fc                 3  s    | ]}t |jt  u V  qd S r1   )r   r?   rA   firstr,   r-   r9   H  r:   z)_is_uniform_join_units.<locals>.<genexpr>c                 3  s*   | ]"}|j j jkp |j jjd v V  qdS )ZiubNr   rA   r   r,   r-   r9   K  s   c                 s  s   | ]}|j  p|jjV  qd S r1   )r   r?   rU   rA   r,   r,   r-   r9   U  r:   )r?   rM   r   rP   )r^   r,   r   r-   rT   <  s    
rT   )G
__future__r   typingr   r   r   numpyrN   Zpandas._libsr   r   rq   r   rn   r   Zpandas._libs.missingr	   Zpandas.util._decoratorsr
   Zpandas.util._exceptionsr   Zpandas.core.dtypes.castr   r   Zpandas.core.dtypes.commonr   r   r   Zpandas.core.dtypes.concatr   Zpandas.core.dtypes.dtypesr   r   Zpandas.core.dtypes.missingr   r   r   Zpandas.core.constructionr   Z#pandas.core.internals.array_managerr   Zpandas.core.internals.blocksr   r   Zpandas.core.internals.managersr   r   collections.abcr   Zpandas._typingr   r   r    r!   r"   Zpandasr#   r$   r%   r.   rc   rJ   r2   rR   rS   rx   ry   rW   r   r   rT   r,   r,   r,   r-   <module>   sB   l"/k..