U
    MZfV                    @  s  d dl mZ d dlmZ d dlZd dlmZmZmZm	Z	m
Z
mZmZ d dlZd dlmZ d dlmZmZmZ d dlmZ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"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l0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZC d dlDmEZEmFZFmGZGmHZH d dlImJZJmKZKmLZL d dlMmNZN d dlOmP  mQZR d dlSmTZTmUZUmVZVmWZWmXZX d dlYmZZZ d dl[m\Z\m]Z]m^Z^ d dl_m`Z` d dlambZbmcZcmdZdmeZemfZfmgZgmhZh d dlimjZj d dlkmlZl d dlmmP  mnZo d dlpmqZq d dlrmsZsmtZt d dlumvZv ervd dlwmxZx d d lymzZz e{d!Z|d"d"d#d$d%Z}G d&d' d'elZ~G d(d) d)e~ZG d*d+ d+ej~eZG d,d- d-eje~ZG d.d/ d/eZG d0d1 d1ejeZd2d3d4d5d6ZG d7d8 d8eZG d9d: d:eZG d;d< d<eZd=d=d>d?d@ZdAdBdCdDZdjd=dEdFdGdHdIZddJdKdFd'dLdMdNZdEdKd3dOdPdQZdRdSdKdTdUdVdWZdkdXdYdZd[Zdld=dKd=d]d^d_Zd`dddadbd=dcdcdddedfdgZd=d=d>dhdiZdS )m    )annotations)wrapsN)TYPE_CHECKINGAnyCallableIterableSequencecastfinal)using_copy_on_write)	internalslibwriters)BlockPlacementBlockValuesRefs)NA)IncompatibleFrequency)		ArrayLikeAxisIntDtypeObjFFillnaOptionsIgnoreRaiseQuantileInterpolationShapenptAbstractMethodError)cache_readonly)validate_bool_kwarg)astype_array_safeastype_is_view)LossySetitemErrorcan_hold_elementfind_result_typemaybe_downcast_to_dtypenp_can_hold_element)ensure_platform_intis_1d_only_ea_dtypeis_1d_only_ea_objis_dtype_equalis_interval_dtypeis_list_like	is_sparseis_string_dtype)DatetimeTZDtypeExtensionDtypePandasDtypePeriodDtype)ABCDataFrameABCIndexABCPandasArray	ABCSeries)is_valid_na_for_dtypeisnana_value_for_dtype)missing)extract_bool_arrayputmask_inplaceputmask_without_repeatsetitem_datetimelike_compatvalidate_putmask)quantile_compat)compare_or_regex_searchreplace_regexshould_use_regex)shift)CategoricalDatetimeArrayExtensionArrayIntervalArrayPandasArrayPeriodArrayTimedeltaArray)SparseDtype)PandasObject)expressions)ensure_wrapped_if_datetimelikeextract_array)check_setitem_lengths)Index)NDArrayBackedExtensionArrayobjectr   )methreturnc                   s$   t  dd fdd}tt|S )zp
    If we have a multi-column block, split and operate block-wise.  Otherwise
    use the original method.
    list[Block]rV   c                   s>   | j dks| jd dkr( | f||S | j f||S d S )N   r   )ndimshapesplit_and_operate)selfargskwargsrU    @/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/internals/blocks.pynewfunc   s    zmaybe_split.<locals>.newfunc)r   r	   r   )rU   rc   ra   r`   rb   maybe_split   s    rd   c                   @  s  e Zd ZU dZded< ded< ded< ded	< d
ZdZdZdZdZ	dZ
eedd ZeeddddZeeddddZedd Zeedd Zedd ZeddddZejdddd dZedd"d d#d$d%Zedd&d"d d'd(d)Zed*dd+d,Zeddd-d.Zed/d d0d1d2Zed3dd d4d5d6Zed7dd8d9d:Zed;dd<d=d>Zed?dd@dAZed?ddBdCZed;d?dDdEdFZ ed?ddGdHZ!ed?ddIdJZ"ed ddKdLZ#edd?dd?dMdNdOZ$ee%ddd?dPdQdRZ&dddSddd?dTdUdVZ'edWddXdYZ(eddWdd[dd d\d]d^Z)edd*d d`dadbZ*eddd dcdddeZ+edddfdd?dgdhdiZ,edddd?djdkdlZ-eddmdndddd?dodpdqZ.eddrdddd?dsdtduZ/dvdvdwdxdyZ0dzd{ Z1ed|dd}d~Z2ddvdddZ3dd;d0ddZ4dd;dddddZ5d!e6j7fddd&d dddZ8ddrdddZ9ddd dPddZ:ddd?dPddZ;dddd?dddZ<ddddd?dddZ=ddd!dd!dd!d!d!dd
dddddd*ddddd?dddZ>dddd?dddZ?dddd7d?dddZ@eddddd dddZAdddd dddZBd?dddZCeddddZDeddddZEdddvdddÄZFdvdddńZGd!S )Blockz
    Canonical n-dimensional unit of homogeneous dtype contained in a pandas
    data structure

    Index-ignorant; let the container take care of that
    np.ndarray | ExtensionArrayvaluesintrZ   r   refsr   __init__ra   FTc                 C  s   | j | jjfS N)_can_consolidatedtypenamer]   ra   ra   rb   _consolidate_key   s    zBlock._consolidate_keyboolrX   c                 C  s"   | j }t|tj r|jdkS |jS )z7
        Can we store NA values in this Block?
        )biu)rm   
isinstancenpkind_can_hold_nar]   rm   ra   ra   rb   rx      s    
zBlock._can_hold_nac                 C  s   | j jttkS )z^
        We can be bool if a) we are bool dtype or b) object dtype with bool objects.
        )rg   rm   rv   rq   ro   ra   ra   rb   is_bool   s    zBlock.is_boolc                 C  s
   t | jS rk   )external_valuesrg   ro   ra   ra   rb   r{      s    zBlock.external_valuesc                 C  s   t | jddS )NF)compat)r9   rm   ro   ra   ra   rb   
fill_value   s    zBlock.fill_valuec                 C  s    | j tkrt|| j r| j}|S rk   )rm   
_dtype_objr7   r}   r]   valuera   ra   rb   _standardize_fill_value   s    zBlock._standardize_fill_valuer   c                 C  s   | j S rk   	_mgr_locsro   ra   ra   rb   mgr_locs   s    zBlock.mgr_locsNone)new_mgr_locsrV   c                 C  s
   || _ d S rk   r   )r]   r   ra   ra   rb   r      s    NBlockValuesRefs | None)ri   rV   c                 C  s4   |dkr| j }| jr"t|| jd}t||| j|dS )zm
        Create a new block, with type inference propagate any values that are
        not specified
        NrZ   	placementrZ   ri   )r   is_extensionensure_block_shaperZ   	new_blockr]   rg   r   ri   ra   ra   rb   
