U
    Mf                    @  s  d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
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" d dl#m$Z$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/m0Z0 d dl1m2  m3Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z= d dl>m?Z?m@Z@mAZA d dlBmCZCmDZDmEZE d dlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN d dlOmPZPmQZQ edddZRG dd deCZSG dd dejTeSZTG dd deSeDZUdPd!d"d#d#dd$d%d&ZVdQd'd"d#dd(d)d*ZWdRd+d,d"d-d.d/d0ZXd1d2d3d4d5ZYd'd#d!d6d7d8ZZd!d9d:d;Z[d<d=d>d?Z\d@d!dAdBdCZ]d!dDd#d!dEdFdGZ^dHdIdJdKZ_dLd+d#dMdNdOZ`dS )S    )annotationsN)AnyCallableHashableSequenceTypeVarcast)	internalslibBlockPlacement)	ArrayLikeDtypeObjShapenpttype_t)PerformanceWarning)cache_readonly)find_stack_level)validate_bool_kwarg)infer_dtype_from_scalar)ensure_platform_intis_1d_only_ea_dtypeis_dtype_equalis_list_likeneeds_i8_conversion)ExtensionDtype)ABCDataFrame	ABCSeries)array_equalsisna)NDArrayBackedExtensionArray)SparseDtype)ensure_wrapped_if_datetimelikeextract_array)maybe_convert_indices)Float64IndexIndexensure_index)DataManagerSingleDataManagerinterleaved_dtype)BlockDatetimeTZBlock
NumpyBlockensure_block_shapeextend_blocksget_block_type	new_blocknew_block_2d)blockwise_alloperate_blockwiseTBaseBlockManager)boundc                   @  s`  e Zd ZU dZdZded< ded< ded< ded	< d
ed< ded< ded< dddddZedddddddZe	ddddZ
e	ddddZddddd d!Zddd"d#ZeZd
d
d$d%d&Zd
d'd(d)d*d+Ze	ddd,d-Ze	d'dd.d/Zd0d1 Ze	d2dd3d4Zd5dd6d7Zddd9ddd:d;d<Zdddd=d>d?Zdddd@dAZdddBdCdDZdd
d
ddEdFdGZddddHdIZdd
d
ddJdKdLZddddMdNdOZdddd5ddQdRdSZddddddddTdUdVZddddMdWdXZ dYdZ Z!ddd[d[dddd\d]d^Z"dddd_d`Z#e	dddadbZ$e	dddcddZ%e	dddedfZ&ddgddhdidjZ'dddddkdldmZ(dddddkdndoZ)dddddpddqdrdsZ*e	d
ddtduZ+dddddvdwZ,ddddxdyZ-dd8dzdd'd
ddddddd{	d|d}Z.e/j0d8fd8dzd~ddddddZ1dddddddZ2ddd
dddddZ3dS )r7   a  
    Core internal data structure to implement DataFrame, Series, etc.

    Manage a bunch of labeled 2D mixed-type ndarrays. Essentially it's a
    lightweight blocked set of labeled data to be manipulated by the DataFrame
    public API class

    Attributes
    ----------
    shape
    ndim
    axes
    values
    items

    Methods
    -------
    set_axis(axis, new_labels)
    copy(deep=True)

    get_dtypes

    apply(func, axes, block_filter_fn)

    get_bool_data
    get_numeric_data

    get_slice(slice_like, axis)
    get(label)
    iget(loc)

    take(indexer, axis)
    reindex_axis(new_labels, axis)
    reindex_indexer(new_labels, indexer, axis)

    delete(label)
    insert(loc, label, value)
    set(label, value)

    Parameters
    ----------
    blocks: Sequence of Block
    axes: Sequence of Index
    verify_integrity: bool, default True

    Notes
    -----
    This is *not* a public API class
     npt.NDArray[np.intp]_blknos_blklocstuple[Block, ...]blockslist[Index]axesintndimbool_known_consolidated_is_consolidatedTverify_integrityc                 C  s   t d S NNotImplementedError)selfr>   r@   rG   r9   r9   B/tmp/pip-unpacked-wheel-eb6vo0j3/pandas/core/internals/managers.py__init__   s    zBaseBlockManager.__init__z	type_t[T]list[Block]r6   )clsr>   r@   returnc                 C  s   t d S rH   rI   rO   r>   r@   r9   r9   rL   from_blocks   s    zBaseBlockManager.from_blocksrP   c                 C  s   | j dkr|   | j S )a
  
        Suppose we want to find the array corresponding to our i'th column.

        blknos[i] identifies the block from self.blocks that contains this column.

        blklocs[i] identifies the column of interest within
        self.blocks[self.blknos[i]]
        N)r;   _rebuild_blknos_and_blklocsrK   r9   r9   rL   blknos   s    

zBaseBlockManager.blknosc                 C  s   | j dkr|   | j S )z$
        See blknos.__doc__
        N)r<   rT   rU   r9   r9   rL   blklocs   s    
zBaseBlockManager.blklocsN)rK   rP   c                 C  s   |dkr t g g| jdd  }| jdkrtt| ts8t| jd }|jdd }tt	dd}|j
||d}|g}ng }t| ||S )z9return an empty BlockManager with the items axis of len 0N   r   	placement)r'   r@   rB   
isinstanceSingleBlockManagerAssertionErrorr>   valuesr   sliceZmake_block_same_classtyperR   )rK   r@   blkarrbpnbr>   r9   r9   rL   
make_empty   s    

