HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/internals/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/internals/__pycache__/blocks.cpython-36.pyc
3

���h>t�@sfddlmZmZddlZddlZddlmZmZmZmZddl	Z	ddl
Zddlm
Z
mZmZmZddljjZddlmZddlmZddlmZddlmZdd	lmZdd
lm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ddlAmBZBdd
lCmDZDmEZEmFZFmGZGddlHmIZImJZJmKZKddlLjMjNZddlOmPZPddlQmRZRmSZSmTZTmUZUmVZVmWZWddlXmYZYddlZjMj[Z\ddl]m^Z^ddl_m`Z`maZambZbddlcjMjdZdddlemfZfe�r
ddlgmhZhGdd�deY�ZiGdd�dei�ZjGdd�dej�ZkGdd�dei�ZlGdd�del�ZmGd d!�d!em�ZnGd"d#�d#em�ZoGd$d%�d%el�ZpGd&d'�d'�ZqGd(d)�d)eqei�ZrGd*d+�d+ejer�ZsGd,d-�d-eqep�ZtGd.d/�d/el�ZuGd0d1�d1ei�ZvGd2d3�d3ej�ZwdFd4d5�ZxdGd6d7�ZydHd8d9�ZzdIee{ed;�d<d=�Z|d>d?�Z}ej~ej~ej~d@�dAdB�Zeej~dC�dDdE�Z�dS)J�)�datetime�	timedeltaN)�
TYPE_CHECKING�Any�List�Optional)�NaT�algos�lib�writers)�BlockPlacement)�
conversion)�
tz_compare)�	ArrayLike)�validate_bool_kwarg)�astype_nansafe�convert_scalar_for_putitemlike�find_common_type�infer_dtype_from�infer_dtype_from_scalar�maybe_downcast_numeric�maybe_downcast_to_dtype�maybe_infer_dtype_type�
maybe_promote�maybe_upcast�soft_convert_objects)�DT64NS_DTYPE�TD64NS_DTYPE�
is_bool_dtype�is_categorical_dtype�is_datetime64_dtype�is_datetime64tz_dtype�is_dtype_equal�is_extension_array_dtype�is_float�is_float_dtype�
is_integer�is_integer_dtype�is_interval_dtype�is_list_like�is_object_dtype�is_period_dtype�is_re�is_re_compilable�	is_sparse�is_timedelta64_dtype�pandas_dtype)�ExtensionDtype)�ABCDataFrame�
ABCIndexClass�ABCPandasArray�	ABCSeries)�_isna_compat�is_valid_nat_for_dtype�isna)�shift)�Categorical�
DatetimeArray�ExtensionArray�PandasArray�PandasDtype�TimedeltaArray)�PandasObject)�
extract_array)�check_setitem_lengths�is_empty_indexer�is_scalar_indexer)�
nanpercentile)�Indexc@s�eZdZdZdddgZdZdZdZdZdZ	dZ
dZdZdZ
dZdZdZdZdZeeeedd�dd	��Zd�dd�Zd
d�Zedd��Zedd��Zeed�dd��Zeed�dd��Zdd�Z dd�Z!e"d�dd�Z#d�dd�Z$e%j&d�d d!�Z'ed"d#��Z(ed$d%��Z)e)j*d&d%��Z)d�dd�d'd(�Z+d�d)d*�Z,e-d�d+d,�Z.ed�d-d.�Z/d/d0�Z0d1d2�Z1d3d4�Z2d�d5d6�Z3ed7d8��Z4ed9d:��Z5d;d<�Z6d=d>�Z7d
d�d?d@�Z8e9dd�dAdB�Z:e9dd�dCdD�Z;d�ee9ddE�dFdG�Z<ee9ddE�dHdI�Z=d�e9de9ddJ�dKdL�Z>d�dMdN�Z?d�ee-dP�dQdR�Z@d�eeeeedS�dTdU�ZAeBedV�dWdX�ZCeedY�dZd[�ZDd�d]d^�ZEd�ed_�d`da�ZFd�eeedb�dcdd�ZGdedf�ZHdgdh�ZId�eeee9ddj�dkdl�ZJdmdn�ZKd�e-eeLdqeeLee-eLe-eLeBeeLe-dr�
dsdt�ZMd�e-eeeLeeLeBeeLe-e9ddu�dvdw�ZNd�e-dqeLeBeeLee-eLe-eeLe-e9ddx�
dydz�ZOd
ePjQfed{�d|d}�ZRd�eee9dd�d�d��ZSd�eed��d�d��ZTd�eee9dd��d�d��ZUd�d��ZVd�ed{�d�d��ZWd�eeedb�d�d��ZXd
S)��Blockz�
    Canonical n-dimensional unit of homogeneous dtype contained in a pandas
    data structure

    Index-ignorant; let the container take care of that
    �	_mgr_locs�values�ndimFT)rI�	placementrJ�returncCs tj|�}||_||_||_|S)z<
        Fastpath constructor, does *no* validation
        )�object�__new__rJrIrH)�clsrIrKrJ�obj�rQ�>/tmp/pip-build-5_djhm0z/pandas/pandas/core/internals/blocks.py�_simple_newos

zBlock._simple_newNcCs^|j||�|_||_||_|jrZ|jrZt|j�t|j�krZtdt|j��dt|j�����dS)NzWrong number of items passed z, placement implies )�_check_ndimrJ�mgr_locsrI�_validate_ndim�len�
ValueError)�selfrIrKrJrQrQrR�__init__|s zBlock.__init__cCs:|dkr|j}|jr6|j|kr6td|j�d|�d���|S)a�
        ndim inference and validation.

        Infers ndim from 'values' if not provided to __init__.
        Validates that values.ndim and ndim are consistent if and only if
        the class variable '_validate_ndim' is True.

        Parameters
        ----------
        values : array-like
        ndim : int or None

        Returns
        -------
        ndim : int

        Raises
        ------
        ValueError : the number of dimensions do not match
        Nz1Wrong number of dimensions. values.ndim != ndim [z != �])rJrVrX)rYrIrJrQrQrRrT�szBlock._check_ndimcCsdS)z�
        The array-like that can hold the underlying values.

        None for 'Block', overridden by subclasses that don't
        use an ndarray.
        NrQ)rYrQrQrR�_holder�sz
Block._holdercCs|j|jjfS)N)�_can_consolidate�dtype�name)rYrQrQrR�_consolidate_key�szBlock._consolidate_key)rLcCs|jjdk	S)z* return a boolean if I am possibly a view N)rI�base)rYrQrQrR�is_view�sz
Block.is_viewcCs|jp
|jS)z$ return True if I am a non-datelike )�is_datetime�is_timedelta)rYrQrQrR�is_datelike�szBlock.is_datelikecCs|jS)aC
        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).
        )rI)rYrQrQrR�external_values�s	zBlock.external_valuescCs|jS)zJ
        The array that Series._values returns (internal values).
        )rI)rYrQrQrR�internal_values�szBlock.internal_valuescCs
t|j�S)zP
        The array that Series.array returns. Always an ExtensionArray.
        )r=rI)rYrQrQrR�array_values�szBlock.array_valuescCst|�r|jjt�S|jS)z�
        return an internal format, currently just the ndarray
        this is often overridden to handle to_dense like operations
        )r*rI�astyperM)rYr^rQrQrR�
get_values�szBlock.get_valuescCstj|j�j|j�S)z2
        This is used in the JSON C code.
        )�np�asarrayrI�reshape�shape)rYrQrQrR�get_block_values_for_json�szBlock.get_block_values_for_jsoncCstjS)N)rk�nan)rYrQrQrR�
fill_value�szBlock.fill_valuecCs|jS)N)rH)rYrQrQrRrU�szBlock.mgr_locscCs t|tj�stj|�}||_dS)N)�
isinstance�libinternalsrrH)rY�new_mgr_locsrQrQrRrU�s
cCs2|dkr|j}|jr"t||jd�}t|||jd�S)zm
        Create a new block, with type inference propagate any values that are
        not specified
        N)rJ)rKrJ)rU�is_extension�_block_shaperJ�
make_block)rYrIrKrQrQrRrw�s
zBlock.make_blockcCs.|dkr|j}|dkr|j}t|�|||d�S)z4 Wrap given values in a block of same type as self. N)rKrJ)rUrJ�type)rYrIrKrJrQrQrR�make_block_same_classs
zBlock.make_block_same_classcCsjt|�j}|jdkr0|�dt|��d|j��}n6djdd�|jD��}|�d|jj�d|�d|j��}|S)	N�z: z dtype: z x css|]}t|�VqdS)N)�str)�.0�srQrQrR�	<genexpr>sz!Block.__repr__.<locals>.<genexpr>z, z	, dtype: )	rx�__name__rJrWr^�joinrnrU�indexer)rYr_�resultrnrQrQrR�__repr__s

 zBlock.__repr__cCs
t|j�S)N)rWrI)rYrQrQrR�__len__sz
Block.__len__cCs|jj|jfS)N)rUr�rI)rYrQrQrR�__getstate__szBlock.__getstate__cCs(tj|d�|_|d|_|jj|_dS)Nrrz)rsrrUrIrJ)rY�staterQrQrR�__setstate__s
zBlock.__setstate__cCs
|j|S)z return a slice of my values )rI)rY�slicerrQrQrR�_sliceszBlock._slicecCst|dkr*t|t�r|dn|}|j|}nt|t�s<t|�}|j|�}|jr`|j|jkr`td��t|�j	|||j�S)z�
        Perform __getitem__-like, return result as block.

        As of now, only supports slices that preserve dimensionality.
        Nrz Only same dim slicing is allowed)
rr�tuplerUrr�rVrJrXrxrS)rYr�rtZaxis0_slicer�
new_valuesrQrQrR�
getitem_block$s

zBlock.getitem_blockcCs|jjS)N)rIrn)rYrQrQrRrn7szBlock.shapecCs|jjS)N)rIr^)rYrQrQrRr^;szBlock.dtypecCs
|j|S)N)rI)rY�irQrQrR�iget?sz
Block.igetcCs||j|<dS)z�
        Modify block values in-place with new item value.

        Notes
        -----
        `set` never creates a new array or new Block, whereas `setitem` _may_
        create a new array and always creates a new Block.
        N)rI)rY�locsrIrQrQrR�setBs	z	Block.setcCs$tj|j|d�|_|jj|�|_dS)z;
        Delete given loc(-s) from block in-place.
        rN)rk�deleterIrU)rY�locrQrQrRr�MszBlock.deletec
Ks0tjdd��||jf|�}WdQRX|j|�S)z[
        apply the function to my values; return a block if we are not
        one
        �ignore)�allN)rkZerrstaterI�_split_op_result)rY�func�kwargsr�rQrQrR�applyTszBlock.applycCsnt|�rT|jdkrTg}x8t|j�D]*\}}||}|j||gd�}|j|�q"W|St|t�sh|j|�}|gS)Nrz)rIrK)r#rJ�	enumeraterUrw�appendrrrG)rYr�Znbsr�r��vals�blockrQrQrRr�^s

zBlock._split_op_result)�inplacerLcs�t�d��t�j�}�dk	rBtjd�d��d||j�jd��k<�js\�rR�gS�j�gS�j	��r��j
|��d�}�j||�S|j�s��r��gS�j�gS����fdd�}�j
d|��S)	zs
        fillna on the block with the value. If we fail, then convert to
        ObjectBlock and try again
        r�N)�limitFrz)r�cs8�j��}|dk	r&|jt||d��}|j���dd�S)Nrz)r�r��downcast)�coerce_to_target_dtyper��slice�fillna)�mask�val�idxr�)r�r�rY�valuerQrR�f�s
zBlock.fillna.<locals>.f)rr8rI�libalgosZ_validate_limitZcumsumrJ�_can_hold_na�copy�_can_hold_element�putmask�_maybe_downcast�any�split_and_operate)rYr�r�r�r�r��blocksr�rQ)r�r�rYr�rRr�os 



	zBlock.fillnac
s�|dkrtjd�jd�}�j}�fdd�}�jdkrl|j�rJ|||d�}n|rR|n|j�}||�j�}|gSg}xbt�j�D]T\}	}
||	}||	}|j�r�||||	�}n|r�|n|j�}|||
g�}|j	|�q|W|S)a�
        split the block per-column, and apply the callable f
        per-column, return a new block for each. Handle
        masking which will not change a block unless needed.

        Parameters
        ----------
        mask : 2-d boolean mask
        f : callable accepting (1d-mask, 1d values, indexer)
        inplace : boolean

        Returns
        -------
        list of blocks
        NT)rncsZt|t�r:t|�dkst|��t|dt�s0t�|d}nt|�jd�}�j||d�}|S)Nrzr)rJ)rIrK)rr�listrW�AssertionErrorrGrvrJrw)�nv�ref_locr�)rYrQrR�make_a_block�s

z-Block.split_and_operate.<locals>.make_a_blockrz)
rkZbroadcast_tornrIrJr�r�rUr�r�)
rYr�r�r�r�r�r�r��
new_blocksr�r��m�vrQ)rYrRr��s(
zBlock.split_and_operate)r�rLcs4�dkr|js|js|jr|St�fdd�|D��S)Ncsg|]}|j���qSrQ)r�)r|�b)r�rQrR�
<listcomp>�sz)Block._maybe_downcast.<locals>.<listcomp>)r$rdrc�_extend_blocks)rYr�r�rQ)r�rRr��szBlock._maybe_downcastcCs�|dkr|S|j}|jdkr<|dkr(d}t||�}|j|�S|dkrH|S|dkpXt|t�sdtd��n|dkrttd��dd�}|jd|d�S)	z< try to downcast each item to the dict of dtypes if present FrzN�inferz:downcast must have a dictionary or 'infer' as its argumentz#dtypes as dict is not supported yetcSst|dd�}|S)Nr�)r^)r)r�r�r�rQrQrRr��szBlock.downcast.<locals>.f)	rIrJrrwrr�dictrXr�r�)rYZdtypesrIr�r�rQrQrRr��s"


zBlock.downcast�raise)r��errorsc
Csd}||kr*dt|��d|�d�}t|��tj|�rTt|t�rTd|j�d�}t|��|dk	rdt|�}t	|�r�t	|j
j�r�|j|j
j
||d	��S|jt|j
|d
��St|�}t|j|�r�|r�|j�S|S|j�ry|j
j
|�}Wn.ttfk
�r|dk�r|j
}n�YnXn�t|jt��r8|j�r0|j�}n|j
}n|j|d
�}|j�}yt||dd	�}Wn8ttfk
�r�|dk�r��|�r�|j�n|}	|	SXt|tj��r�|j|j�}t||j|jd�}	|	j �r|j �r|	j|jk�rtd
|�d|jj!�d|j�d|	jj!�d|	j�d���|	S)a�
        Coerce to the new dtype.

        Parameters
        ----------
        dtype : str, dtype convertible
        copy : bool, default False
            copy if indicated
        errors : str, {'raise', 'ignore'}, default 'ignore'
            - ``raise`` : allow exceptions to be raised
            - ``ignore`` : suppress exceptions. On error return original object

        Returns
        -------
        Block
        r�r�z.Expected value of kwarg 'errors' to be one of z. Supplied value is '�'zExpected an instance of z7, but got the class instead. Try instantiating 'dtype'.N)r�)r^T)rKrJzcannot set astype for copy = [z
] for dtype (z [z]) to different shape (z]))r�r�)"r�rX�inspectZisclass�
issubclassr1r�	TypeErrorr0rrIr^rwrir:r"r�rurxr{re�to_native_typesrj�ravelrrrrk�ndarrayrmrnrUrJ�
is_numericr_)
rYr^r�r�Zerrors_legal_valuesZinvalid_arg�msgrIZvals1d�newbrQrQrRris\



2zBlock.astype)r�r�numericr�coercecCs|r|j�S|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!
        )r�)rYr�rr�rr�rQrQrR�convertks
z
Block.convert)�elementrLcCs0|jjj}t|�}|dk	r&t|j|�St||�S)z% require the same dtype as ourselves N)rIr^rxrr�rr)rYr�r^�tiporQrQrRr�zs

zBlock._can_hold_element)r�rLcCst|j|j�S)z�
        Should we set self.values[indexer] = value inplace or do we need to cast?

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

        Returns
        -------
        bool
        )r"r^)rYr�rQrQrR�should_store�szBlock.should_storerpcKsx|j}t|�}tj|�}|jr^|r^|r^|jt�}|jjt	jd�j|krl|jd|���}nt	j
|dd�}|||<|S)z$ convert to our native types format ZU1z<UrM)r^)rIr8rZword_len�	is_objectrir{r^�itemsizerk�array)rY�na_rep�quotingr�rIr�r�rQrQrRr��s

zBlock.to_native_types)�deepcCs"|j}|r|j�}|j||jd�S)z copy constructor )rJ)rIr�ryrJ)rYr�rIrQrQrRr��sz
Block.copy)r��regexr�c
svt�d��|}�j|�s�t|t�s6�r,�gS�j�gS�fdd�|D�}t|�sd�rZ�gS�j�gSt|�dkr��j|d|�||d�St��r�t��j	t
�}|j||�||d�S�j}tj
|�r�t|tj�r�t||j�}tj||�}	y�j|	|�d�}
Wn^ttfk
�rXt���r��j�s:�j|��s:t|���j	t
�}|j||�||d�SX|�rr�fd	d�|
D�}
|
S)
z�
        replace the to_replace value with value, possible to create new
        blocks here this is just a call to putmask. regex is not used here.
        It is used in ObjectBlocks.  It is here for API compatibility.
        r�csg|]}�j|�r|�qSrQ)r�)r|�x)rYrQrRr��sz!Block.replace.<locals>.<listcomp>rzr)r�r�r�)�
to_replacer�r�r�r�)r�csg|]}|jd�d��qS)F)r�r�)r�)r|r�)r�rQrRr�s)rr�rrr�r�rW�replacer*r�rirMrIr
�	is_scalarrkr�rr^�missingZmask_missingr�r�rXru)rYr�r�r�r�r�Zoriginal_to_replacer�rIr�r�rQ)r�rYrRr��sZ








z
Block.replacecOs|dr|S|j�S)z no-op on a non-ObjectBlock r�)r�)rY�argsr�rQrQrR�_replace_single
szBlock._replace_singlecCs|jdk}t|tj�r2|j|jkr2td|j����|dkrF|jrFtj}|j}|j|�rnt	j
|�r�t||j�}n�t
|d�r�|j}nLt	j
|�r�t|�r�t|dd�\}}n&ttj|�j�\}}|j|�j||�St|j|g�}t|j|�s�t�|j|�j||�Stt|dd���rd}|}nd}tj|�}|�r4|j}t|||�t|j��ol|jd|jdk�ol|j|jk}	t||��r|n�t||j��r�|||<nn|	�r�t |j��r�|||<|j!t"|j|jd	��S|	�r�|�r�|j!|�S|	�r�|||<|j|jdd
�}n|||<|�r|j}|j!|�}
|
S)a�
        Attempt self.values[indexer] = value, possibly creating a new array.

        Parameters
        ----------
        indexer : tuple, list-like, array-like, slice
            The subset of self.values to set
        value : object
            The value being set

        Returns
        -------
        Block

        Notes
        -----
        `indexer` is a direct slice/positional indexer. `value` must
        be a compatible shape.
        �zCannot set values with ndim > Nr^T)r0Fr)r^)r�)#rJrrrkr�rXr�rprIr�r
r�rr^�hasattrr8rrr�ri�setitemrr"r�r#�getattr�TrBrWrn�sizerCrDrrwr:)rYr�r��	transposerIr^�_Zis_ea_valueZ	arr_valueZexact_matchr�rQrQrRr�s^







z
Block.setitemr)r��axisr�rLc
sVt|�}t�tttf�st�|j}t��rDt��rD|j	rD|j
�|j���rttj
��rht�|jj��|rr|j}t�dd�dkr�|jd�jkr�|dkr�tj�|jd�j|j���j|j��||jkr�|r�|j�}t��o�tj||�o�t�dd�dk�rd||jd	t��k�r(tj||��n:|jd
t��k�sJt��dk�rZtj||��ntd��ntj||��n�|j��r|�r�|j}t�tj��r��j�|j|d}t�dd�dk�r�|jd�jk�r�t�j�}|j |d��jt!|����fdd�}|j"|||�}	|	S|�r|gS|�rJ|dk�rD|�r:|jn|jj�}|j}|j#|�gS)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
        inplace : bool, default False
            Perform inplace modification.
        axis : int
        transpose : bool, default False
            Set to True if self is stored with axes reversed.

        Returns
        -------
        List[Block]
        rJrrzz-cannot assign mismatch length to masked arraycsf|dkr�}nHt�tj�r4tj�|�jd�}n
tj��}t|j�\}}|j|�}t	|||�}|S)Nr)
rrrkr�Zsqueezernr�rr^ri�_putmask_smart)r�r�r��nr^r�r�)�newrQrRr��s

zBlock.putmask.<locals>.fN���r�r�)$�_extract_bool_arrayrrr3r5r2r�rIr)r8r�rqr�r
r�rr^r�r�rJrk�repeatrnrmrir�r�rW�placer�rXr�r��insertr�r�rw)
rYr�r�r�r�r�r��	new_shaper�r�rQ)r�rRr��s\
	"



z
Block.putmaskc
CsJt|dd�\}}t|j|�r |S|js6t|�s6t|�r@|jt�S|jsL|j	r`t
|�s\t|�r`|S|jsvt
|�svt|�r�t
|�s�t|�o�|js�|jt�St|jdd�}t|dd�}t||�s�|jt�Std|�d|����n>|js�t|��rt|�o�|j�s|jt�Std|�d|����y
|j|�Stttfk
�rD|jt�SXdS)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
        T)r0�tzNz.possible recursion in coerce_to_target_dtype: � )rr"r^�is_boolr*rrirMr$�
is_complexr'r%rcr r!r�rr�rdr/rXr��
OverflowError)rY�otherr^r�ZmytzZothertzrQrQrRr�s:	





zBlock.coerce_to_target_dtype�pad�forwardrF)
�methodr��indexr�r��limit_direction�
limit_arearqr�r�c
Ks�t|d�}|js|jr.|jr.|r&|S|j�Sytj|�}Wntk
rTd}YnX|dk	rv|j||||||	|
d�Stj	|f|�}|dk	s�t
�|jf|||||||||
d�	|��S)Nr�)r�r�r�r�rqr�r�)	r�r�r�r�r�r�rqr�r�)rr�r&rdr�r�Zclean_fill_methodrX�_interpolate_with_fillZclean_interp_methodr��_interpolate)
rYr�r�r�r�r�r�r�rqr�r�r�r�rQrQrR�interpolate@s:

zBlock.interpolate)r�r�r�r�rqr�r�rLc
Cs�t|d�}|r(|js(|r|gS|j�gS|r2|jn|jj�}t||jj�}tj||||||jd�}|j||j	d�g}	|j
|	|�S)z, fillna but using the interpolate machinery r�)r�r�r�rqr^)rJ)rr�r�rIrr^r�Zinterpolate_2dryrJr�)
rYr�r�r�r�rqr�r�rIr�rQrQrRr�ws"


zBlock._interpolate_with_fill)
r�r�rqr�r�r�r�r�r�rLc
s�t|d�}|r|jn|jj�}|js<|js0|gS|jtj�}�dkrJ|j��d
krf�j	sft
��d���tjtjd��������fdd	�}tj|||�}
|j
|
�g}|j||	�S)z" interpolate using scipy wrappers r�N�krogh�piecewise_polynomial�pchipz4 interpolation requires that the index be monotonic.)�yvaluesrLcs"tjf�|�����dd����S)NF)Zxvaluesrr�r�r�r�rqZbounds_error)r�Zinterpolate_1d)r)rqr�r�r�r�r�r�rQrRr��sz Block._interpolate.<locals>.func)r�rr)rrIr�r$r&rirkZfloat64rqZis_monotonicrXr�Zapply_along_axisryr�)rYr�r�rqr�r�r�r�r�r�r��datar�Z
interp_valuesr�rQ)rqr�r�r�r�r�r�rRr��s 
$zBlock._interpolate)r�cCs�|j}|tjkr|j}d}nd}tj|||||d�}|dkoB|dksJt�|dkrX|j}t|j	|j	�sr|j
||�S|j||�SdS)zQ
        Take values according to indexer and return them as a block.bb

        FT)r��
allow_fillrqrN)rIr
�
no_defaultrqr	�take_ndr�rUr"r^rwry)rYr�r�rtrqrIrr�rQrQrRr�s	
z
Block.take_ndrz)r�r�rLcCs"tj|j||dd�}|j|d�gS)z) return block for the diff of the values �)r��
stacklevel)rI)r	�diffrIrw)rYr�r�r�rQrQrRr	�sz
Block.diff)�periodsr�cCs*t|j|�\}}t||||�}|j|�gS)z- shift the block by periods, possibly upcast )rrIr9rw)rYr
r�rqr�rQrQrRr9�szBlock.shift)�try_castr�rLc
sddljjj�t���t|tttf�s,t	�|dks8t	��j
dk}�j}|}|rV|j}t
|dd�dkr�|j
d|j
kr�|dkr�|jt|jd��}n|r�|j
�j
dkr��j�t�d�s�td	����fd
d�}	�jd�j�r�|}
nNy|	�||�}
Wn<tk
�r.�j|�}|j|�|||d
�}�j|d�SX�j�sD�j
dk�r\|�rP|
j}
�j|
�gS�j
d}�j|d��tj�fdd�t�jd�D�td�}
g}x`|
|
gD]R}|j ��r�|
j!|j"�d|d�}t#|�j$�}�j|j�j%|d�}|j&|��q�W|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
        errors : str, {'raise', 'ignore'}, default 'raise'
            - ``raise`` : allow exceptions to be raised
            - ``ignore`` : suppress exceptions. On error return original object
        axis : int, default 0

        Returns
        -------
        List[Block]
        rNr�r�r�rJrzrnz0where must have a condition that is ndarray likecsb�js�jotj|�otj|�sP�j|�s.t�tj|�rPt	|tj