make_block   s
    zBlock.make_blockzBlockPlacement | None)r   ri   rV   c                 C  s$   |dkr| j }t| ||| j|dS )z2Wrap given values in a block of same type as self.Nr   )r   typerZ   r   ra   ra   rb   make_block_same_class   s    
zBlock.make_block_same_classstrc                 C  sj   t | j}| jdkr0| dt|  d| j }n6ddd | jD }| d| jj d| d| j }|S )	NrY   z: z dtype: z x c                 S  s   g | ]}t |qS ra   )r   .0sra   ra   rb   
<listcomp>   s     z"Block.__repr__.<locals>.<listcomp>z, z	, dtype: )	r   __name__rZ   lenrm   joinr[   r   indexer)r]   rn   resultr[   ra   ra   rb   __repr__   s    

 zBlock.__repr__c                 C  s
   t | jS rk   )r   rg   ro   ra   ra   rb   __len__  s    zBlock.__len__zslice | npt.NDArray[np.intp]slicerrV   c                 C  s>   | j | }| |}t|tr$| jnd}t| ||| j|dS )
        Perform __getitem__-like, return result as block.

        Only supports slices that preserve dimensionality.
        Nri   )r   _sliceru   sliceri   r   rZ   )r]   r   r   
new_valuesri   ra   ra   rb   getitem_block  s    

zBlock.getitem_blockr   )r   r   rV   c                 C  s8   |  |}|j| jjkr tdt| ||| j| jdS )r   z Only same dim slicing is allowedr   )r   rZ   rg   
ValueErrorr   ri   )r]   r   r   r   ra   ra   rb   getitem_block_columns  s    	
zBlock.getitem_block_columnsr   )elementrV   c                 C  s   t |dd}t| j|S )z#require the same dtype as ourselvesTZextract_numpy)rP   r#   rg   )r]   r   ra   ra   rb   _can_hold_element'  s    zBlock._can_hold_elementr   )r   rV   c                 C  s*   z|j | j kW S  tk
r$   Y dS X dS )z
        Should we set self.values[indexer] = value inplace or do we need to cast?

        Parameters
        ----------
        value : np.ndarray or ExtensionArray

        Returns
        -------
        bool
        FN)rm   	TypeErrorr   ra   ra   rb   should_store-  s    zBlock.should_storerW   c                 K  s   || j f|}| |S )z[
        apply the function to my values; return a block if we are not
        one
        )rg   _split_op_result)r]   funcr_   r   ra   ra   rb   applyC  s    zBlock.applyc                 C  sP   | j dkst|| j}| jj dkr4t|gg}n|dd}| |}|gS )N   rY   )rZ   AssertionErrorrg   rv   arrayreshaper   )r]   r   r   
res_valuesnbra   ra   rb   reduceM  s    

zBlock.reduce)r   rV   c                 C  s|   |j dkrlt|jtrlg }t| jD ]B\}}t|sF|||d  }n|| }| j||d}|| q$|S | |}|gS )NrY   )rg   r   )	rZ   ru   rm   r0   	enumerater   r)   r   append)r]   r   nbsrs   locvalsblockr   ra   ra   rb   r   ^  s    
zBlock._split_op_resultc                 C  sf   | j dkstg }t| jD ]D\}}| jt||d  }t|}t| ||d| jd}|	| q|S )zD
        Split a block into a list of single-column blocks.
        r   rY   r   )
rZ   r   r   r   rg   r   r   r   ri   r   )r]   
new_blocksrs   Zref_locr   bpr   ra   ra   rb   _splits  s    zBlock._splitc                 O  sL   | j dkr| jd dkstg }|  D ]}||f||}|| q(|S )z
        Split the block and apply func column-by-column.

        Parameters
        ----------
        func : Block method
        *args
        **kwargs

        Returns
        -------
        List[Block]
        r   r   rY   )rZ   r[   r   r   extend)r]   r   r^   r_   
res_blocksr   rbsra   ra   rb   r\     s    zBlock.split_and_operatec                 C  s   t | j|}| j|ddS )z
        coerce the current block to a dtype compat for other
        we will return a block, possibly object, and not raise

        we can also safely try to coerce to the same dtype
        and will receive the same block
        Fcopy)r$   rg   astype)r]   otherZ	new_dtypera   ra   rb   coerce_to_target_dtype  s    	zBlock.coerce_to_target_dtype)blocks	using_cowrV   c                   sP    dkr|S | j tkr,tfdd|D S  d kr8|S t fdd|D S )NFc                   s   g | ]}|j    d qS ))r   r   convertr   blkr   ra   rb   r     s     z)Block._maybe_downcast.<locals>.<listcomp>c                   s   g | ]}|  qS ra   )_downcast_2dr   rr   downcastr   ra   rb   r     s     )rm   r~   extend_blocks)r]   r   r   r   ra   r   rb   _maybe_downcast  s    
zBlock._maybe_downcastr   rV   c                 C  s6   t | j|d}|r"|| jkr"| jnd}| j||dgS )zs
        downcast specialized to 2D case post-validation.

        Refactored to allow use of maybe_split.
        rm   Nr   )r%   rg   ri   r   )r]   rm   r   r   ri   ra   ra   rb   r     s    zBlock._downcast_2dr   r   r   r   rV   c                C  s*   |s|r| j ddgS |r$|   gS | gS )z
        attempt to coerce any object types to better types return a copy
        of the block (if copy = True) by definition we are not an ObjectBlock
        here!
        Fdeepr   )r]   r   r   ra   ra   rb   r     s    zBlock.convertr   c                 C  s   | j jS rk   )rg   rm   ro   ra   ra   rb   rm     s    zBlock.dtyperaiser   )rm   r   errorsr   rV   c           	      C  s   | j }t||||d}t|}d}|r:t|j|jr:| j}| j||d}|j| jkrtd| d| jj	 d| j d|jj	 d|j d|S )	a  
        Coerce to the new dtype.

        Parameters
        ----------
        dtype : np.dtype or ExtensionDtype
        copy : bool, default False
            copy if indicated
        errors : str, {'raise', 'ignore'}, default 'raise'
            - ``raise`` : allow exceptions to be raised
            - ``ignore`` : suppress exceptions. On error return original object
        using_cow: bool, default False
            Signaling if copy on write copy logic is used.

        Returns
        -------
        Block
        )r   r   Nr   zcannot set astype for copy = [z] for dtype (z [z]) to different shape (z]))