zBaseBlockManager.make_emptyc                 C  s   dS NTr9   rU   r9   r9   rL   __nonzero__   s    zBaseBlockManager.__nonzero__)axisrP   c                 C  s   | j dkr|dkrdnd}|S )N   r   rX   rB   )rK   rh   r9   r9   rL   _normalize_axis   s    
z BaseBlockManager._normalize_axisr'   None)rh   
new_labelsrP   c                 C  s   |  || || j|< d S rH   )Z_validate_set_axisr@   )rK   rh   rm   r9   r9   rL   set_axis   s    zBaseBlockManager.set_axisc                 C  s   t | jdkS )NrX   lenr>   rU   r9   r9   rL   is_single_block   s    z BaseBlockManager.is_single_blockc                 C  s
   | j d S Nr   r@   rU   r9   r9   rL   items   s    zBaseBlockManager.itemsc                 C  s"   t dd | jD }|| jS )Nc                 S  s   g | ]
}|j qS r9   dtype.0ra   r9   r9   rL   
<listcomp>   s     z/BaseBlockManager.get_dtypes.<locals>.<listcomp>)nparrayr>   takerV   rK   Zdtypesr9   r9   rL   
get_dtypes   s    zBaseBlockManager.get_dtypeslist[ArrayLike]c                 C  s   dd | j D S )a2  
        Quick access to the backing arrays of the Blocks.

        Only for compatibility with ArrayManager for testing convenience.
        Not to be used in actual code, and return value is not the same as the
        ArrayManager method (list of 1D arrays vs iterator of 2D ndarrays / 1D EAs).
        c                 S  s   g | ]
}|j qS r9   r^   rw   r9   r9   rL   ry      s     z+BaseBlockManager.arrays.<locals>.<listcomp>r>   rU   r9   r9   rL   arrays   s    	zBaseBlockManager.arraysstrc                 C  sh   t | j}t| jD ]4\}}|dkr4|d| 7 }q|d| d| 7 }q| jD ]}|d| 7 }qP|S )Nr   z
Items: z
Axis z: 
)r`   __name__	enumerater@   r>   )rK   outputiaxblockr9   r9   rL   __repr__   s    

zBaseBlockManager.__repr__Fzlist[str] | None)rK   
align_keysignore_failuresrP   c              
     s(  d kst |pg }g } fdd|D }| jD ]}|r| D ]d\}}	t|	ttfr|	jdkrv|	j|jj	 j
 |< q|	jdd|jj	f j
 |< q@|	|jj	  |< q@z,t|r|j|f }
nt||f  }
W n" ttfk
r   |s Y q0Y nX t|
|}q0|r| |S t| || j}|S )a  
        Iterate over the blocks, collect and create a new BlockManager.

        Parameters
        ----------
        f : str or callable
            Name of the Block method to apply.
        align_keys: List[str] or None, default None
        ignore_failures: bool, default False
        **kwargs
            Keywords to pass to `f`

        Returns
        -------
        BlockManager
        filterc                   s   i | ]}| | qS r9   r9   )rx   kkwargsr9   rL   
<dictcomp>  s      z*BaseBlockManager.apply.<locals>.<dictcomp>rX   N)r]   r>   rt   r[   r   r   rB   ilocmgr_locsindexerZ_valuescallableapplygetattr	TypeErrorrJ   r0   _combiner`   rR   r@   )rK   fr   r   r   result_blocksZaligned_argsbr   objappliedoutr9   r   rL   r      s2    



zBaseBlockManager.apply)rK   alignrP   c                 C  s2   |rddg}ndg}t |dd}| jd|||dS )NothercondTZextract_numpywhere)r   r   r   r$   r   )rK   r   r   r   r   r9   r9   rL   r   =  s    
zBaseBlockManager.wherec                 C  s   | j d||dS )zi
        Set values with indexer.

        For SingleBlockManager, this backs s[indexer] = value
        setitem)r   valuer   )rK   r   r   r9   r9   rL   r   K  s    zBaseBlockManager.setitem)r   c                 C  s2   |rddg}ndg}t |dd}| jd|||dS )NnewmaskTr   putmask)r   r   r   r   )rK   r   r   r   r   r9   r9   rL   r   S  s    
zBaseBlockManager.putmask)rK   nrh   rP   c                 C  s   |  |}| jd||dS )Ndiff)r   rh   )rk   r   )rK   r   rh   r9   r9   rL   r   b  s    
zBaseBlockManager.diffc                 K  s   | j d|S )Ninterpolate)r   r   rK   r   r9   r9   rL   r   f  s    zBaseBlockManager.interpolate)rK   periodsrh   rP   c                 C  s   |  |}|tjkrd }|dkr| jdkr| jdks^| jd |st|r^t| jd j	s| j
d }t|}t||}|dkrtjdg| tt||  tjd}n$tjtt||dg|  tjd}| j| j|d|ddd}|S | jd	|||d
S )Nr   ri   rX   ru   TF)rh   
fill_value
allow_dupsconsolidateshift)r   rh   r   )rk   r
   
no_defaultrB   nblocksr>   Z_can_hold_element
is_integerr   rv   shapeabsminrz   r{   listrangeintpreindex_indexerrt   r   )rK   r   rh   r   ZncolsZnperr   resultr9   r9   rL   r   i  sJ    



  zBaseBlockManager.shift)rK   inplacerP   c                 C  s   | j d||||dS )Nfillna)r   limitr   downcastr   )rK   r   r   r   r   r9   r9   rL   r     s        zBaseBlockManager.fillnaraise)rK   copyerrorsrP   c                 C  s   | j d|||dS )Nastype)rv   r   r   r   )rK   rv   r   r   r9   r9   rL   r     s    zBaseBlockManager.astype)rK   r   datetimenumeric	timedeltarP   c                 C  s   | j d||||dS )Nconvert)r   r   r   r   r   )rK   r   r   r   r   r9   r9   rL   r     s    zBaseBlockManager.convertc                 C  s4   t |d}t|rtt|r"t| jd|||dS )Nr   replace)
to_replacer   r   )r   r   r]   r   )rK   r   r   r   r9   r9   rL   r     s    
   zBaseBlockManager.replacec                 K  s   | j d|S )N_replace_regex)r   r   r   r9   r9   rL   replace_regex  s    zBaseBlockManager.replace_regexz	list[Any])rK   src_list	dest_listr   regexrP   c                 C  s*   t |d}| jd||||d}|  |S )zdo a list replacer   replace_list)r   r   r   r   )r   r   _consolidate_inplace)rK   r   r   r   r   bmr9   r9   rL   r     s    
zBaseBlockManager.replace_listc                 K  s   | j d|S )z}
        Convert values to native types (strings / python objects) that are used
        in formatting (repr / csv).
        to_native_types)r   r   r   r9   r9   rL   r     s    z BaseBlockManager.to_native_typesc                 C  s   t dd | jD S )Nc                 s  s   | ]}|j V  qd S rH   
is_numericrx   r   r9   r9   rL   	<genexpr>  s     z9BaseBlockManager.is_numeric_mixed_type.<locals>.<genexpr>)allr>   rU   r9   r9   rL   is_numeric_mixed_type  s    z&BaseBlockManager.is_numeric_mixed_typec                 C  s   t dd | jD S )z>Whether any of the blocks in this manager are extension blocksc                 s  s   | ]}|j V  qd S rH   is_extensionr   r9   r9   rL   r     s     z7BaseBlockManager.any_extension_types.<locals>.<genexpr>)anyr>   rU   r9   r9   rL   any_extension_types  s    z$BaseBlockManager.any_extension_typesc                 C  s   t | jdkr| jd jS dS )z8return a boolean if we are a single block and are a viewrX   r   F)rp   r>   is_viewrU   r9   r9   rL   r     s    	zBaseBlockManager.is_viewr   )rK   	predicaterP   c                   s"    fdd| j D }| j|ddS )Nc                   s   g | ]} |j r|qS r9   r   rw   r   r9   rL   ry     s     
 z5BaseBlockManager._get_data_subset.<locals>.<listcomp>Fr   )r>   r   )rK   r   r>   r9   r   rL   _get_data_subset  s    z!BaseBlockManager._get_data_subset)rK   r   rP   c                 C  sZ   g }| j D ]B}|jtkr$|| q