�rPt||j�}�j
|||�}|S)N)r&r�r
r$rk�isnanr�r�r�rrr�rr^�where)�condrIr�Zfastres)�expressionsrYrQrR�
where_func0s


zBlock.where.<locals>.where_func�K)r�rr�r�csg|]}�|j��qSrQ)r�)r|r�)rrQrRr�^szBlock.where.<locals>.<listcomp>)r^)r�)rK)r�r�)rz)'Z#pandas.core.computation.expressions�coreZcomputationrr�rrr3r5r2r�rJrIr�r�rmr�rnr�rXr�r�r�r�r
r�r�rwZswapaxesrkr��range�boolr��takeZnonzerorr^rUr�)rYr�rr�rr�r�rIZ
orig_otherrr�r�r�r��
result_blocksr�Ztaken�r�nbrQ)rrrYrRr
sR



&
zBlock.wherecCsH|j|jj|d�\}}|jd�}|j|}||}t||d�g}||fS)a�
        Return a list of unstacked blocks of self

        Parameters
        ----------
        unstacker : reshape._Unstacker
        fill_value : int
            Only used in ExtensionBlock._unstack

        Returns
        -------
        blocks : list of Block
            New blocks of unstacked values.
        mask : array_like of bool
            The mask of columns of `blocks` we should keep.
        )rqr)rK)�get_new_valuesrIr�r�rw)rY�	unstackerrq�
new_placementr�r�r�rQrQrR�_unstackjs

zBlock._unstack�linearc
	Cs|jdkst�|j�}|j|dk}t|�}|r8|g}|rptjtj|jgt	|��t	|��j
t	|�t	|��}nFtjt|��}t
|tj|�d||j||j|d�}tj|dd�}|j}|r�tj|�r�|jd
dks�t|j��|d}tj|�}tj|�}	t|tjt	|��|	d	�S)a)
        compute the quantiles of the

        Parameters
        ----------
        qs: a scalar or list of the quantiles to be computed
        interpolation: type of interpolation, default 'linear'
        axis: axis to compute, default 0

        Returns
        -------
        Block
        r�r�d)r��na_valuer�rJ�
interpolationF)r�rz.)rKrJr�).r)rJr�rjrnr)rkr�r�rqrWrmrlr8rEr�r
r�Zitem_from_zerodimrw�arange)
rY�qsr r�rIZis_emptyZorig_scalarr�r�rJrQrQrR�quantile�s6


zBlock.quantilecCs@|j�r<|s&|j|�}|j|||d�S|j||||||d�S|S)a
        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.
        inplace : bool, default True
            Perform inplace modification.
        regex : bool, default False
            If true, perform regular expression substitution.
        convert : bool, default True
            If true, try to coerce any object types to better types.
        mask : array-like of bool, optional
            True indicate corresponding element is ignored.

        Returns
        -------
        A new block if there is anything to replace or the original block.
        )r�)r�r�r�r�)r�r�r�r�)rYr�r�r�r�r�r�rQrQrR�_replace_coerce�s 