rg   r    maybe_coerce_valuesr!   rm   ri   r   r[   r   rn   )	r]   rm   r   r   r   rg   r   ri   Znewbra   ra   rb   r     s    .zBlock.astypenan)na_reprV   c                 K  s"   t | jf||d|}| |S )"convert to our native types format)r   quoting)to_native_typesrg   r   )r]   r   r   r_   r   ra   ra   rb   r     s    zBlock.to_native_types)r   rV   c                 C  s6   | j }|r| }d}n| j}t| || j| j|dS )zcopy constructorNr   )rg   r   ri   r   r   rZ   )r]   r   rg   ri   ra   ra   rb   r     s    z
Block.copyznpt.NDArray[np.bool_] | None)inplacemaskr   rV   c                 C  s  | j }t|trp|r,| j s"|s,|  }n"|r>| jdd}n|rF| n|  }tt|j }|j||dd |gS | |s|r| jddgS |r| gS |  gS |dkrt	
||}| s|r| jddgS |r| gS |  gS n,| |rh|r|r| j| j d}n|  }n|r&| n|  }t|j || | jrN|dks^|jd|d}n|g}|S | jdks| jd dkr|dks|tkr| tt}n
| |}|j||d|d	S g }t|  D ]6\}	}
|t| j|
||d||	|	d  |d
 q|S dS )z
        replace the to_replace value with value, possible to create new
        blocks here this is just a call to putmask.
        Fr   T
to_replacer   r   Nr   rY   r   )r   r   r   r   r   r   r   r   r   )rg   ru   rE   ri   has_referencer   r	   _replacer   r:   mask_missinganyr<   	is_objectr   rZ   r[   r   r   rv   rm   rT   r   replacer   r   r   r   )r]   r   r   r   r   r   rg   r   r   rs   r   ra   ra   rb   r   %  sl    




	
zBlock.replace)r   r   rV   c           
      C  s   |  |s0|r| jddgS |r&| gS |  gS t|}|rj|rZ| j sZ| j}| j}qd}| j }nd}|rx| jn| j }t|||| | j||d}	|	j	d|dS )aA  
        Replace elements by the given value.

        Parameters
        ----------
        to_replace : object or pattern
            Scalar to replace or regular expression to match.
        value : object
            Replacement object.
        inplace : bool, default False
            Perform inplace modification.
        mask : array-like of bool, optional
            True indicate corresponding element is ignored.
        using_cow: bool, default False
            Specifying if copy on write is enabled.

        Returns
        -------
        List[Block]
        Fr   Nr   r   )
r   r   recompileri   r   rg   rB   r   r   )
r]   r   r   r   r   r   rxri   r   r   ra   ra   rb   _replace_regex  s     

zBlock._replace_regexzIterable[Any]zSequence[Any])src_list	dest_listr   regexr   rV   c              
     s  j ttr^r,|r,jj d}n|r4n }tt|j j||dd |gS fddt||D }t	|srjddgS |rgS  gS t	|d }t
jrt   fdd	|D }	nfd
d	|D }	|rt|	}	r|rg}
n|rn g}
tt||	D ]\}\\}}}||k}g }t|
D ]\}}t	|
dkrp|}n$|}t|trt|||d  }|j||||d}|r|jrtdd	 |D stfdd|D }|| qT|}
q0|
S )z:
        See BlockManager.replace_list docstring.
        r   Tr   c                   s"   g | ]\}}  |r||fqS ra   )r   )r   xyro   ra   rb   r     s    
 z&Block.replace_list.<locals>.<listcomp>FrY   c              
   3  s,   | ]$}t ttt|d   dV  qdS )r   )r   r   N)r;   r	   r   rA   r   )na_maskr   rg   ra   rb   	<genexpr>  s   	   z%Block.replace_list.<locals>.<genexpr>c                 3  s   | ]}t  |d  V  qdS )r   N)r:   r   r   rg   ra   rb   r     s     )r   r   r   r   r   r   c                 s  s   | ]}|d kV  qd S rk   ra   r   r   ra   ra   rb   r     s     c                   s    g | ]}|j d o   dqS )Tr   r   r   r   ra   rb   r     s   )rg   ru   rE   r   ri   r   r	   r   zipr   r.   rm   r8   listr   rq   r   _replace_coercer   allr   r   )r]   r   r   r   r   r   r   pairsZsrc_lenmasksrbrs   srcdestr   r   Znew_rbZblk_nummZmibr   ra   )r   r   r]   r   rg   rb   replace_list  sj    



	"
zBlock.replace_listnpt.NDArray[np.bool_])r   r   r   r   rV   c           	      C  s   t ||r| j||||dS |dkr| r| j }| jttd|d}|| ksX|rf|sf|	 }n|r|r|j r|	 }t
|j|| |gS |r| j	ddgS |r| gS | 	 gS | j|||||dS dS )ai  
        Replace value corresponding to the given boolean array with another
        value.

        Parameters
        ----------
        to_replace : object or pattern
            Scalar to replace or regular expression to match.
        value : object
            Replacement object.
        mask : np.ndarray[bool]
            True indicate corresponding element is ignored.
        inplace : bool, default True
            Perform inplace modification.
        regex : bool, default False
            If true, perform regular expression substitution.

        Returns
        -------
        List[Block]
        )r   r   NFr   r   r   )rC   r   r   ri   r   r   rv   rm   rT   r   r<   rg   r   )	r]   r   r   r   r   r   r   Zhas_refr   ra   ra   rb   r   (  s6    


zBlock._replace_coerce
np.ndarray)argrV   c                 C  s   |S zA
        For compatibility with 1D-only ExtensionArrays.
        ra   r]   r	  ra   ra   rb   _maybe_squeeze_argj  s    zBlock._maybe_squeeze_argc                 C  s   |S r
  ra   )r]   r   ra   ra   rb   _unwrap_setitem_indexerp  s    zBlock._unwrap_setitem_indexerr   c                 C  s   | j jS rk   )rg   r[   ro   ra   ra   rb   r[   x  s    zBlock.shape)int | tuple[int, int] | tuple[slice, int])rs   rV   c                 C  s
   | j | S rk   r   )r]   rs   ra   ra   rb   iget|  s    z
Block.iget4slice | npt.NDArray[np.bool_] | npt.NDArray[np.intp]c                 C  s
   | j | S )zreturn a slice of my valuesr   )r]   r   ra   ra   rb   r     s    zBlock._slicerg   r   rV   c                 C  s   |r| j  | _ || j |< dS )a  
        Modify block values in-place with new item value.

        If copy=True, first copy the underlying values in place before modifying
        (for Copy-on-Write).

        Notes
        -----
        `set_inplace` never creates a new array or new Block, whereas `setitem`
        _may_ create a new array and always creates a new Block.

        Caller is responsible for checking values.dtype == self.dtype.
        Nrg   r   r]   locsrg   r   ra   ra   rb   set_inplace  s    zBlock.set_inplacenpt.NDArray[np.intp]r   )r   axisr   rV   c                 C  s   | j }|tjkr| j}d}nd}tj|||||d}t| trT| jdkrT|dkrTt	|dkrh|dkrht	|dkrv| j
}t|j| js| ||S | ||S dS )zN
        Take values according to indexer and return them as a block.
        FT)r  