|jr
| }|D ]}|jr6|| q6q
| ||S )z
        Select blocks that are bool-dtype and columns from object-dtype blocks
        that are all-bool.

        Parameters
        ----------
        copy : bool, default False
            Whether to copy the blocks
        )r>   rv   rC   append	is_object_splitZis_boolr   )rK   r   
new_blocksra   nbsrd   r9   r9   rL   get_bool_data  s    

zBaseBlockManager.get_bool_datac                 C  sB   dd | j D }t|t| j kr6|r2| jddS | S | ||S )zy
        Parameters
        ----------
        copy : bool, default False
            Whether to copy the blocks
        c                 S  s   g | ]}|j r|qS r9   r   rw   r9   r9   rL   ry     s      z5BaseBlockManager.get_numeric_data.<locals>.<listcomp>Tdeep)r>   rp   r   r   )rK   r   Znumeric_blocksr9   r9   rL   get_numeric_data  s    z!BaseBlockManager.get_numeric_datazIndex | None)rK   r>   r   indexrP   c           	      C  s   t |dkrb| jdkrZ|dk	r2| jdd |g}n| jdd g| jdd  }| |S |  S ttdd |D }t	|| j
d }g }|D ],}|j|d}t||jj |_|| qt| j}|dk	r||d< | j||d< t| ||S )	z$return a new manager with the blocksr   ri   NrX   c                 S  s   g | ]}|j jqS r9   r   as_arrayrx   r   r9   r9   rL   ry   .  s     z-BaseBlockManager._combine.<locals>.<listcomp>r   r   )rp   rB   rt   r@   re   rz   sortconcatenater
   Zget_reverse_indexerr   r   r   r   r   r   r   r|   r`   rR   )	rK   r>   r   r   r@   r   Zinv_indexerr   r   r9   r9   rL   r     s&    


zBaseBlockManager._combinec                 C  s
   t | jS rH   ro   rU   r9   r9   rL   r   >  s    zBaseBlockManager.nblocksc                   s   r&fdd  fdd| j D }n
t| j }| jdd}||_ | jdkrr| j}|dk	rr| |_| j |_r~|  |S )	a>  
        Make deep or shallow copy of BlockManager

        Parameters
        ----------
        deep : bool or string, default True
            If False, return shallow copy (do not copy data)
            If 'all', copy data and a deep copy of the index

        Returns
        -------
        BlockManager
        c                   s    dkr| j ddS |  S )Nr   Tr   )r   view)r   r   r9   rL   	copy_funcT  s    z(BaseBlockManager.copy.<locals>.copy_funcc                   s   g | ]} |qS r9   r9   rx   r   )r   r9   rL   ry   W  s     z)BaseBlockManager.copy.<locals>.<listcomp>r   r   rX   N)r@   r   r   rB   r;   r   r<   r   )rK   r   new_axesresrV   r9   )r   r   rL   r   B  s    


zBaseBlockManager.copyc                 C  s4   |   r| S t| | j| jdd}d|_|  |S )zr
        Join together blocks having same dtype

        Returns
        -------
        y : BlockManager
        FrF   )is_consolidatedr`   r>   r@   rE   r   )rK   r   r9   r9   rL   r   j  s    zBaseBlockManager.consolidate)use_na_proxy)	rK   new_axisrh   r   r   r   
only_slicer   rP   c	                  s   dkrD|| j | kr|s| S | j|d}
t| j |
_ ||
j |< |
S |rP|   |sd| j |  || jkrvtd|dkr| j ||	d}n fdd| jD }t| j }|||< t	| 
||}|dkr| j |_| j |_|S )	af  
        Parameters
        ----------
        new_axis : Index
        indexer : ndarray of int64 or None
        axis : int
        fill_value : object, default None
        allow_dups : bool, default False
        copy : bool, default True
        consolidate: bool, default True
            Whether to consolidate inplace before reindexing.
        only_slice : bool, default False
            Whether to take views, not copies, along columns.
        use_na_proxy : bool, default False
            Whether to use a np.void ndarray for newly introduced columns.

        pandas-indexer with -1's only.
        Nr   #Requested axis not found in managerr   )r   r   r   c                   s*   g | ]"}|j d  dk	r n|jdqS )rX   N)rh   r   )take_ndr   rw   r   r   r9   rL   ry     s   z4BaseBlockManager.reindex_indexer.<locals>.<listcomp>rX   )r@   r   r   r   Z_validate_can_reindexrB   