zBlock._replace_coerce)N)N)N)NN)N)NFN)N)N)Fr�)TTTTF)rpN)T)FFT)FrF)
r�rNFNr�NNFN)r�rFNNFN)NrNr�NFN)rz)rN)r�Fr)rr)TFFN)Yr�
__module__�__qualname__�__doc__�	__slots__r�r$r&r�rc�
is_datetimetzrdr�r��is_categoricalrur�r]rV�classmethodrr�intrSrZrT�propertyr\r`rrbrerfrgr<rhrjrkr�rorqrU�setterrwryr{r�r�r�r�r�r�rnr^r�r�r�rr�r�r�r�r�r�rir�rr�r�r�r�r�r�r�r�r�rr�r�r�r
rrr	r9r
rr#r$rQrQrQrRrGWs�




	


';
$k	
Zu}A6."$, c<rGcs�eZdZdZdZdZdZdE�fdd�	Zedd��Z	d	d
�Z
eed�dd
�Z
dd�ZdFeeeedd�dd�Zdd�Zedd��Zedd��Zedd��Zeed�dd��Zed d!��Zd"d#�ZdGd$d%�Zed�d&d'�ZdHd)d*�Zddejfed+�d,d-�Ze ed.�d/d0�Z!d1d2�Z"dId3d4�Z#dJd6d7�Z$dKeeedd9��fd:d;�
Z%dLeee edd<�d=d>�Z&dMeeedd@�dAdB�Z'dCdD�Z(�Z)S)N�ExtensionBlocka
    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, CategoricalBlock.

    ExtensionArrays are limited to 1-D.
    FTNcst|j|�}t|tj�s tj|�}|dkr>t|�dkr:d}nd}t�j|||d�|jdkrpt|j�dkrpt	d��dS)z�
        Initialize a non-consolidatable block.

        'ndim' may be inferred from 'placement'.

        This will call continue to call __init__ for the other base
        classes mixed in with this Mixin.
        Nrzr�)rJzblock.size != values.size)
�_maybe_coerce_valuesrrrsrrW�superrZrJrUr�)rYrIrKrJ)�	__class__rQrRrZs	

zExtensionBlock.__init__cCs*|jdkrt|j�fSt|j�t|j�fS)Nrz)rJrWrIrU)rYrQrQrRrns
zExtensionBlock.shapecCs�|jdkrtt|t�rt|\}}tj|�r@|dkr@t|�d���n*t|t�rj|td�kr^t|��|j|gS|j|S|dkr�t|�d���|jSdS)Nr�rz only contains one item)	rJrrr��comZ
is_null_slice�
IndexErrorr��NotImplementedErrorrI)rY�colr�rQrQrRr�#s

zExtensionBlock.iget)r�rLcCst||j�S)z@
        Can we set the given array-like value inplace?
        )rrr\)rYr�rQrQrRr�4szExtensionBlock.should_storecCs|j�dgkst�||_dS)Nr)�tolistr�rI)rYr�rIrQrQrRr�:szExtensionBlock.setrrG)r�r�r�rLcCspt|d�}t|�}|r|jn|jj�}t|tjtf�rNt|�t|�krN||}t	||j
�}|||<|j|d�gS)z+
        See Block.putmask.__doc__
        r�)rI)rr�rIr�rrrkr�r<rW�
_safe_reshapernrw)rYr�r�r�r�r�r�rQrQrRr�>s
 zExtensionBlock.putmaskcCst|�S)aF
        Unbox to an extension array.

        This will unbox an ExtensionArray stored in an Index or Series.
        ExtensionArrays pass through. No dtype coercion is done.

        Parameters
        ----------
        values : Index, Series, ExtensionArray

        Returns
        -------
        ExtensionArray
        )rA)rYrIrQrQrRr0Rsz#ExtensionBlock._maybe_coerce_valuescCs
t|j�S)N)rxrI)rYrQrQrRr\cszExtensionBlock._holdercCs
|jjjS)N)rIr^r)rYrQrQrRrqhszExtensionBlock.fill_valuecCs|jjS)N)r\r�)rYrQrQrRr�mszExtensionBlock._can_hold_na)rLcCsdS)z,Extension arrays are never treated as views.FrQ)rYrQrQrRrbrszExtensionBlock.is_viewcCs
|jjjS)N)rIr^Z_is_numeric)rYrQrQrRr�wszExtensionBlock.is_numericcCs.t|t�r|d}t|||j�||j|<|S)a2
        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
            The subset of self.values to set
        value : object
            The value being set

        Returns
        -------
        Block

        Notes
        -----
        `indexer` is a direct slice/positional indexer. `value` must
        be a compatible shape.
        r)rrr�rBrI)rYr�r�rQrQrRr�{s


zExtensionBlock.setitemcCstj|j�j|j�S)N)rkrlrIrmrn)rYr^rQrQrRrj�szExtensionBlock.get_valuescCs|jS)N)rI)rYrQrQrRrh�szExtensionBlock.array_valuesrpcKs6|j}t|�}tj|jt��}|||<|jdt|��S)z8override to use ExtensionArray astype for the conversionrz)rIr8rkrlrirMrmrW)rYr�r�r�rIr�rQrQrRr��s
zExtensionBlock.to_native_types)r�cCsR|tjkrd}|jj||dd�}|jdko0|dks8t�|dkrF|j}|j||�S)zN
        Take values according to indexer and return them as a block.
        NT)rqrrz)r
rrIrrJr�rUry)rYr�r�rtrqr�rQrQrRr�s
zExtensionBlock.take_nd)r�rLcCsdS)NTrQ)rYr�rQrQrRr��sz ExtensionBlock._can_hold_elementcCs�t|t�r"|jdkr"|td�f}t|t�rzt|�dkrz|d}t|t�sTtd|��|j|}t|�rp|d}n
td|��|j|S)a
        Return a slice of my values.

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

        Returns
        -------
        np.ndarray or ExtensionArray
        r�Nrz+invalid slicing for a 1-ndim ExtensionArrayrz)rrr�rJr�rWr�rUrI)rYr��firstZnew_locsrQrQrRr��s


zExtensionBlock._slicecCs8|r
|jn|jj�}|j||d�}|j||j|jd�gS)N)r�r�)rIrKrJ)rIr�r�ryrUrJ)rYr�r�r�r�rIrQrQrRr��szExtensionBlock.fillnar�cKs0|r
|jn|jj�}|j|j|||d�|jd�S)N)r�r�r�)rIrK)rIr�ryr�rU)rYr�r�r�r�rqr�rIrQrQrRr��szExtensionBlock.interpolaterz)r�r�rLcs@|dkr&|dkr&t�jt|j�dd�S|dkr2d}t�j||�S)Nr)r�rz)r1r	rWrI)rYr�r�)r2rQrRr	s
zExtensionBlock.diff)r
r�rqrLcCs"|j|jj||d�|j|jd�gS)z�
        Shift the block by `periods`.

        Dispatches to underlying ExtensionArray and re-boxes in an
        ExtensionBlock.
        )r
rq)rKrJ)ryrIr9rUrJ)rYr
r�rqrQrQrRr9s
zExtensionBlock.shiftr�)rr�rLc
Cs<t|�}t|tttf�st�t|tj�rV|jdkrV|j	ddksFt�|dd�df}t|tj�r�|jdkr�|j	ddks~t�|dd�df}t
j|�r�t|�r�|j
j}t|j�r�d}n|j
}|jj�}|}t
j|�r�|}	n||}	y|	||<Wn6ttfk
�r(|jjtj||j|�|d�}YnX|j||jd�gS)Nr�rzr)r^)rK)r�rrr3r5r2r�rkr�rJrnr
r�r8r^rr.rIr�r5r�r\�_from_sequencer
ryrU)
rYr�rr�rr�r^r�ZicondZ	set_otherrQrQrRr
"s0


zExtensionBlock.wherec	sT�jd}tj|�}|j|dd�\}}|jd�}��fdd�t|j|�D�}||fS)Nrz)rqrcs,g|]$\}}�j�jj|d�d�|g��qS)T)rrq)ryrIr)r|�indicesr�)rqrYrQrRr�bsz+ExtensionBlock._unstack.<locals>.<listcomp>r�r�)rnrkr!rr��zipr�)	rYrrqrZn_rowsZ	dummy_arrr�r�r�rQ)rqrYrRrSs


zExtensionBlock._unstack)N)FrF)N)rpN)NFN)r�rFNN)rz)rN)r�Fr)*rr%r&r'r]rVrurZr-rnr�rrr�r�r,rr�r0r\rqr�rbr�r�rjr<rhr�r
rrrr�r�r�r�r	r9r
r�
__classcell__rQrQ)r2rRr/�sB
 


&


	/r/c@seZdZdZdd�ZdS)�ObjectValuesExtensionBlockz�
    Block providing backwards-compatibility for `.values`.

    Used by PeriodArray and IntervalArray to ensure that
    Series[T].values is an ndarray of objects.
    cCs|jjt�S)N)rIrirM)rYrQrQrRrfssz*ObjectValuesExtensionBlock.external_valuesN)rr%r&r'rfrQrQrQrRr>ksr>c@seZdZfZdZdZdS)�NumericBlockTN)rr%r&r(r�r�rQrQrQrRr?wsr?c@seZdZfZdS)�FloatOrComplexBlockN)rr%r&r(rQrQrQrRr@}sr@c@s.eZdZfZdZeed�dd�Zd
dd	�ZdS)�
FloatBlockT)r�rLcCsnt|�}|dk	r:t|jtjtjf�o8t|jtjtjf�St|t	t
tjtjf�olt|ttj
tttjtjf�S)N)rr�rxrk�floating�integer�
datetime64�timedelta64rr�floatr,�int_r�bool_rr)rYr�r�rQrQrRr��szFloatBlock._can_hold_element�N�.c
Ksp|j}|dkrH|dkrHt|�}|s.|jt�}ntj|dd�}|||<|Sddlm}||||||dd�}	|	j�S)	z$ convert to our native types format NrJrM)r^r)�FloatArrayFormatterF)r��float_format�decimalr�Zfixed_width)	rIr8rir{rkr�Zpandas.io.formats.formatrKZget_result_as_array)
rYr�rLrMr�r�rIr�rK�	formatterrQrQrRr��s"zFloatBlock.to_native_types)rINrJN)	rr%r&r(r$rrr�r�rQrQrQrRrA�srAc@s4eZdZfZdZeed�dd�Zeed�dd�Z	dS)	�ComplexBlockT)r�rLcCsRt|�}|dk	r(t|jtjtjtjf�St|tt	t
tjtjf�oPt|t
tjf�S)N)rr�rxrkrBrC�complexfloatingrrrFr,�complexZfloat_rGrrH)rYr�r�rQrQrRr��szComplexBlock._can_hold_element)r�rLcCst|jjtj�S)N)r�r^rxrkrP)rYr�rQrQrRr��szComplexBlock.should_storeN)
rr%r&r(r�rrr�rr�rQrQrQrRrO�srOc@s(eZdZfZdZdZeed�dd�ZdS)�IntBlockTF)r�rLcCsZt|�}|dk	rBt|jtj�o@t|jtjtjf�o@|jj|jkSt	|�pXt
|�oX|j	�S)N)rr�rxrkrCrDrEr^r�r&r$)rYr�r�rQrQrRr��szIntBlock._can_hold_elementN)	rr%r&r(r&r�rrr�rQrQrQrRrR�srRc@sTeZdZdZedd��Zedd��Zddd�Zd	d
�Zdd�Z	d
d�Z
ddd�ZdS)�DatetimeLikeBlockMixinzCMixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock.cCstS)N)r;)rYrQrQrRr\�szDatetimeLikeBlockMixin._holdercCstjdd�S)Nr�ns)rkrD)rYrQrQrRrq�sz!DatetimeLikeBlockMixin.fill_valueNcCs t|�r|j|j�jt�S|jS)zS
        return object dtype as boxed values, such as Timestamps/Timedelta
        )r*r\rIrirM)rYr^rQrQrRrj�sz!DatetimeLikeBlockMixin.get_valuescCs|j�S)N)rh)rYrQrQrRrg�sz&DatetimeLikeBlockMixin.internal_valuescCs|jj|j�S)N)r\rSrI)rYrQrQrRrh�sz#DatetimeLikeBlockMixin.array_valuescCs|j�j|j�|S)N)rhrmrn)rY�keyrQrQrRr��szDatetimeLikeBlockMixin.igetrcCs"|j�}|j|||d�}|j|�S)N)rqr�)rhr9ry)rYr
r�rqrIr�rQrQrRr9�szDatetimeLikeBlockMixin.shift)N)rN)rr%r&r'r-r\rqrjrgrhr�r9rQrQrQrRrS�s
	rScsreZdZfZdZd�fdd�	Zedd��Zdd�Zde	e
d��fdd
�
Zee	d�dd�Z
ddd�Zdd�Z�ZS)�
DatetimeBlockTNcs |j|�}t�j|||d�dS)N)rKrJ)r0r1rZ)rYrIrKrJ)r2rQrRrZs
zDatetimeBlock.__init__cCsdS)NTrQ)rYrQrQrRr�szDatetimeBlock._can_hold_nacCs@|jtkrtj|�}t|t�r$|j}t|tj�s<t	t
|���|S)ac
        Input validation for values passed to __init__. Ensure that
        we have datetime64ns, coercing if necessary.

        Parameters
        ----------
        values : array-like
            Must be convertible to datetime64

        Returns
        -------
        values : ndarray[datetime64ns]

        Overridden by DatetimeTZBlock.
        )r^rr
�ensure_datetime64nsrrr;�_datarkr�r�rx)rYrIrQrQrRr0s


z"DatetimeBlock._maybe_coerce_valuesFr�)r�r�csht|�}t|�rV|j}|r"|j�}t|dd�dkr@t|�jd�}|j|j�}|j	|�St
�j|||d�S)zr
        these automatically copy, so copy=True has no effect
        raise on an except if raise == True
        r�N�UTC)r^r�r�)r0r!rIr�r�r;Ztz_localizeZ
tz_convertr�rwr1ri)rYr^r�r�rI)r2rQrRri!s
zDatetimeBlock.astype)r�rLcCsxt|�}|dk	r6|jr.t||j�p,t||j�St|�S|tkrBdSt|t�rl|jrbt	|j
|jj�S|j
dkSt||j�S)NT)rr)r"r^r7r rrrrr�tzinfor�)rYr�r�rQrQrRr�6s


zDatetimeBlock._can_hold_elementrcKs(|j�}|jf||d�|��}tj|�S)z$ convert to our native types format )r��date_format)rh�_format_native_typesrk�
atleast_2d)rYr�r[r�Zdtar�rQrQrRr�JszDatetimeBlock.to_native_typescCstj|dd�}||j|<dS)z'
        See Block.set.__doc__
        F)r�N)r
rWrI)rYr�rIrQrQrRr�SszDatetimeBlock.set)N)Fr�)rN)rr%r&r(rcrZr-r�r0rr{rirr�r�r�r=rQrQ)r2rRrV�s
	rVcs�eZdZdZfZdZdZejZe	j
Z
e	jZej
dd�ZejZejZedd��Zdd�Zeed	�d
d��Zd d
d�Zdd�Zd!eeedd�dd�Zd"�fdd�	Z�fdd�Zd#dd�Zdd�Z�Z S)$�DatetimeTZBlockz2 implement a datetime64 block with a tz attribute TrrTcCstS)N)r;)rYrQrQrRr\jszDatetimeTZBlock._holdercCs,t||j�s|j|�}|jdkr(td��|S)a3
        Input validation for values passed to __init__. Ensure that
        we have datetime64TZ, coercing if necessary.

        Parameters
        ----------
        values : array-like
            Must be convertible to datetime64

        Returns
        -------
        values : DatetimeArray
        Nz,cannot create a DatetimeTZBlock without a tz)rrr\r�rX)rYrIrQrQrRr0ns


z$DatetimeTZBlock._maybe_coerce_values)rLcCs|jjjdk	S)z* return a boolean if I am possibly a view N)rIrXra)rYrQrQrRrb�szDatetimeTZBlock.is_viewNcCs*|j}t|�r|jt�}tj|�j|j�S)a9
        Returns an ndarray of values.

        Parameters
        ----------
        dtype : np.dtype
            Only `object`-like dtypes are respected here (not sure
            why).

        Returns
        -------
        values : ndarray
            When ``dtype=object``, then and object-dtype ndarray of
            boxed values is returned. Otherwise, an M8[ns] ndarray
            is returned.

            DatetimeArray is always 1-d. ``get_values`` will reshape
            the return value to be the same dimensionality as the
            block.
        )rIr*rirMrkrlrmrn)rYr^rIrQrQrRrj�s
zDatetimeTZBlock.get_valuescCstj|jjddd��S)Nzdatetime64[ns]F)r�)rkrlrIri)rYrQrQrRrf�szDatetimeTZBlock.external_valuesrrG)r�r�rLcCsr|dkrt�|dkr*tj|jjtjd�}n|j|j||d�djj}|jdt	|��}|j
d�}t||jj
d�gS)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 TimeDeltaBlock.

        Notes
        -----
        The arguments here are mimicking shift so they are called correctly
        by apply.
        r)r^)r�rzztimedelta64[ns])rK)r5rk�zerosrIrnZint64r9Zasi8rmrWri�TimeDeltaBlockrUr�)rYr�r�r�rQrQrRr	�s
zDatetimeTZBlock.diffFcs4|j|�rt�j||||�S|jt�j||||d�S)N)r�r�r�)r�r1r�rirM)rYr�r�r�r�)r2rQrRr��s

zDatetimeTZBlock.fillnacsZ|j|�s t|tj�r.|jdkr.t�j||�S|jjt	�}t
||jt|j
d�}|j||�S)Nr)rK�klassrJ)r�rrrkr�r�r1r�rIrirMrwrU�ObjectBlockrJ)rYr�r�Zobj_valsr�)r2rQrRr��s
zDatetimeTZBlock.setitemrcCsX|jjd�}|j|j�}|j|�}|j|||d�}|j|jj�|jd�}|j	||j
d�S)NzM8[ns])r r�)r^)rJ)rI�viewrmrnrwr#r\r�r^ryrJ)rYr"r r�ZnaiveZblkZres_blkZawarerQrQrRr#�s
zDatetimeTZBlock.quantilecCs4|dkr|j}|j|kr0td|j�d|�d���|S)a�
        ndim inference and validation.

        This is overriden by the DatetimeTZBlock to check the case of 2D
        data (values.ndim == 2), which should only be allowed if ndim is
        also 2.
        The case of 1D array is still allowed with both ndim of 1 or 2, as
        if the case for other EAs. Therefore, we are only checking
        `values.ndim > ndim` instead of `values.ndim != ndim` as for
        consolidated blocks.
        Nz1Wrong number of dimensions. values.ndim != ndim [z != r[)rJrX)rYrIrJrQrQrRrT�s
zDatetimeTZBlock._check_ndim)N)r)NFN)rr)!rr%r&r'r(r)rurGrgrVr�r�rkrDrqr�r/rhr-r\r0rrbrjrfr,rr	r�r�r#rTr=rQrQ)r2rRr^\s*
%

r^csleZdZfZdZdZdZejdd�Z	d�fdd�	Z
edd	��Ze
ed
�dd�Z�fd
d�Zddd�Z�ZS)r`TFrrTNcsT|jtkrtj|�j}t|t�r&|j}t|tj�s>tt	|���t
�j|||d�dS)N)rKrJ)r^rr?r:rXrrrkr�r�rxr1rZ)rYrIrKrJ)r2rQrRrZ	s