allow_fillr}   rY   Nr   )rg   r   
no_defaultr}   algostake_ndru   ExtensionBlockrZ   r   r   r*   rm   r   r   )r]   r   r  r   r}   rg   r  r   ra   ra   rb   r    s(    

    
zBlock.take_ndnew_placementneeds_maskingc           	      C  sP   |j | jj|d\}}|d}|j| }|| }t|}t||dg}||fS )a  
        Return a list of unstacked blocks of self

        Parameters
        ----------
        unstacker : reshape._Unstacker
        fill_value : int
            Only used in ExtensionBlock._unstack
        new_placement : np.ndarray[np.intp]
        allow_fill : bool
        needs_masking : np.ndarray[bool]

        Returns
        -------
        blocks : list of Block
            New blocks of unstacked values.
        mask : array-like of bool
            The mask of columns of `blocks` we should keep.
        r}   r   r   )Zget_new_valuesrg   Tr   r   new_block_2d)	r]   	unstackerr}   r  r  r   r   r   r   ra   ra   rb   _unstack  s     


zBlock._unstackc                 C  s  |  |}ttj| j}| jdkr(|j}t||| t|dd}zt	|j
|}W n* tk
rz   | |}||| Y S X | j
tkr|| }t|rt|t||}|r| j r| }| |jdkr|jn|} t|tjr|jdkrt|dkr|d }|||< | S )a  
        Attempt self.values[indexer] = value, possibly creating a new array.

        Parameters
        ----------
        indexer : tuple, list-like, array-like, slice, int
            The subset of self.values to set
        value : object
            The value being set
        using_cow: bool, default False
            Signaling if CoW is used.

        Returns
        -------
        Block

        Notes
        -----
        `indexer` is a direct slice/positional indexer. `value` must
        be a compatible shape.
        r   Tr   rY   )r   .)r   r	   rv   ndarrayrg   rZ   r"  rQ   rP   r&   rm   r"   r   setitemr~   r   r,   r>   r   ri   r   r   r   ru   )r]   r   r   r   rg   castedr   vira   ra   rb   r'    s0    




(zBlock.setitemc              	   C  s  |}t tj| j}t|j|\}}t|ttt	fr6t
|tjkrF| j}| |}t|dd}|rx|rr| jddgS | gS zVt|j|}|r| j r| }| |} t|j|| |r| jddgW S | gW S  tk
r   | jdk s| jd dkrHt|s| ||| Y S | d }| j||| |d}	|	g Y S nt|tj}
g }|  }t |D ]^\}}	|}|
r|dd||d f }|dd||d f }|	j|||d}|!| qh| Y S Y nX dS )	a}  
        putmask the data to the block; it is possible that we may create a
        new dtype of block

        Return the resulting block(s).

        Parameters
        ----------
        mask : np.ndarray[bool], SparseArray[bool], or BooleanArray
        new : a ndarray/object
        using_cow: bool, default False

        Returns
        -------
        List[Block]
        Tr   Fr   rY   r   r   N)"r	   rv   r&  rg   r?   r"  ru   r4   r6   r3   r   r   r  r}   r   rP   r   r&   rm   ri   r   r   r=   r"   rZ   r[   r,   r   putmaskZnonzeror'  r   r   r   )r]   r   newr   	orig_maskrg   noopr(  r   r   is_arrayr   r   rs   nsubmaskr   ra   ra   rb   r*  .  sN    