IndexError_slice_take_blocks_ax0r>   r`   rR   rV   r;   rW   r<   )rK   r   r   rh   r   r   r   r   r   r   r   r   r   new_mgrr9   r  rL   r   z  s<    


z BaseBlockManager.reindex_indexerslice | np.ndarray)slice_or_indexerr   r   rP   c                  sn  |t jk	}t|| jd |d\}}}| jr| jd  |dkrf|dkrHg S ttd|}	 j||	dgS |rt| j	dkr|r|dkr j
}|s|r fddt|D }
|
S ttd|}	 j|d|	|d	gS |dkr| j| }| j| }n(tj| j|d
|d}tj| j|d
|d}g }
| }tj||dD ]>\}}|d
krV|
| j|||d q(| j|   js js|D ]0} jdd}tt||d |_|
| qtn||j }tt|| d }|rt ||}t|tr  j||d}|
| nf|rLt||D ]8\}}t||d }t|}	 j||	d}|
| qn j|d|d}|
| q(|
S )ab  
        Slice/take blocks along axis=0.

        Overloaded for SingleBlock

        Parameters
        ----------
        slice_or_indexer : slice or np.ndarray[int64]
        fill_value : scalar, default lib.no_default
        only_slice : bool, default False
            If True, we always return views on existing arrays, never copies.
            This is used when called from ops.blockwise.operate_blockwise.
        use_na_proxy : bool, default False
            Whether to use a np.void ndarray for newly introduced columns.

        Returns
        -------
        new_blocks : list of Block
        r   )
allow_fillr_   new_mgr_locsrX   Nc                   s,   g | ]$\}} j t||d  t|dqS )rX   r	  )getitem_block_columnsr_   r   )rx   r   mlra   r9   rL   ry     s
    z;BaseBlockManager._slice_take_blocks_ax0.<locals>.<listcomp>)rh   r
  r   r   )r   r  group)rZ   r   r   Fr   )rh   r
  )r
   r   _preprocess_slice_or_indexerr   rq   r>   r   r_   r  rB   r   r   r  rV   rW   algoslibinternalsget_blkno_placementsr   _make_na_block_can_consolidateZ_validate_ndimr   r   r   maxrp   Zmaybe_indices_to_slicer[   zip)rK   r  r   r   r   r  Zsl_typeslobjZsllenrc   r>   rV   rW   r  blknor   mgr_locZnewblktakermax_lenrd   r   r  Zslcr9   r  rL   r    s    
  

	
      


z'BaseBlockManager._slice_take_blocks_ax0r   r,   )rZ   r   rP   c           
      C  s   |rD|d kst t|| jd f}tj|tjd}t||dd}|S |d krRtj}t| j}t||d< t	|\}}tj||d}	|	
| t|	|dS )NrX   ru   ri   rj   r   rY   )r]   rp   r   rz   emptyvoidr.   nanr   r   fillr3   )
rK   rZ   r   r   r   valsrd   block_shaperv   block_valuesr9   r9   rL   r  Q  s    

zBaseBlockManager._make_na_blockrX   )rK   rh   verifyrP   c                 C  sp   t |tr$tj|j|j|jtjdntj|tjd}| j	| }t
|||d}| j| |}| j|||dddS )ac  
        Take items along any axis.

        indexer : np.ndarray or slice
        axis : int, default 1
        verify : bool, default True
            Check that all entries are between 0 and len(self) - 1, inclusive.
            Pass verify=False if this check has been done by the caller.

        Returns
        -------
        BlockManager
        ru   )r$  TF)r   r   rh   r   r   )r[   r_   rz   arangestartstopstepr   Z
asanyarrayr   r%   r@   r|   r   )rK   r   rh   r$  r   rm   r9   r9   rL   r|   k  s    
zBaseBlockManager.take)T)N)NF)T)Fr   )TTTT)FF)F)F)TN)T)NFTTF)NF)rX   T)4r   
__module____qualname____doc__	__slots____annotations__rM   classmethodrR   propertyrV   rW   re   rg   __bool__rk   rn   rq   rt   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r  r  r|   r9   r9   r9   rL   r7   W   s   
2

  @4    	     (     "R 
   c                   @  s  e Zd ZdZdZd]dddddd	Zd
dddZeddd dddZdddddZ	dddddZ
dddddZedddd Zd^d"ddd#d$d%Zddddd&d
d'd(d)Zdd*dd
d+d,d-Zd
dd.d/Zd
dd0d1Zd dd2d3Zd_d4d5dd4d6d7d8Zd`d4d5dd9d6d:d;Zd d d<d=d>Zd d dd?d@dAZdBdCdDd4dEdd4dFdGdHZd ddIdJZdaddKdLdMZdNd!ejfdOddPdQdRdSZdNejfdOdPdTdUdVZdddWdXZd
ddYdZZ d
dd[d\Z!dNS )bBlockManagerz0
    BaseBlockManager that holds 2D blocks.
    ri   TzSequence[Block]zSequence[Index]rC   )r>   r@   rG   c              	   C  s   |r|D ]}| j |j kr2td|j  d| j  dt|tr|jj dkrtjdtt d t	|j| j |_z|j
  W q tk
r   Y qX q|   d S )NzNumber of Block dimensions (z) must equal number of axes ()rX   zIn a future version, the BlockManager constructor will assume that a DatetimeTZBlock with block.ndim==2 has block.values.ndim == 2.
stacklevel)rB   r]   r[   r-   r^   warningswarnDeprecationWarningr   r/   _cacheclearAttributeError_verify_integrity)rK   r>   r@   rG   r   r9   r9   rL   rM     s*     zBlockManager.__init__rl   rS   c                 C  s   | j }tdd | jD }| jD ]6}|j dd  |dd  kr t||j dd  | jq t| j|krtdt| j d| d S )Nc                 s  s   | ]}t |jV  qd S rH   )rp   r   rx   xr9   r9   rL   r     s     z1BlockManager._verify_integrity.<locals>.<genexpr>rX   zINumber of manager items must equal union of block items
# manager items: z, # tot_items: )r   sumr>   construction_errorr@   rp   rt   r]   )rK   Z	mgr_shape	tot_itemsr   r9   r9   rL   r;    s    
zBlockManager._verify_integrityrN   r?   r>   r@   rP   c                 C  s   | ||ddS )Z
        Constructor for BlockManager and SingleBlockManager with same signature.
        FrF   r9   rQ   r9   r9   rL   rR     s    zBlockManager.from_blocksrA   r   )locrP   c           	      C  s   t | jdkr&| jd td|fS tdd | jD }t | }t|trf| }|j|f|d}nt	j
||d}t|}| jD ]*}t|jD ]\}}|||f||< qq|S )z
        Return the array corresponding to `frame.iloc[loc]`.

        Parameters
        ----------
        loc : int

        Returns
        -------
        np.ndarray or ExtensionArray
        rX   r   Nc                 S  s   g | ]
}|j qS r9   ru   rw   r9   r9   rL   ry     s     z(BlockManager.fast_xs.<locals>.<listcomp>ru   )rp   r>   igetr_   r+   r[   r   Zconstruct_array_type_emptyrz   r  r#   r   r   )	rK   rC  rv   r   rO   r   ra   r   rlr9   r9   rL   fast_xs  s    

zBlockManager.fast_xsr\   )r   rP   c                 C  sT   | j | j|  }|| j| }ttdt|}t|||dd}t|| j	d S )z:
        Return the data as a SingleBlockManager.
        r   rX   rZ   rB   )