zTimeDeltaBlock.__init__cCstS)N)r?)rYrQrQrRr\!	szTimeDeltaBlock._holder)r�rLcCsJt|�}|dk	rt|jtj�S|tkr*dSt|ttjf�r>dSt||j	�S)NT)
rr�rxrkrErrrrr7r^)rYr�r�rQrQrRr�%	sz TimeDeltaBlock._can_hold_elementcs t|�rtd��t�j|f|�S)Nz�Passing integers to fillna for timedelta64[ns] dtype is no longer supported.  To obtain the old behavior, pass `pd.Timedelta(seconds=n)` instead.)r&r�r1r�)rYr�r�)r2rQrRr�/	szTimeDeltaBlock.fillnacKs|j�}|j|f|�S)z$ convert to our native types format )rhr\)rYr�r�ZtdarQrQrRr�<	szTimeDeltaBlock.to_native_types)N)r)rr%r&r(rdr�r�rkrErqrZr-r\rrr�r�r�r=rQrQ)r2rRr`	s	

r`cs:eZdZfZdZdZeed�dd�Zd�fdd�	Z	�Z
S)	�	BoolBlockTF)r�rLcCs.t|�}|dk	rt|jtj�St|ttjf�S)N)rr�rxrkrHrrr)rYr�r�rQrQrRr�G	szBoolBlock._can_hold_elementcs:t|d�}tj|�}tj|t�s$|St�j|||||d�S)Nr�)r�r�r�)rrkZ
atleast_1dZcan_castrr1r�)rYr�r�r�r�r�Zto_replace_values)r2rQrRr�M	s