zBlock.putmaskinfer
str | bool	_downcastr   rV   c              
   C  sp  |j | j kstt|tttfr$t| j dk}t|}ttj	| j
}|}|rR|j}t|| \}}	|	r|rx| jddgS |  gS |tjkr| j}| |}zt|j|}
W n tttfk
r   | j dks| jd dkr| |}|j|||d}| j|||d Y S t|tj	tf}g }|  }t|D ]`\}}|}|rZ|dd||d f }|dd||d f }|j||||d	}|| q0| Y S Y nX |
}t||  |}||k	r
t!|rt"|t"|k rt| || t#d
| }t$||| nNt!|rHt|tj	sHt"|| jd krHt%|&|j}t'| ||}|rd|j}| (|gS )a  
        evaluate the block; return result block(s) from the result

        Parameters
        ----------
        other : a ndarray/object
        cond : np.ndarray[bool], SparseArray[bool], or BooleanArray
        _downcast : str or None, default "infer"
            Private because we only specify it when calling from fillna.

        Returns
        -------
        List[Block]
        r   Fr   rY   r   r   r   N)r4  r   zThis should not be reached; call to np.where above is expected to raise ValueError. Please report a bug at github.com/pandas-dev/pandasr   ))rZ   r   ru   r4   r6   r3   r;   r	   rv   r&  rg   r"  r?   r   r   r  r}   r   r&   rm   r   r   r"   r[   r   wherer   rG   r   r   r   r>   sumr,   r   NotImplementedErrorr*  r   r   rN   r   )r]   r   condr4  r   Z	transposerg   
orig_othericondr-  r(  r   r   r.  r   r   rs   r   Zothr0  r   Zaltr   ra   ra   rb   r5  w  s    




  
   

zBlock.where
int | Nonelimitr   r   rV   c           
        s   t |d}| jsd}nt| j}t| j|\}}|rt|rPrJ| jddgS | gS | j d}|j|g d}	|	S |dk	rd||| jd |k< |r| j	|j
|d}	n| j||j
 dd	}	t fd
d|	D S )zs
        fillna on the block with the value. If we fail, then convert to
        ObjectBlock and try again
        r   TFr   r   NrY   r   )r4  c                   s   g | ]}|j |g d qS )r   )r   r   r   ra   rb   r      s   z Block.fillna.<locals>.<listcomp>)r   rx   r8   rg   r?   r   r   ZcumsumrZ   r*  r"  r5  r   )
r]   r   r=  r   r   r   r-  r   r   r   ra   r   rb   fillna  s.    

zBlock.fillnapadr   Zforward)
methodr  indexr   r=  limit_direction
limit_arear}   r   r   r   Index | Nonez
str | Nonez
Any | None)r@  r  rA  r   r=  rB  rC  r}   r   r   rV   c       
         K  sv  t |d}| js6|
r"| jddgS |r,| gS |  gS zt|}W n tk
r\   d }Y nX |d kr| jjdkr|
r| jddgS |r| gS |  gS | jr| j	dkr| j
d dkr|dkr| jt| jf|||||||||	d	|S d }|r |
r| j r| j }n| j}| j}n
| j }ttj|}tj|f|||||||d	| | j||d
}||g|	|
S )Nr   Fr   fr   r   rY   )	r@  r  rA  r   r=  rB  rC  r}   r   )r@  r  rA  r=  rB  rC  r}   r   )r   rx   r   r:   Zclean_fill_methodr   rm   rw   r   rZ   r[   r\   r   interpolateri   r   rg   r	   rv   r&  interpolate_array_2dr   r   )r]   r@  r  rA  r   r=  rB  rC  r}   r   r   r_   r  ri   datar   ra   ra   rb   rF  &  sh    

&
	zBlock.interpolaterY   r/  r  rV   c                 C  s    t j| j||d}| j|dgS )z'return block for the diff of the valuesr  r   r  diffrg   r   r]   r/  r  r   ra   ra   rb   rL  s  s    z
Block.diffperiodsr  r}   rV   c                 C  s   t |s| jtkrtd| |}zt| j|}W n. tk
rd   | |}|j	|||d Y S X t
tj| j}t	||||}| |gS dS )z+shift the block by periods, possibly upcastzfill_value must be a scalar)r  r}   N)r   Z	is_scalarrm   r~   r   r   r&   r"   r   rD   r	   rv   r&  rg   r   )r]   rO  r  r}   r(  r   rg   r   ra   ra   rb   rD   y  s    
 
zBlock.shiftlinearrR   r   )qsinterpolationr  rV   c                 C  sV   | j dkst|dkstt|s&tt| jt|j|}t|dd}t	|| j
dS )aX  
        compute the quantiles of the

        Parameters
        ----------
        qs : Index
            The quantiles to be computed in float64.
        interpolation : str, default 'linear'
            Type of interpolation.
        axis : int, default 0
            Axis to compute.

        Returns
        -------
        Block
        r   rY   r   r!  )rZ   r   r,   r@   rg   rv   asarray_valuesr   r#  r   )r]   rQ  rR  r  r   ra   ra   rb   quantile  s    zBlock.quantile)decimalsr   rV   c                 C  sT   | j r| jr| j| dS d}| j|}|| jkrF| j}|sF| }| j||dS )a  
        Rounds the values.
        If the block is not of an integer or float dtype, nothing happens.
        This is consistent with DataFrame.round behavivor.
        (Note: Series.round would raise)

        Parameters
        ----------
        decimals: int,
            Number of decimal places to round to.
            Caller is responsible for validating this
        using_cow: bool,
            Whether Copy on Write is enabled right now
        r   Nr   )
is_numericrz   r   rg   roundri   r   )r]   rV  r   ri   rg   ra   ra   rb   rX    s    
zBlock.roundc                 C  s  t |s|g}| jdkrTttj| j}t||}| j|}t| ||| jdgS t	|| jj
d krntt|| jj
d gg}| jj}g }d}| j r| jnd}|D ]d}||d krnN| j|d |ddf }||d | }	t| |t|	| j|d}
||
 |}q|S )a  Deletes the locs from the block.

        We split the block to avoid copying the underlying data. We create new
        blocks for every connected segment of the initial block that is not deleted.
        The new blocks point to the initial array.
        rY   r   rZ   r   r   Nr   )r,   rZ   r	   rv   r&  rg   deleter   r   maxr[   
IndexErrorZconcatenateZas_arrayri   r   r   r   )r]   r   rg   r   Zmgr_locs_arrr   Zprevious_locri   idxr  r   ra   ra   rb   rZ    s8    
   
zBlock.deletec                 C  s   t | dS z(return a boolean if I am possibly a viewNr   ro   ra   ra   rb   is_view  s    zBlock.is_viewrG   c                 C  s   t | dS )zP
        The array that Series.array returns. Always an ExtensionArray.
        Nr   ro   ra   ra   rb   array_values  s    zBlock.array_valuesDtypeObj | Nonerm   rV   c                 C  s   t | dS )z
        return an internal format, currently just the ndarray
        this is often overridden to handle to_dense like operations
        Nr   ry   ra   ra   rb   
get_values  s    zBlock.get_valuesc                 C  s   t | d S rk   r   ro   ra   ra   rb   values_for_json$  s    zBlock.values_for_json)NN)NN)NF)F)Fr   F)r   N)T)FNF)FNF)FFF)TFF)F)F)F)r1  F)NFNF)rY   )r   N)rP  r   )F)N)Hr   
__module____qualname____doc____annotations__	__slots__rW  r   r   rl   _validate_ndimr
   r   rp   rx   propertyrz   r{   r}   r   r   setterr   r   r   r   r   r   r   r   r   r   r   r   r\   r   r   rd   r   r   rm   r   r   r   r   r   r  r   r  r  r[   r  r   r  r   r  r  r%  r'  r*  r5  r>  rF  rL  rD   rU  rX  rZ  r_  r`  rc  rd  ra   ra   ra   rb   re      s0  
	

     	      ,   c   5   h   A	'.;J   z    ;&M   "  !$0re   c                      s   e Zd ZU dZded< d(ddddZd)d
dddddZd*dddddZdd fddZe	ddddZ
d+dddddZddddZdd ddddd!d"d#dd$dd%d&d'Z  ZS ),EABackedBlockz>
    Mixin for Block subclasses backed by ExtensionArray.
    rG   rg   Frq   r   c           	   
   C  s   |}|}|  |}| |}| j}|jdkr2|j}t||| z|||< W n ttfk
r } z^t| t	| j
r| |}||| W Y 0S t| tr| |}||| W Y S  W 5 d}~X Y nX | S dS )a  
        Attempt self.values[indexer] = value, possibly creating a new array.

        This differs from Block.setitem by not allowing setitem to change
        the dtype of the Block.

        Parameters
        ----------
        indexer : tuple, list-like, array-like, slice, int
            The subset of self.values to set
        value : object
            The value being set
        using_cow: bool, default False
            Signaling if CoW is used.

        Returns
        -------
        Block

        Notes
        -----
        `indexer` is a direct slice/positional indexer. `value` must
        be a compatible shape.
        r   N)r  r  rg   rZ   r"  rQ   r   r   _catch_deprecated_value_errorr+   rm   r   r'  ru   NDArrayBackedExtensionBlock)	r]   r   r   r   Zorig_indexerZ
orig_valuerg   errr   ra   ra   rb   r'  /  s(    






zEABackedBlock.setitemr1  r2  rW   r3  c              
   C  s  | j j}t|}|}|}| |}| |}|tjkr<| j}t|| \}}	|	rl|rb| jddgS |  gS z|	||j}
W nZ t
tfk
r } z4t| | jdks| jd dkr:t| jr| |}|j|||d}| j|||d W Y S t| tr6| |}|j|||d}| j|||d W Y S  nt|tjtf}g }|  }t|D ]^\}}|}|r|d d ||d f }|d d ||d f }|j|||d}|| q^| W Y S W 5 d }~X Y nX | |
}|gS )NFr   rY   r   r   r   )rg   r"  r;   r  r   r  r}   r?   r   Z_wherer   r   rn  rZ   r[   r+   rm   r   r5  r   ru   ro  rv   r&  rG   r   r   r   r   )r]   r   r8  r4  r   Zarrr9  Z	orig_condr:  r-  r   rp  r   r   r.  r   rs   r   r/  r0  r   ra   ra   rb   r5  i  s`    





  
  