r>   rV   rD  rW   r   r_   rp   r`   r\   r@   )rK   r   r   r^   rc   rd   r9   r9   rL   rD    s
    zBlockManager.igetc                 C  s$   | j | j|  }|| j| }|S )zY
        Return the data for column i as the values (ndarray or ExtensionArray).
        )r>   rV   rD  rW   )rK   r   r   r^   r9   r9   rL   iget_values  s    zBlockManager.iget_valueszlist[np.ndarray]c                 C  sd   dgt | j }| jD ]H}|j}| }|jdkr@|||d < qt|D ]\}}|| ||< qHq|S )z
        Used in the JSON C code to access column arrays.
        This optimizes compared to using `iget_values` by converting each
        NrX   r   )rp   rt   r>   	_mgr_locsZvalues_for_jsonrB   r   )rK   r   ra   r   r^   r   rC  r9   r9   rL   column_arrays  s    

zBlockManager.column_arraysFzint | slice | np.ndarray)rC  r   r   c                   s  | j dkr| jdkr|   tj}|sljdkr<jntddjdd | jdd krltdt	
|rtt|}| j| }| j| }t|jdkr| j||||dS |g}|r·fdd}nfd	d}| j| }| j|  }	g }
g }g  tj|d
dD ]\}}| j| }|	|j }|rL|rL|||| n^|
|jj|  || t|t|jkr | n"|| tt|| j |jj< qt r2tj| j tj!d}d
| < tj"| j tj#d}|$d t| j t  || < || j% | _%t& fddt'| jD | _|rt(|
}t|}g }|r|)fdd|D  t|t| j | j%|< d| j |< nP|d |dd }|t*||t+|d t| j| j%|< t|| j |< |  jt&|7  _d| _,dS )z
        Set new item in-place. Does not consolidate. Adds new Block if not
        contained in the current set of items
        NrX   ri   rj   z9Shape of new values must be compatible with manager shape)r   r  ra   c                   s    S rH   r9   rY   r   r9   rL   value_getitemY  s    z(BlockManager.iset.<locals>.value_getitemc                   s
    | j  S rH   )r   rY   rL  r9   rL   rM  ^  s    Tr  ru   r   c                 3  s"   | ]\}}|t  kr|V  qd S rH   )set)rx   r   ra   )removed_blknosr9   rL   r     s     z$BlockManager.iset.<locals>.<genexpr>c                 3  s(   | ] }t  tt||d  dV  qdS )rX   r^   rZ   N)r3   r   r_   )rx   r  rL  r9   rL   r     s
   r   rP  F)-r<   rB   rT   r   rv   r6   r/   r   r]   r
   r   r   rA   rV   r>   rp   rJ  _iset_singlerW   r   r  r  r   should_storeset_inplacer   r   r   deleterz   r%  zerosr   bool_r  r   r   r;   tupler   r   extendr3   r   rD   )rK   rC  r   r   Zvalue_is_extension_typer  ra   rM  rV   rW   Zunfit_mgr_locsZunfit_val_locsZblkno_lZval_locsZblk_locs
is_deleted
new_blknosZ
unfit_idxrZunfit_countr   Zunfit_val_itemsr9   )rO  r   rL   iset%  s    















zBlockManager.isetr,   )rC  r   r   r  ra   rP   c           
      C  sr   |r2| |r2| j| }|t||d | dS t||jd}| j}|d| |f ||d d  }	|	| _dS )z
        Fastpath for iset when we are only setting a single position and
        the Block currently in that position is itself single-column.

        In this case we can swap out the entire Block and blklocs and blknos
        are unaffected.
        rX   NrY   )rR  rW   rS  r_   r3   rJ  r>   )
rK   rC  r   r   r  ra   r   rd   Z
old_blocksr   r9   r9   rL   rQ    s    
"zBlockManager._iset_singler   )rC  itemr   rP   c                 C  s   | j ||}|jdkr>|j}t|dkrLtd|jj nt|| jd}tt	||d }t
||d}t| jstjdgtjd| _tjdgtjd| _n| | | | || jd< |  j|f7  _d| _td	d
 | jD dkrtjdtt d dS )z
        Insert item at selected position.

        Parameters
        ----------
        loc : int
        item : hashable
        value : np.ndarray or ExtensionArray
        ri   rX   z-Expected a 1D array, got an array with shape rj   rP  r   ru   Fc                 s  s   | ]}|j  V  qd S rH   r   r   r9   r9   rL   r     s     z&BlockManager.insert.<locals>.<genexpr>d   a   DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`r3  N)rt   insertrB   r6   rp   
ValueErrorr   r/   r   r_   r3   r>   rz   r{   r   r<   r;   _insert_update_mgr_locs!_insert_update_blklocs_and_blknosr@   rD   r>  r5  r6  r   r   )rK   rC  r\  r   r   rc   r   r9   r9   rL   r^    s0    




zBlockManager.insertc                 C  s8   t | j|d D ] \}}| j| }|j||_qdS )z
        When inserting a new Block at location 'loc', we increment
        all of the mgr_locs of blocks above that by one.
        N)_fast_count_smallintsrV   r>   rJ  Zincrement_above)rK   rC  r  countra   r9   r9   rL   r`    s    
z$BlockManager._insert_update_mgr_locsc                 C  s   || j jd kr8t| jd| _t| jt| j| _n|dkrt| jddd dddd | _t| jddd t| jddd | _n*t	| j | j
|t| j\}}|| _|| _dS )zk
        When inserting a new Block at location 'loc', we update our
        _blklocs and _blknos.
        r   Nr   )rW   r   rz   r   r<   r;   rp   r>   r  Zupdate_blklocs_and_blknosrV   )rK   rC  Znew_blklocsrZ  r9   r9   rL   ra    s    $,   z.BlockManager._insert_update_blklocs_and_blknosc                 C  sj   t j| jd t jd}d||< |  d }| j|dd}| j|  }|| jd g}t| t	||ddS )zJ
        Delete selected locations, returning a new BlockManager.
        r   ru   T)r   rX   FrF   )
rz   rU  r   rV  nonzeror  rt   r@   r`   rW  )rK   r   rY  r  r   new_columnsr@   r9   r9   rL   idelete  s    zBlockManager.ideleter6   r   )rK   funcr   rP   c           	      C  s  g }d}| j D ]}|jrh| D ]D}z||}W n& ttfk
rX   |sL d}Y q Y nX t||}q qz||}W n& ttfk
r   |s d}Y qY nX t||}qt|dkrtdg}ntt	|d j
jd }|r| j|d|dS t| || jd |gS )aP  
        Apply grouped reduction function blockwise, returning a new BlockManager.

        Parameters
        ----------
        func : grouped reduction function
        ignore_failures : bool, default False
            Whether to drop blocks where func raises TypeError.

        Returns
        -------
        BlockManager
        FTr   Nr   r   r   )r>   r   r   r   r   rJ   r0   rp   r'   r   r^   r   r   r`   rR   r@   )	rK   rg  r   r   Zdropped_anyra   Zsbr   r   r9   r9   rL   grouped_reduce#  s6    


zBlockManager.grouped_reduceztuple[T, np.ndarray]c           	      C  s   | j dkstg }| jD ]}|||}|| qtdg}|r|rltdd |D }| j|d|d}qg }t	| 
g | jdd |g}n&t| jd }t	| 
|| j|g}||fS )a  
        Apply reduction function blockwise, returning a single-row BlockManager.

        Parameters
        ----------
        func : reduction function
        ignore_failures : bool, default False
            Whether to drop blocks where func raises TypeError.

        Returns
        -------
        BlockManager
        np.ndarray
            Indexer of mgr_locs that are retained.
        ri   Nc                 S  s   g | ]}|j jqS r9   r   rw   r9   r9   rL   ry   s  s     z'BlockManager.reduce.<locals>.<listcomp>Frh  r   )rB   r]   r>   reducerX  r'   rz   r   r   r`   rR   rt   r%  r   )	rK   rg  r   Z
res_blocksra   r   r   r   r  r9   r9   rL   rj  V  s    

 zBlockManager.reduce)r   rP   c                 C  s   t | ||S )zO
        Apply array_op blockwise with another (aligned) BlockManager.
        )r5   )rK   r   Zarray_opr9   r9   rL   r5   }  s    zBlockManager.operate_blockwiserK   r   rP   c                 C  s   t | |tS )
        Used in .equals defined in base class. Only check the column values
        assuming shape and indexes have already been checked.
        )r4   r   )rK   r   r9   r9   rL   _equal_values  s    zBlockManager._equal_valuesr   Zlinear)rh   interpolationr&   )rK   qsrh   rP   c                  sb   | j dksttst dks&tt| j}t|d<  fdd| jD }t| ||S )a  
        Iterate over blocks applying quantile reduction.
        This routine is intended for reduction type operations and
        will do inference on the generated blocks.

        Parameters
        ----------
        axis: reduction axis, default 0
        consolidate: bool, default True. Join together blocks having same
            dtype
        interpolation : type of interpolation, default 'linear'
        qs : list of the quantiles to be computed

        Returns
        -------
        BlockManager
        ri   rX   c                   s   g | ]}|j  d qS ))rh   ro  rn  )quantilerw   rh   rn  ro  r9   rL   ry     s   z)BlockManager.quantile.<locals>.<listcomp>)rB   r]   r   r   r@   r&   r>   r`   )rK   ro  rh   rn  r   r>   r9   rq  rL   rp    s    