zBoolBlock.replace)FFT)rr%r&r(r�r�rrr�r�r=rQrQ)r2rRrdB	s
rdcs�eZdZfZdZdZd�fdd�	Zedd��Zde	e	e	e	e	d	�d
d�Z
dededd
�dd�Ze
e	d�dd�Zd�fdd�	Zd�fdd�	Zd�fdd�	Z�ZS)rbTNr�cs2t|jjt�rtj|td�}t�j|||d�dS)N)r^)rJrK)	r�r^rxr{rkr�rMr1rZ)rYrIrKrJ)r2rQrRrZ\	szObjectBlock.__init__cCstj|jjd��S)z]
        we can be a bool if we have only bool values but are of type
        object
        r)r
Z
is_bool_arrayrIr�)rYrQrQrRr�b	szObjectBlock.is_boolF)r�rr�rr�c	sX�����fdd�}|jdkr.|jd|d�}n&|d|jj�d�}t||j|jd�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 an ObjectBlock!!!!!

        can return multiple blocks!
        cs8|j}t|j������d�}t|tj�r4|j|�}|S)N)rr�rr�r�)rnrr�rrrkr�rm)r�r�r�rnrI)r�r�rr�rrQrRr�y	s
zObjectBlock.convert.<locals>.fr�NF)rJrK)rJr�rIr�rwrU)	rYr�rr�rr�r�r�rIrQ)r�r�rr�rrRr�j	s
zObjectBlock.convertrG)r�rLcCs|dk	r|Stdd�|D��S)NcSsg|]}|jddd��qS)TF)rr�)r�)r|r�rQrQrRr��	sz/ObjectBlock._maybe_downcast.<locals>.<listcomp>)r�)rYr�r�rQrQrRr��	szObjectBlock._maybe_downcast)r�rLcCsdS)NTrQ)rYr�rQrQrRr��	szObjectBlock._can_hold_elementc	s"t|�}t|�}|o|}|p|}	g}
|g}|	rLt|�rL|j|||d|d�S|	pR|sjt�j|||||d�S|r�xJt||�D]<\}}
g}
x*|D]"}|j||
|||d�}t||
�}
q�W|
}qzW|
S|o�|�rx@|D]8}g}
x*|D]"}|j|||||d�}t||
�}
q�W|
}q�W|
S|j|||||d�S)NT)r�r�r�)r�r�r�)r)r,r�r1r�r<r�)rYr�r�r�r�r�Zto_rep_is_listZ
value_is_listZ
both_listsZeither_listrr�Zto_repr�r�r�)r2rQrRr��	s@



