U
    MZfX                     @  s   d Z ddlmZ ddlmZmZm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mZ ddlmZ dd	lmZmZ ed
ddZG dd deZG dd deZdddddZdS )zc
Base class for the internal managers. Both BlockManager and ArrayManager
inherit from this class.
    )annotations)LiteralTypeVarfinalN)	ArrayLikeAxisIntDtypeObjShapeAbstractMethodError)find_common_typenp_can_hold_element)PandasObject)Indexdefault_indexTDataManager)boundc                   @  s$  e Zd ZU ded< eddddZedddd	Zeddd
dZeddddZ	eddddddZ
d3dddddddddZed4ddddddddZddddd d!Zed"dd#d$d%Zd5dd&dd'd(d)Zeddd*d+d,Zddd-d.Zddd*d/d0Zddd1d2ZdS )6r   zlist[Index]axesr   returnc                 C  s   t | d S Nr
   self r   >/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/internals/base.pyitems*   s    zDataManager.itemsintc                 C  s
   t | jS r   )lenr   r   r   r   r   __len__.   s    zDataManager.__len__c                 C  s
   t | jS r   )r   r   r   r   r   r   ndim2   s    zDataManager.ndimr	   c                 C  s   t dd | jD S )Nc                 s  s   | ]}t |V  qd S r   )r   ).0Zaxr   r   r   	<genexpr>8   s     z$DataManager.shape.<locals>.<genexpr>)tupler   r   r   r   r   shape6   s    zDataManager.shaper   None)axis
new_labelsr   c                 C  sP   t | j| }t |}|dkr.t | jdkr.n||krLtd| d| dd S )N   r   z#Length mismatch: Expected axis has z elements, new values have z	 elements)r   r   r   
ValueError)r   r&   r'   Zold_lenZnew_lenr   r   r   _validate_set_axis:   s    zDataManager._validate_set_axisNFTr   bool)r   r&   
allow_dupscopy
only_slicer   c                 C  s   t | d S r   r
   )r   Znew_axisindexerr&   
fill_valuer,   r-   r.   r   r   r   reindex_indexerK   s    
zDataManager.reindex_indexer)r   	new_indexr&   r.   r   c                 C  s*   | j | |\}}| j||||d|dS )z4
        Conform data manager to new index.
        F)r&   r0   r-   r.   )r   Zreindexr1   )r   r2   r&   r0   r.   r/   r   r   r   reindex_axisW   s    zDataManager.reindex_axis)r   otherr   c                 C  s   t | dS )z
        To be implemented by the subclasses. Only check the column values
        assuming shape and indexes have already been checked.
        Nr
   )r   r4   r   r   r   _equal_valuesm   s    zDataManager._equal_valuesobject)r4   r   c                 C  sV   t |tsdS | j|j }}t|t|kr0dS tdd t||D sLdS | |S )z5
        Implementation for DataFrame.equals
        Fc                 s  s   | ]\}}| |V  qd S r   )equals)r!   Zax1Zax2r   r   r   r"      s     z%DataManager.equals.<locals>.<genexpr>)
isinstancer   r   r   allzipr5   )r   r4   Z	self_axesZ
other_axesr   r   r   r7   t   s    
zDataManager.equalszlist[str] | None)r   
align_keysr   c                 K  s   t | d S r   r
   )r   fr;   kwargsr   r   r   apply   s    zDataManager.apply)r   r   c                 C  s   | j d|dS )Nr>   )func)r>   )r   r?   r   r   r   isna   s    zDataManager.isnac                 C  s   dS )NTr   r   r   r   r   is_consolidated   s    zDataManager.is_consolidatedc                 C  s   | S r   r   r   r   r   r   consolidate   s    zDataManager.consolidatec                 C  s   d S r   r   r   r   r   r   _consolidate_inplace   s    z DataManager._consolidate_inplace)NFTF)NF)N)__name__
__module____qualname____annotations__propertyr   r   r   r    r$   r*   r1   r3   r5   r7   r>   r@   rA   rB   rC   r   r   r   r   r   %   s<   
       c                   @  s^   e Zd ZeddddZeeddddZddd	d
Zdd Ze	dddddZ
dS )SingleDataManagerz
Literal[1]r   c                 C  s   dS )Nr(   r   r   r   r   r   r       s    zSingleDataManager.ndimr   c                 C  s
   | j d S )zW
        Quick access to the backing array of the Block or SingleArrayManager.
        r   )Zarraysr   r   r   r   array   s    zSingleDataManager.arrayr%   c                 C  sT   | j }t|tjrt|j|}t|tjrH|jdkrHt|dkrH|d }|||< dS )a'  
        Set values with indexer.

        For Single[Block/Array]Manager, this backs s[indexer] = value

        This is an inplace version of `setitem()`, mutating the manager/values
        in place, not returning a new Manager (and Block), and thus never changing
        the dtype.
        r(   )r   .N)rJ   r8   npZndarrayr   Zdtyper    r   )r   r/   valuearrr   r   r   setitem_inplace   s    
"z!SingleDataManager.setitem_inplacec                 C  s.   | j }||}tt|}t| ||}|S r   )rJ   r   r   type
from_array)r   r?   rM   resindexZmgrr   r   r   grouped_reduce   s
    z SingleDataManager.grouped_reducer   )rM   rR   c                 C  s   t | d S r   r
   )clsrM   rR   r   r   r   rP      s    zSingleDataManager.from_arrayN)rD   rE   rF   rH   r    r   rJ   rN   rS   classmethodrP   r   r   r   r   rI      s   rI   zlist[DtypeObj]zDtypeObj | None)dtypesr   c                 C  s   t | sdS t| S )z
    Find the common dtype for `blocks`.

    Parameters
    ----------
    blocks : List[DtypeObj]

    Returns
    -------
    dtype : np.dtype, ExtensionDtype, or None
        None is returned when `blocks` is empty.
    N)r   r   )rV   r   r   r   interleaved_dtype   s    rW   )__doc__
__future__r   typingr   r   r   ZnumpyrK   Zpandas._typingr   r   r   r	   Zpandas.errorsr   Zpandas.core.dtypes.castr   r   Zpandas.core.baser   Zpandas.core.indexes.apir   r   r   r   rI   rW   r   r   r   r   <module>   s   x3