zBlockManager.quantilec                 C  s  | | j}|j}|j }|r<|j j|j }|jdd}ntj	|jd t
d}g }g }	t| jdkrld}
n*t|t| j }|t|kstt|}
| jD ]\}|j}||
}|j||||d\}}|| |	| | tdd |D kstq||	 }t|||gdd	}|S )
a'  
        Return a BlockManager with all blocks unstacked.

        Parameters
        ----------
        unstacker : reshape._Unstacker
        fill_value : Any
            fill_value for newly introduced missing values.

        Returns
        -------
        unstacked : BlockManager
        r   rh   rX   ru   )new_placementneeds_maskingc                 s  s   | ]}t |jV  qd S rH   )rp   rJ  )rx   rd   r9   r9   rL   r     s     z'BlockManager.unstack.<locals>.<genexpr>FrF   )Zget_new_columnsrt   	new_indexZmask_allr   reshapeZ
full_shaper   rz   rU  rC   rp   rA   r]   r>   r   Ztile_for_unstackZ_unstackrX  r>  r1  )rK   Z	unstackerr   re  ru  r  Z
new_mask2Drt  r   Zcolumns_maskZfactorZfacra   r   rs  r>   r   r   r9   r9   rL   unstack  s:    




 zBlockManager.unstackr   c                   s@   i }j D ]}|t|jg | q
 fdd| D S )z
        Return a dict of str(dtype) -> BlockManager

        Parameters
        ----------
        copy : bool, default True

        Returns
        -------
        values : a dict of dtype -> BlockManager
        c                   s    i | ]\}}|j | d qS )r   )r   )rx   rv   r>   r   rK   r9   rL   r     s      z(BlockManager.to_dict.<locals>.<dictcomp>)r>   
setdefaultr   rv   r   rt   )rK   r   Zbdr   r9   rx  rL   to_dict  s    
zBlockManager.to_dictNznp.dtype | None
np.ndarray)rv   r   rP   c                 C  s   t | jdkr&tj| jtd}| S |p2|tjk	}| j	r| jd }|j
rd|jj||d|j}qt| }|r|j|dd}n| j||d}d}|r| }|tjk	r||t|< | S )a-  
        Convert the blockmanager data into an numpy array.

        Parameters
        ----------
        dtype : np.dtype or None, default None
            Data type of the return array.
        copy : bool, default False
            If True then guarantee that a copy is returned. A value of
            False does not guarantee that the underlying data is not
            copied.
        na_value : object, default lib.no_default
            Value to be used as the missing value sentinel.

        Returns
        -------
        arr : ndarray
        r   ru   rv   na_valueFr   )rp   r>   rz   r  r   floatZ	transposer
   r   rq   r   r^   to_numpyrv  Zasarray
get_valuesr   _interleaver   r    )rK   rv   r   r}  rb   ra   r9   r9   rL   r     s.    

zBlockManager.as_array)rv   rP   c                 C  s*  |st dd | jD }t|tr6|j}ttj|}n*t|trLtd}nt	|t
r`td}tj| j|d}t| jd }|tdkr|tjkr| jD ](}|j}||}|||j< d||j< q|S | jD ]@}|j}|jr|jj||d}n
||}|||j< d||j< q| s&td|S )	zt
        Return ndarray from blocks with specified item order
        Items must be contained in the blocks
        c                 S  s   g | ]
}|j qS r9   ru   rw   r9   r9   rL   ry   O  s     z,BlockManager._interleave.<locals>.<listcomp>objectru   r   rX   r|  z'Some items were not contained in blocks)r+   r>   r[   r"   subtyper   rz   rv   r   r   r   r  r   rU  r
   r   r   r  r   r   r^   r  r   r]   )rK   rv   r}  r   Zitemmaskra   rF  rb   r9   r9   rL   r  A  sB    	










zBlockManager._interleavec                 C  s   | j s|   | jS )zH
        Return True if more than one block with the same dtype
        )rD   _consolidate_checkrE   rU   r9   r9   rL   r     s    zBlockManager.is_consolidatedc                 C  sN   t | jdkrd| _d| _d S dd | jD }t |t t|k| _d| _d S )NrX   Tc                 S  s   g | ]}|j r|jqS r9   )r  rv   rw   r9   r9   rL   ry     s      z3BlockManager._consolidate_check.<locals>.<listcomp>)rp   r>   rE   rD   rN  r}   r9   r9   rL   r    s    zBlockManager._consolidate_checkc                 C  s0   |   s,tt| j| _d| _d| _|   d S rf   )r   rW  _consolidater>   rE   rD   rT   rU   r9   r9   rL   r     s
    z!BlockManager._consolidate_inplace)T)F)F)F)T)"r   r)  r*  r+  rB   rM   r;  r.  rR   rG  rD  rI  r/  rK  r[  rQ  r^  r`  ra  rf  ri  rj  r5   rm  rp  rw  rz  r
   r   r   r  r   r  r   r9   r9   r9   rL   r1    sP   	 )	!  0
4 '*=>@
r1  c                   @  s  e Zd ZdZdZdZdZdZdZde	j
fdddd	d
dZeddd dddZeddd dddZdddddZdd Zdd Zdd Zeddd d!Zed"d# Zed$d% Zd dd&d'ZdMd)d*d d+d,d-Zeddd.d/Zed0dd1d2Zd3dd4d5Zd6d7 Zd8d9 Zd:d; ZdNdd<d=d>Z eddd?d@Z!d ddAdBZ"dCdD Z#ddEdFdGZ$dHdHddIdJdKZ%dLS )Or\   zmanage a single block withrX   Tr9   Fr,   r'   rC   )r   rh   rG   c                 C  s0   |t jk	rtjdtt d |g| _|f| _d S )NzMThe `fastpath` keyword is deprecated and will be removed in a future version.r3  )r
   r   r5  r6  FutureWarningr   r@   r>   )rK   r   rh   rG   Zfastpathr9   r9   rL   rM     s    