zObjectBlock.replacecst|d�}|ot|�}t|�}|r.|r.td��|r6|}|p<|}t|�rN|j}	n|}	|rf|	rftj|��nt�j|�||d�S|r�|j	n|j	j
�}
t��s�t�t
�r���fdd�}n��fdd�}tj||jgd�}|dkr�||
�|
dd�<n||
|�|
|<|j|
�}
|�r|
jd	d
�}
|
S)a�
        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.
        regex : bool, default False
            If true, perform regular expression substitution.
        convert : bool, default True
            If true, try to coerce any object types to better types.
        mask : array-like of bool, optional
            True indicate corresponding element is ignored.

        Returns
        -------
        a new block, the result after replacing
        r�z8only one of to_replace and regex can be regex compilable)r�r�cs0t��r(t|t�r(�j|�dk	r$�S|S|SdS)N)r,rrr{�search)r})�rxr�rQrR�re_replacer

sz0ObjectBlock._replace_single.<locals>.re_replacercs&t��rt|t�r�j�|�S|SdS)N)r,rrr{�sub)r})rfr�rQrRrg
s)ZotypesNF)r�)rr-r�r,�pattern�re�compiler1r�rIr�r8rrr{rkZ	vectorizer^rwr�)rYr�r�r�r�r�r�Z	to_rep_reZregex_rerir�rgr�r�)r2)rfr�rRr��	s6
	