zEABackedBlock.wherer   c              
   C  s  t |}|tjkr| j}| j}|jdkr.|j}|}|}| |}| |}| sj|rd| j	ddgS | gS |r| j
 r|	 }| |jdkr|jn|} z||| W n0 ttfk
r } z
t| | jdks| jd dkr@t| jr| |}||| W Y S t| tr<| |}||| W Y S  nt|tjtf}	g }
|  }t|D ]Z\}}|}|	r|dd||d f }|dd||d f }|||}|
| qd|
 W Y S W 5 d}~X Y nX | gS )z+
        See Block.putmask.__doc__
        r   Fr   rY   r   N)r;   r   r  r}   rg   rZ   r"  r  r   r   ri   r   r   Z_putmaskr   r   rn  r[   r+   rm   r   r*  ru   ro  rv   r&  rG   r   r   r   )r]   r   r+  r   rg   Zorig_newr,  rp  r   r.  r   r   rs   r   r/  r0  r   ra   ra   rb   r*    sV    





zEABackedBlock.putmaskrX   c                   sT   | j dkr8| j|}| j|}t| ||| j dgS | jj dkrHg S t |S )NrY   rY  )rZ   rg   rZ  r   r   super)r]   r   rg   r   	__class__ra   rb   rZ    s    
zEABackedBlock.deletec                 C  s   | j S rk   r   ro   ra   ra   rb   r`    s    zEABackedBlock.array_valuesNra  r  rb  c                 C  s*   | j }|tkr|t}t|| jS )zS
        return object dtype as boxed values, such as Timestamps/Timedelta
        )rg   r~   r   rT   rv   rS  r   r[   )r]   rm   rg   ra   ra   rb   rc    s    
zEABackedBlock.get_valuesc                 C  s   t | jS rk   )rv   rS  rg   ro   ra   ra   rb   rd    s    zEABackedBlock.values_for_jsonr?  r   )r@  r  r   r=  r}   r   r   rh   r;  )r@  r  r   r=  r   c          
      K  sH   | j }|jdkr.|dkr.|jj|||dj}	n|j|||d}	| |	S )Nr   r   r   r@  r=  )rg   rZ   r"  r>  r   )
r]   r@  r  r   r=  r}   r   r_   rg   r   ra   ra   rb   rF    s
    zEABackedBlock.interpolate)F)r1  F)F)N)r   re  rf  rg  rh  r'  r5  r*  rZ  r   r`  rc  rd  rF  __classcell__ra   ra   rr  rb   rm  (  s&   
;   FE
rm  c                      s  e Zd ZU dZdZdZdZded< d9dddd	d
 fddZe	ddddZ
ddddZd:ddddddZdd Zdd ZeddddZe	d d! Zd"dd#d$d%Zed&d d#d'd(Zd;d*d+d	d,d-d.Zd<d*d+d0d	d1d2d3Zd4d5d6d7d8Z  ZS )=r  z
    Block for holding extension types.

    Notes
    -----
    This holds all 3rd-party extension array types. It's also the immediate
    parent class for our internal extension types' blocks.

    ExtensionArrays are limited to 1-D.
    FTrG   rg   Nr;  rq   rW   r<  c           	        sv   t | jr t j|||||dS |r@| jr@| jjs@| j}| j}nd }| jj|d |d}| j||d}|j	|g||dS )N)r   r=  r   r   r   rt  r   r   )
r+   rm   rq  r>  rx   rg   Z_hasnari   r   r   )	r]   r   r=  r   r   r   ri   r   r   rr  ra   rb   r>  6  s    
zExtensionBlock.fillnar   rX   c                 C  s*   | j dkrt| jfS t| jt| jfS )NrY   )rZ   r   rg   r   ro   ra   ra   rb   r[   P  s    
zExtensionBlock.shaper  )rs   c                 C  s   t |trn|\}}t|s2|dkr2t|  dt |trd|dk rR|t| j7 }| j||d  S | j| S |dkrt|  d| jS d S )Nr   z only contains one itemrY   )ru   tuplecomis_null_slicer\  r   r   rg   )r]   rs   colr   ra   ra   rb   r  W  s    	


zExtensionBlock.igetr   r   r  c                 C  s"   |r| j  | _ || j d d < d S rk   r  r  ra   ra   rb   r  s  s    zExtensionBlock.set_inplacec                 C  sv   t |tjtfrF|j| jjd krF|jd dks4t|dddf }n,t |trr|jd dksbt|j	dddj
}|S )z@
        If necessary, squeeze a (N, 1) ndarray to (N,)
        rY   Nr   rJ  )ru   rv   r&  rG   rZ   rg   r[   r   r3   Z_ixsrT  r  ra   ra   rb   r  z  s    
z!ExtensionBlock._maybe_squeeze_argc                 C  s   t |trt|dkrtdd |D rn|\}}|jdkrT|dk rT|jd dks\td|dddf }njt|d r|d dkr|d }nFt	
|d r|d }n.t|d r|d d dkr|d }ntd|S )zw
        Adapt a 2D-indexer to our 1D values.

        This is intended for 'setitem', not 'iget' or '_slice'.
        r   c                 s  s$   | ]}t |tjo|jd kV  qdS )r   N)ru   rv   r&  rZ   r   ra   ra   rb   r     s     z9ExtensionBlock._unwrap_setitem_indexer.<locals>.<genexpr>rY   r   zPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/N)ru   rv  r   r   sizer[   r7  r   
is_integerrw  rx  r,   )r]   r   firstsecondra   ra   rb   r    s.    



z&ExtensionBlock._unwrap_setitem_indexerc                 C  s   dS )z,Extension arrays are never treated as views.Fra   ro   ra   ra   rb   r_    s    zExtensionBlock.is_viewc                 C  s
   | j jjS rk   )rg   rm   Z_is_numericro   ra   ra   rb   rW    s    zExtensionBlock.is_numericr  r   c                 C  sN   | j dkrDt|tstd|td| }t|s<td|td}| j| S )z
        Return a slice of my values.

        Parameters
        ----------
        slicer : slice, ndarray[int], or ndarray[bool]
            Valid (non-reducing) indexer for self.values.

        Returns
        -------
        ExtensionArray
        r   z+invalid slicing for a 1-ndim ExtensionArrayrY   N)rZ   ru   r   r   ranger   rg   )r]   r   Znew_locsra   ra   rb   r     s    

  zExtensionBlock._slicer   c                 C  s$   | j | }t| || j| j| jdS )zN
        Perform __getitem__-like specialized to slicing along index.
        )rZ   ri   )rg   r   r   rZ   ri   )r]   r   r   ra   ra   rb   getitem_block_index  s    