zSingleBlockManager.__init__rN   r?   rA  c                 C  s6   t |dkstt |dks t| |d |d ddS )rB  rX   r   FrF   )rp   r]   rQ   r9   r9   rL   rR     s    zSingleBlockManager.from_blocksr   )r{   r   rP   c                 C  s"   t |tdt|dd}| ||S )zN
        Constructor for if we have an array that is not yet a Block.
        r   rX   rH  )r2   r_   rp   )rO   r{   r   r   r9   r9   rL   
from_array  s    zSingleBlockManager.from_arrayr1  )columnsrP   c                 C  sP   | j d }t|jdd}td}t|||dd}|| jd g}t|g|ddS )z5
        Manager analogue of Series.to_frame
        r   ri   rj   rH  F)r@   rG   )r>   r/   r^   r   r`   r@   r1  )rK   r  ra   rb   rc   Znew_blkr@   r9   r9   rL   	to_2d_mgr  s    
zSingleBlockManager.to_2d_mgrc                   sT   dd  j D } fdd j D }t j}d|dd  j D di}||||fS )Nc                 S  s   g | ]
}|j qS r9   r   r   r9   r9   rL   ry     s     z3SingleBlockManager.__getstate__.<locals>.<listcomp>c                   s   g | ]} j |jj qS r9   )rt   r   r   r   rU   r9   rL   ry     s     0.14.1c                 S  s   g | ]}|j |jjd qS ))r^   r   )r^   r   r   r   r9   r9   rL   ry     s   )r@   r>   )r>   r   r@   )rK   r#  Zblock_itemsZ
axes_arrayZextra_stater9   rU   rL   __getstate__  s    
zSingleBlockManager.__getstate__c                   s   dddddt |tr|t|dkr|d|d kr||d d }d	d
 |d D | _t| j t fdd|d D | _ntd|   d S )NrA   r,   )rB   rP   c                 S  s   t | dd} t| ||dS )NTr   rH  )r$   r2   )r^   r   rB   r9   r9   rL   unpickle_block  s    z7SingleBlockManager.__setstate__.<locals>.unpickle_block   r     c                 S  s   g | ]}t |qS r9   )r(   r   r9   r9   rL   ry     s     z3SingleBlockManager.__setstate__.<locals>.<listcomp>r@   c                 3  s$   | ]}|d  |d  dV  qdS )r^   r   rj   Nr9   r   rB   r  r9   rL   r     s   z2SingleBlockManager.__setstate__.<locals>.<genexpr>r>   z*pre-0.14.1 pickles are no longer supported)r[   rW  rp   r@   r>   rJ   _post_setstate)rK   stater9   r  rL   __setstate__  s    "
zSingleBlockManager.__setstate__c                 C  s   d S rH   r9   rU   r9   r9   rL   r    s    z!SingleBlockManager._post_setstaterS   c                 C  s
   | j d S rr   r   rU   r9   r9   rL   _block   s    zSingleBlockManager._blockc                 C  s   dS zcompat with BlockManagerNr9   rU   r9   r9   rL   r;     s    zSingleBlockManager._blknosc                 C  s   dS r  r9   rU   r9   r9   rL   r<   	  s    zSingleBlockManager._blklocsc                 C  s^   | j }||}|jdkr"tdttdt|}t|||dd}| j| }t| ||S )NrX   z(dimension-expanding indexing not allowedr   rH  )	r  _slicerB   r_  r   r_   rp   r`   r   )rK   r   ra   r{   rc   r   Znew_idxr9   r9   rL   getitem_mgr  s    


zSingleBlockManager.getitem_mgrr   r_   rA   )r  rh   rP   c                 C  s`   || j krtd| j}||}ttdt|}t|||dd}| j	|}t| ||S )Nr   r   rX   rH  )
rB   r  r  r  r   r_   rp   r`   r   Z_getitem_slice)rK   r  rh   ra   r{   rc   r   ru  r9   r9   rL   	get_slice  s    

zSingleBlockManager.get_slicec                 C  s
   | j d S rr   rs   rU   r9   r9   rL   r   )  s    zSingleBlockManager.indexr   c                 C  s   | j jS rH   )r  rv   rU   r9   r9   rL   rv   -  s    zSingleBlockManager.dtyper{  c                 C  s   t | jjgS rH   )rz   r{   r  rv   rU   r9   r9   rL   r~   1  s    zSingleBlockManager.get_dtypesc                 C  s
   | j  S )z$The array that Series.values returns)r  external_valuesrU   r9   r9   rL   r  4  s    z"SingleBlockManager.external_valuesc                 C  s   | j jS )z%The array that Series._values returns)r  r^   rU   r9   r9   rL   internal_values8  s    z"SingleBlockManager.internal_valuesc                 C  s   | j jS )z#The array that Series.array returns)r  array_valuesrU   r9   r9   rL   r  <  s    zSingleBlockManager.array_valuesr   c                 C  s    | j jr|r|  S | S |  S rH   )r  r   r   re   )rK   r   r9   r9   rL   r   @  s
    z#SingleBlockManager.get_numeric_datac                 C  s   | j jS rH   )r  _can_hold_narU   r9   r9   rL   r  G  s    zSingleBlockManager._can_hold_nac                 C  s&   | j | | jd || jd< | S )zy
        Delete single location from SingleBlockManager.

        Ensures that self.blocks doesn't become empty.
        r   )r  rT  r@   )rK   r   r9   r9   rL   rf  K  s    zSingleBlockManager.ideletec                 C  s   t ddS )zY
        fast path for getting a cross-section
        return a view of the data
        zUse series._values[loc] insteadNrI   )rK   rC  r9   r9   rL   rG  U  s    zSingleBlockManager.fast_xsr   c                 C  s(   || j d _ttt|| j d _dS )z
        Set the values of the single block in place.

        Use at your own risk! This does not check if the passed values are
        valid for the current Block/SingleBlockManager (length, dtype, etc).
        r   N)r>   r^   r   r_   rp   rJ  )rK   r^   r9   r9   rL   
set_values\  s    zSingleBlockManager.set_valuesr6   rk  c                 C  s0   |j dkrdS | jd j}|jd j}t||S )rl  rX   Fr   )rB   r>   r^   r   )rK   r   leftrightr9   r9   rL   rm  f  s
    
z SingleBlockManager._equal_valuesN)r   )F)&r   r)  r*  r+  rB   rE   rD   r,  rq   r
   r   rM   r.  rR   r  r  r  r  r  r   r  r/  r;   r<   r  r  r   rv   r~   r  r  r  r   r  rf  rG  r  rm  r9   r9   r9   rL   r\     sP   



r\   TrN   r?   rC   )r>   r@   r   rG   rP   c              
   C  s   zt | ||d}W n^ tk