zObjectBlock._replace_singlecsN|j�r6t�j||||||d�}|r2dd�|D�}|S|rJ|jddd�gS|S)a
        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.
        inplace : bool, default False
            Perform inplace modification.
        regex : bool, default False
            If true, perform regular expression substitution.
        convert : bool, default True
            If true, try to coerce any object types to better types.
        mask : array-like of bool, optional
            True indicate corresponding element is ignored.

        Returns
        -------
        A new block if there is anything to replace or the original block.
        )r�r�r�r�r�r�cSsg|]}|jddd��qS)FT)r�r�)r�)r|r�rQrQrRr�J
sz/ObjectBlock._replace_coerce.<locals>.<listcomp>FT)r�r�)r�r1r$r�)rYr�r�r�r�r�r�r�)r2rQrRr$&
szObjectBlock._replace_coerce)Nr�)TTTTF)N)FFT)FFTN)TFFN)rr%r&r(r�r�rZr-r�rr�rr�rr�r�r�r$r=rQrQ)r2rRrbW	s
!,_rbcsPeZdZfZdZdZejZd�fdd�	Ze	dd��Z
deeed�d	d
�Z�Z
S)
�CategoricalBlockTNcs4t|�}t|t�stt|���t�j|||d�dS)N)rKrJ)rArrr:r�rxr1rZ)rYrIrKrJ)r2rQrRrZX
szCategoricalBlock.__init__cCstS)N)r:)rYrQrQrRr\^
szCategoricalBlock._holderF)r�r�r�cCs0t|d�}|r|n|j�}|jj||dd�|S)Nr�T)r�)rr�rIr�)rYr�r�r�r�r�r�rQrQrRr�b
s
zCategoricalBlock.replace)N)FFT)rr%r&r(r*r�rGr�rZr-r\rr�r=rQrQ)r2rRrlQ
srlcCs�|p|j}|j}t|�rt}n�t|j�r.t}n�t|tj�rPt	|j�sJt
�t}n�t	|j�r`t}n�t
|�spt|�rvt}n|t|j�r�t}nlt|tj�r�t}nZt|tj�r�t|tj�s�t
�t}n8t|tj�r�t}n&t|tj�r�t}n|tjkr�t}nt}|S)z�
    Find the appropriate Block subclass to use for the given values and dtype.

    Parameters
    ----------
    values : ndarray-like
    dtype : numpy or pandas dtype

    Returns
    -------
    cls : class, subclass of Block
    )r^rxr.r/rrlr�rkrDr!r�rVr^r(r+r>r#rBrArErCr`rPrOrRrHrdrb)rIr^ZvtyperOrQrQrR�get_block_typeu
s8




rmcCs�t|t�r(|j�}|r(|dkr(tj|�}t|t�r8|j}|dkrV|pH|j}t||�}n"|t	krxt
|j�rxtj||d�}||||d�S)Nrz)r^)rJrK)
rrr4Zto_numpyrkr]r>Znumpy_dtyper^rmr^r!r;rS)rIrKrarJr^rQrQrRrw�
s



rwcCsj|dkrg}t|t�rFxN|D]$}t|t�r6|j|�q|j|�qWn t|t�s\tt|���|j|�|S)z0 return a new extended blocks, given the result N)rrr��extendr�rGr�rx)r�r�rrQrQrRr��
s



r�rz)rIrJrLcCs0|j|kr,|j}t|j�s,|jtd|��}|S)z6 guarantee the shape of the values to be at least 1 d rz)rz)rJrnr#r^rmr�)rIrJrnrQrQrRrv�
s


rvcCs.t|t�r|j}t|j�s*tj|�j|�}|S)a�
    If possible, reshape `arr` to have shape `new_shape`,
    with a couple of exceptions (see gh-13012):

    1) If `arr` is a ExtensionArray or Index, `arr` will be
       returned as is.
    2) If `arr` is a Series, the `_values` attribute will
       be reshaped and returned.

    Parameters
    ----------
    arr : array-like, object to be reshaped
    new_shape : int or tuple of ints, the new shape
    )rrr5Z_valuesr#r^rkrlrm)Zarrr�rQrQrRr8�
s


r8)r�r�rLc
s(t|�stj|t���}y|�}Wntk
r8Yn�Xt||d�sJn�t|j�p\t|j�s`ntt|j�prt|j�svn^t	j
dd�� t	jdtj�|j
|j�}WdQRX||k}t|�r�|j�r�|j�}||�<|Stj|�}�fdd�}|jj|jjk�r|||�St|j�\}}	|j
|�}|||�S)a�
    Return a new ndarray, try to preserve dtype if possible.

    Parameters
    ----------
    v : np.ndarray
        `values`, updated in-place.
    mask : np.ndarray[bool]
        Applies to both sides (array like).
    n : `new values` either scalar or an array like aligned with `values`

    Returns
    -------
    values : ndarray with updated values
        this *may* be a copy of the original

    See Also
    --------
    ndarray.putmask
    rT)�recordr�Ncs6y|�|�<Wn ttfk
r0||�<YnX|S)N)r4rX)r�r�)r�rQrR�_putmask_preserve/s
z)_putmask_smart.<locals>._putmask_preserve)r)rkr�rWr�r6r%r^r'�warnings�catch_warnings�simplefilterZComplexWarningrir�r�rl�kindr)
r�r�r��nnZnn_at�compr�rpr^r�rQ)r�rRr��
s6


r�)r�rLcCsJt|t�rtj|tjd�}t|tj�s2tt|���|jt	ksFt|j��|S)zP
    If we have a SparseArray or BooleanArray, convert it to ndarray[bool].
    )r^)
rrr<rkrlrHr�r�rxr^r)r�rQrQrRr�Bs

r�)N)NNN)N)rz)�rrr�rjZtypingrrrrrqZnumpyrkZpandas._libsrr	r�r
rZpandas._libs.internalsZ_libsZ	internalsrsrZpandas._libs.tslibsr
Zpandas._libs.tslibs.timezonesrZpandas._typingrZpandas.util._validatorsrZpandas.core.dtypes.castrrrrrrrrrrrZpandas.core.dtypes.commonrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0Zpandas.core.dtypes.dtypesr1Zpandas.core.dtypes.genericr2r3r4r5Zpandas.core.dtypes.missingr6r7r8Zpandas.core.algorithmsrZ
algorithmsZ"pandas.core.array_algos.transformsr9Zpandas.core.arraysr:r;r<r=r>r?Zpandas.core.baser@Zpandas.core.common�commonr3Zpandas.core.constructionrAZpandas.core.indexersrBrCrDZpandas.core.missingr�Zpandas.core.nanopsrEZpandasrFrGr/r>r?r@rArOrRrSrVr^r`rdrbrlrmrwr�r,rvr8r�r�r�rQrQrQrR�<module>s�4
\ $}2'`61{$
.

P