z"ExtensionBlock.getitem_block_indexrY   rh   r   rI  c                 C  s    t j| j|dd}| j|dgS )Nr   rJ  r   rK  rM  ra   ra   rb   rL    s    zExtensionBlock.diffr   r   rN  c                 C  s   | j j||d}| |gS )z
        Shift the block by `periods`.

        Dispatches to underlying ExtensionArray and re-boxes in an
        ExtensionBlock.
        )rO  r}   rg   rD   r   )r]   rO  r  r}   r   ra   ra   rb   rD     s    	zExtensionBlock.shiftr  r  r  c                   sD   |j \}}|j| }|| } fddtt||D }||fS )Nc                   s<   g | ]4\}\}}t jj||  d t|ddqS ))r  r}   r   r   )r   rg   Ztaker   )r   rs   indicesZplacer}   r  r]   ra   rb   r   #  s   	
  z+ExtensionBlock._unstack.<locals>.<listcomp>)Zarange_resultr"  r   r   )r]   r$  r}   r  r  r   r   r   ra   r  rb   r%    s    

	zExtensionBlock._unstack)NFNF)F)rY   )r   N)r   re  rf  rg  rl   rj  r   rh  r>  r   r[   r  r  r  r  rk  r_  rW  r   r
   r  rL  rD   r%  ru  ra   ra   rr  rb   r  $  s8   
    *
(	   r  c                   @  sZ   e Zd ZU ded< eddddZedddd	ZddddddZddddZd
S )
NumpyBlockr  rg   rq   rX   c                 C  s   | j jdk	S r^  )rg   basero   ra   ra   rb   r_  4  s    zNumpyBlock.is_viewrG   c                 C  s
   t | jS rk   )rI   rg   ro   ra   ra   rb   r`  9  s    zNumpyBlock.array_valuesNra  rb  c                 C  s   |t kr| jt S | jS rk   )r~   rg   r   ry   ra   ra   rb   rc  =  s    zNumpyBlock.get_valuesc                 C  s   | j S rk   r   ro   ra   ra   rb   rd  B  s    zNumpyBlock.values_for_json)N)	r   re  rf  rh  rk  r_  r`  rc  rd  ra   ra   ra   rb   r  1  s   
r  c                   @  s   e Zd ZdZdZdS )NumericBlockra   TN)r   re  rf  ri  rW  ra   ra   ra   rb   r  F  s   r  c                   @  sh   e Zd ZU dZded< e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Z
dS )ro  z8
    Block backed by an NDArrayBackedExtensionArray
    rS   rg   rq   rX   c                 C  s   t | jtj S rk   )ru   rm   rv   ro   ra   ra   rb   r   S  s    z(NDArrayBackedExtensionBlock.is_extensionc                 C  s   | j jjdk	S r^  )rg   _ndarrayr  ro   ra   ra   rb   r_  X  s    z#NDArrayBackedExtensionBlock.is_viewr   rh   r   rW   rI  c                 C  s$   | j }||j||d }| |gS )a  
        1st discrete difference.

        Parameters
        ----------
        n : int
            Number of periods to diff.
        axis : int, default 0
            Axis to diff upon.

        Returns
        -------
        A list with a new Block.

        Notes
        -----
        The arguments here are mimicking shift so they are called correctly
        by apply.
        rJ  )rg   rD   r   )r]   r/  r  rg   r   ra   ra   rb   rL  ^  s    z NDArrayBackedExtensionBlock.diffNr   rN  c                 C  s"   | j }|j|||d}| |gS )N)r}   r  r  )r]   rO  r  r}   rg   r   ra   ra   rb   rD   x  s    z!NDArrayBackedExtensionBlock.shift)r   )r   N)r   re  rf  rg  rh  r   r   rk  r_  rL  rD   ra   ra   ra   rb   ro  K  s   
   ro  	Exceptionr   )rp  rV   c                 C  s&   t | tr"t | trndt| kr"dS )z
    We catch ValueError for now, but only a specific one raised by DatetimeArray
    which will no longer be raised in version.2.0.
    z'value.closed' isN)ru   r   r   r   )rp  ra   ra   rb   rn    s
    

rn  c                   @  sZ   e Zd ZU dZdZdZded< dddd	Zd
ddddddddddddddddZdS )DatetimeLikeBlockz*Block for datetime64[ns], timedelta64[ns].ra   FzDatetimeArray | TimedeltaArrayrg   r  rX   c                 C  s   | j jS rk   )rg   r  ro   ra   ra   rb   rd    s    z!DatetimeLikeBlock.values_for_jsonr?  Nr   )r@  rA  r  r   r=  r}   r   r   rD  rh   rq   r;  )r@  rA  r  r   r=  r   c                K  s   | j }	|dkrd }
|r>|r2| j s2|	j}| j}
qR|	j }n|rH|	jn|	j }tj|||||d t|	j||	j	d}| j
||
dS |	jdkr|dkr|	jj|||dj}n|	j|||d}| 
|S )NrP  )r@  r=  rA  r  r   r   r   r   rt  )rg   ri   r   r  r   r:   rG  r   Z_simple_newrm   r   rZ   r"  r>  )r]   r@  rA  r  r   r=  r}   r   r_   rg   ri   Zdata_outr   ra   ra   rb   rF    s,        zDatetimeLikeBlock.interpolate)	r   re  rf  rg  ri  rW  rh  rd  rF  ra   ra   ra   rb   r    s   
r  c                   @  s0   e Zd ZU dZded< dZdZdZdZe	j
Z
dS )DatetimeTZBlockz0implement a datetime64 block with a tz attributerF   rg   ra   TFN)r   re  rf  rg  rh  ri  r   rj  rl   ro  rd  ra   ra   ra   rb   r    s   
r  c                   @  s2   e Zd ZdZdZeddddddddd	Zd
S )ObjectBlockra   TFr   rq   rW   r   c                C  s   | j tkr"|r| jddgS | gS | j}|jdkr:|d }tj|ddddd}d}|rh||krh| }n||krz|rz| j}t|| j}| j	||dgS )	z
        attempt to cast any object types to better types return a copy of
        the block (if copy = True) by definition we ARE an ObjectBlock!!!!!
        Fr   r   r   T)Zconvert_datetimeZconvert_timedeltaZconvert_periodZconvert_intervalNr   )
rm   r~   r   rg   rZ   r   Zmaybe_convert_objectsri   r   r   )r]   r   r   rg   r   ri   ra   ra   rb   r     s*    


zObjectBlock.convertN)r   re  rf  ri  r   rd   r   ra   ra   ra   rb   r    s   r  r   )rg   rV   c                 C  sV   t | tjr0t| } t| jjtr0tj| t	d} t | t
tfrR| jdk	rR| d} | S )a:  
    Input validation for values passed to __init__. Ensure that
    any datetime64/timedelta64 dtypes are in nanoseconds.  Ensure
    that we do not have string dtypes.

    Parameters
    ----------
    values : np.ndarray or ExtensionArray

    Returns
    -------
    values : np.ndarray or ExtensionArray
    r   N)ru   rv   r&  rO   