rp } z@dd | D }tdd |D }t||d jdd  ||W 5 d }~X Y nX |r~|  |S )NrF   c                 S  s   g | ]
}|j qS r9   r   rw   r9   r9   rL   ry     s     z4create_block_manager_from_blocks.<locals>.<listcomp>c                 s  s   | ]}|j d  V  qdS )r   N)r   )rx   rb   r9   r9   rL   r     s     z3create_block_manager_from_blocks.<locals>.<genexpr>r   rX   )r1  r_  r>  r?  r   r   )r>   r@   r   rG   mgrerrr   r@  r9   r9   rL    create_block_manager_from_blocksw  s    .r  r   )r   r@   r   rP   c              
   C  sh   zt | |}t||dd}W n: tk
rV } ztt| | d j||W 5 d }~X Y nX |rd|  |S )NFrF   r   )_form_blocksr1  r_  r?  rp   r   r   )r   r@   r   r>   r  er9   r9   rL   'create_block_manager_from_column_arrays  s    
*r  rA   r   zValueError | None)r@  r"  r@   r  c                 C  s   t tt| gt| }t|dkr2|ddd }t dd |D }t|dkr^|ddd }||krr|dk	rr|S |d dkrtdS td| d	| S )
z.raise a helpful message about our constructionri   Nr   c                 s  s   | ]}t |V  qd S rH   )rp   r   r9   r9   rL   r     s     z%construction_error.<locals>.<genexpr>r   z)Empty data passed with indices specified.zShape of passed values is z, indices imply )rW  maprA   r   rp   r_  )r@  r"  r@   r  ZpassedZimpliedr9   r9   rL   r?    s    r?  ztuple[int, ArrayLike]ztuple[int, bool, DtypeObj])tuprP   c                 C  s2   | d j }t|rt|}nd}|t|tj |fS )NrX   r   )rv   r   idr[   rz   )r  rv   sepr9   r9   rL   _grouping_func  s
    

r  )r   r   rP   c                   s   t t| }|st|}|S t|t}g }|D ]\\}}}}t| t|tj	r|j
dk}t|jttfrvt	t}tt ||\}	}
|rt|	}	 |	t|
dd}|| q0t|r؇ fdd|D }|| q0 fdd|D }|| q0|S )N)mMri   rH  c                   s&   g | ]} |d  t |d ddqS )rX   r   ri   rH  r   r<  Z
block_typer9   rL   ry     s   z _form_blocks.<locals>.<listcomp>c                   s,   g | ]$} t |d  dt|d ddqS )rX   ri   r   rH  )r/   r   r<  r  r9   rL   ry     s    
 )r   r    _tuples_to_blocks_no_consolidate	itertoolsgroupbyr  r1   r[   rz   rv   kind
issubclassr`   r   bytesr  _stack_arraysr#   r   r   r   rX  )r   r   tuplesr   grouper_rv   Z	tup_blockZ	is_dtliker^   rZ   ra   Zdtype_blocksr9   r  rL   r    s6    



r  rS   c                 C  s   dd | D S )Nc                 S  s,   g | ]$}t t|d  ddt|d dqS )rX   ri   rj   r   rY   )r3   r/   r   r<  r9   r9   rL   ry     s   z4_tuples_to_blocks_no_consolidate.<locals>.<listcomp>r9   )r  r9   r9   rL   r    s    r  znp.dtyperu   c           	      C  sT   t |  \}}|d }t|f|j }tj||d}t|D ]\}}|||< q:||fS )Nr   ru   )r  rp   r   rz   r  r   )	r  rv   rZ   r   firstr   Zstackedr   rb   r9   r9   rL   r    s    
r  r=   )r>   rP   c                 C  sR   dd }t t| |d|}g }|D ](\\}}}tt|||d}t||}q$|S )zJ
    Merge blocks having same dtype, exclude non-consolidating blocks
    c                 S  s   | j S rH   )Z_consolidate_key)r=  r9   r9   rL   <lambda>      z_consolidate.<locals>.<lambda>)key)rv   can_consolidate)r  r  sorted_merge_blocksr   r0   )r>   Zgkeyr  r   r  rv   Zgroup_blocksZmerged_blocksr9   r9   rL   r    s      r  r   )r>   rv   r  rP   c           	      C  s   t | dkr| S |rtdd | D }t| d jtjrPtdd | D }n.dd | D }ttt |}|d j	|dd}t
|}|| }|| }t|}t||dgS | S )	NrX   c                 S  s   g | ]}|j jqS r9   r   r   r9   r9   rL   ry   6  s     z!_merge_blocks.<locals>.<listcomp>r   c                 S  s   g | ]
}|j qS r9   r   r   r9   r9   rL   ry   ?  s     c                 S  s   g | ]
}|j qS r9   r   rw   r9   r9   rL   ry   A  s     rr  rY   )rp   rz   r   r[   rv   Zvstackr   r   r!   Z_concat_same_typeargsortr   r3   )	r>   rv   r  r
  Z
new_valuesZbvalsZbvals2r  rc   r9   r9   rL   r  +  s    
r  r:   )rb   c                 C  s$   t | }| d }t||| S )z:Faster version of set(arr) for sequences of small numbers.r   )rz   Zbincountrd  r  )rb   countsZnzr9   r9   rL   rb  P  s    
rb  r  )r  lengthr  c                 C  sv   t | trd| t| |fS t | tjr4| jjdkrNt| dd }t	t
| |t| }|sdt||}d|t|fS d S )Nr_   r   rv   Zfancy)r[   r_   r  Z	slice_lenrz   Zndarrayrv   r  r   r   r`   r   r%   rp   )r  r  r  rv   r   r9   r9   rL   r  Y  s    




r  )TT)T)N)a
__future__r   r  typingr   r   r   r   r   r   r5  Znumpyrz   Zpandas._libsr	   r  r
   Zpandas._libs.internalsr   Zpandas._typingr   r   r   r   r   Zpandas.errorsr   Zpandas.util._decoratorsr   Zpandas.util._exceptionsr   Zpandas.util._validatorsr   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr   r   r   r   r   Zpandas.core.dtypes.dtypesr   Zpandas.core.dtypes.genericr   r   Zpandas.core.dtypes.missingr   r    Zpandas.core.algorithmscoreZ
algorithmsr  Zpandas.core.arrays._mixinsr!   Zpandas.core.arrays.sparser"   Zpandas.core.constructionr#   r$   Zpandas.core.indexersr%   Zpandas.core.indexes.apir&   r'   r(   Zpandas.core.internals.baser)   r*   r+   Zpandas.core.internals.blocksr,   r-   r.   r/   r0   r1   r2   r3   Zpandas.core.internals.opsr4   r5   r6   r7   r1  r\   r  r  r?  r  r  r  r  r  r  rb  r  r9   r9   r9   rL   <module>   sr    (
      <       `    ,%	