issubclassrm   r   r   r   rT   rF   rK   freqZ
_with_freqr   ra   ra   rb   r   	  s    
r   r   r   c                 C  sj   | j }t| trt}nPt| tr&t}n@t| tr6t}n0t| trFt}n |dkrTt	}n|dkrbt
}nt}|S )z
    Find the appropriate Block subclass to use for the given values and dtype.

    Parameters
    ----------
    dtype : numpy or pandas dtype

    Returns
    -------
    cls : class, subclass of Block
    )Mr  )rE  crs   rt   rr   )rw   ru   rL   r  r/   r  r2   ro  r0   r  r  r  )rm   rw   clsra   ra   rb   get_block_type	  s    



r  r   r   )rg   r   ri   c                 C  s"   t | j}t| } || d||dS )Nr   rZ   r   ri   )r  rm   r   )rg   r   ri   klassra   ra   rb   r#  D	  s    
r#  r   rh   )rZ   ri   rV   c                C  s@   t |tst|}t| || t| j}t| } || |||dS )Nr  )ru   r   
check_ndimr  rm   r   )rg   r   rZ   ri   r  ra   ra   rb   r   Q	  s    

r   )r   rZ   rV   c                 C  s   | j |kr"td| j  d| dt| js|| j |krNtd| j  d| dt|t| krtdt|  dt| n|dkrt|d	krtd
dS )aV  
    ndim inference and validation.

    Validates that values.ndim and ndim are consistent.
    Validates that len(values) and len(placement) are consistent.

    Parameters
    ----------
    values : array-like
    placement : BlockPlacement
    ndim : int

    Raises
    ------
    ValueError : the number of dimensions do not match
    z0Wrong number of dimensions. values.ndim > ndim [z > ]z1Wrong number of dimensions. values.ndim != ndim [z != zWrong number of items passed z, placement implies r   rY   zneed to splitN)rZ   r   r(   rm   r   )rg   r   rZ   ra   ra   rb   r  a	  s    


r  rf   ra  z3tuple[np.ndarray | ExtensionArray, DtypeObj | None])rg   rm   rZ   rV   c                 C  s@   t | tr(|  } |r(|dkr(t| } t |tr8|j}| |fS )zL
    Ensure that we don't allow PandasArray / PandasDtype in internals.
    rY   )ru   r5   Zto_numpyrv   Z
atleast_2dr1   Znumpy_dtype)rg   rm   rZ   ra   ra   rb   extract_pandas_array	  s    


r  rW   rX   c                 C  sf   |dkrg }t | trB| D ]$}t |tr4|| q|| qn t | tsXtt| ||  |S )z.return a new extended blocks, given the resultN)ru   r   r   r   re   r   r   )r   r   rra   ra   rb   r   	  s    


r   rY   )rg   rZ   rV   c                 C  s.   | j |k r*t| js*td| } | dd} | S )z:
    Reshape if possible to have values.ndim == ndim.
    z+np.ndarray | DatetimeArray | TimedeltaArrayrY   r   )rZ   r(   rm   r	   r   )rg   rZ   ra   ra   rb   r   	  s
    


r   r   .)r   r   float_formatdecimalr   r  )rg   r   r  rV   c                K  s0  t | tr2| jjjdkr2tj| jjt| j	|d} t
| } t | ttfr| jdkrx| jf d|i|}|jtdd}|S g }tt| D ]8}| |ddf jf d|i|}||jtdd qt|S | jjdkrtt| st|dkr:|d	kr:t| }	|s| t} ntj| d
d} || |	< | jtdd} | S ddlm}
 |
| ||||dd}| }|jtdd}|S t | trt| }	t| t}|||	< |S t| }	t |}| jt!kr|s|r| t} | jj"tdj" |k r| d| } ntj| d
d} || |	< | jtdd} | S dS )r   ZMmr   rY   r   Fr   NrE  r  rT   r   r   )FloatArrayFormatter)r   r  r  r   Zfixed_widthZU1z<U)#ru   rE   
categoriesrm   rw   r  r  rT  r'   _codesrO   rF   rK   rZ   Z_format_native_typesr   rT   r~  r   r   rv   Zvstackr-   r8   r   r   Zpandas.io.formats.formatr  Zget_result_as_arrayrG   rS  r   Zword_lenr~   itemsize)rg   r   r   r  r  r_   r   Zresults_convertedrs   r   r  	formatterresr   r  ra   ra   rb   r   	  sj    

 


r   c                 C  sR   t | ttfr| tS t | ttfr,| j} t | tj	rNt
 rN|  } d| j_| S )a+  
    The array that Series.values returns (public attribute).

    This has some historical constraints, and is overridden in block
    subclasses to return the correct array (e.g. period returns
    object ndarray and datetimetz a datetime64[ns] ndarray instead of
    proper extension array).
    F)ru   rJ   rH   r   rT   rF   rK   r  rv   r&  r   viewflagsZ	writeabler   ra   ra   rb   r{   
  s    	
r{   )N)N)rY   )
__future__r   	functoolsr   r   typingr   r   r   r   r   r	   r
   Znumpyrv   Zpandas._configr   Zpandas._libsr   Zlibinternalsr   r   Zpandas._libs.internalsr   r   Zpandas._libs.missingr   Zpandas._libs.tslibsr   Zpandas._typingr   r   r   r   r   r   r   r   r   Zpandas.errorsr   Zpandas.util._decoratorsr   Zpandas.util._validatorsr   Zpandas.core.dtypes.astyper    r!   Zpandas.core.dtypes.castr"   r#   r$   r%   r&   Zpandas.core.dtypes.commonr'   r(   r)   r*   r+   r,   r-   r.   Zpandas.core.dtypes.dtypesr/   r0   r1   r2   Zpandas.core.dtypes.genericr3   r4   r5   r6   Zpandas.core.dtypes.missingr7   r8   r9   Zpandas.corer:   Zpandas.core.algorithmscoreZ
algorithmsr  Zpandas.core.array_algos.putmaskr;   r<   r=   r>   r?   Z pandas.core.array_algos.quantiler@   Zpandas.core.array_algos.replacerA   rB   rC   Z"pandas.core.array_algos.transformsrD   Zpandas.core.arraysrE   rF   rG   rH   rI   rJ   rK   Zpandas.core.arrays.sparserL   Zpandas.core.baserM   Zpandas.core.commoncommonrw  Zpandas.core.computationrN   Zpandas.core.constructionrO   rP   Zpandas.core.indexersrQ   Zpandas.core.apirR   Zpandas.core.arrays._mixinsrS   rm   r~   rd   re   rm  r  r  r  ZNDArrayBackedBlockro  rn  r  r  r  r   r  r#  r   r  r  r   r   r   r{   ra   ra   ra   rb   <module>   s   $
,(
$	
           # }  541' *W