U
    MZff.                     @  s   d dl mZ d dlZd dlmZmZ d dlZd dlm	Z	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 d d
lmZ d dlmZmZ erd dlZd dlmZ eG dd deZ ddddddZ!G dd deZ"dS )    )annotationsN)TYPE_CHECKINGcast)libmissing)DtypeDtypeObjtype_t)is_list_likeis_numeric_dtype)register_extension_dtype)isna)ops)masked_accumulations)BaseMaskedArrayBaseMaskedDtype)nptc                   @  s   e Zd ZdZdZeddddZedddd	Zed
dddZe	ddddZ
ddddZeddddZeddddZdddddZdS )BooleanDtypeaI  
    Extension dtype for boolean data.

    .. warning::

       BooleanDtype is considered experimental. The implementation and
       parts of the API may change without warning.

    Attributes
    ----------
    None

    Methods
    -------
    None

    Examples
    --------
    >>> pd.BooleanDtype()
    BooleanDtype
    booleantypereturnc                 C  s   t jS N)npbool_self r   >/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/arrays/boolean.pyr   E   s    zBooleanDtype.typestrc                 C  s   dS )Nbr   r   r   r   r   kindI   s    zBooleanDtype.kindznp.dtypec                 C  s
   t dS )Nbool)r   dtyper   r   r   r   numpy_dtypeM   s    zBooleanDtype.numpy_dtypeztype_t[BooleanArray]c                 C  s   t S )zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )BooleanArray)clsr   r   r   construct_array_typeQ   s    	z!BooleanDtype.construct_array_typec                 C  s   dS )Nr   r   r   r   r   r   __repr__\   s    zBooleanDtype.__repr__r"   c                 C  s   dS NTr   r   r   r   r   _is_boolean_   s    zBooleanDtype._is_booleanc                 C  s   dS r)   r   r   r   r   r   _is_numericc   s    zBooleanDtype._is_numericz$pyarrow.Array | pyarrow.ChunkedArrayr%   )arrayr   c           
      C  s  ddl }|j| kr(td|j dt||jr<|g}n|j}g }|D ]}| }|jj	|jt
|d|d g|jdjdd}|jdkr|jj	|jt
|d|d g|jdjdd}| }ntjt
|td	}t||}	||	 qJ|sttjg tjd	tjg tjd	S t|S dS )
zI
        Construct BooleanArray from pyarrow Array/ChunkedArray.
        r   Nz$Expected array of boolean type, got z instead   )offsetF)Zzero_copy_onlyr#   )pyarrowr   r   	TypeError
isinstanceArraychunksbuffersr%   Zfrom_bufferslenr.   Zto_numpyZ
null_countr   zerosr"   appendr,   Z_concat_same_type)
r   r,   r0   r4   resultsZarrZbuflistdatamaskZbool_arrr   r   r   __from_arrow__g   sH      
 
  
 
 zBooleanDtype.__from_arrow__N)__name__
__module____qualname____doc__namepropertyr   r!   r$   classmethodr'   r(   r*   r+   r<   r   r   r   r   r   )   s    
r   Fr"   tuple[np.ndarray, np.ndarray])copyr   c                 C  s@  t | trD|dk	rtd| j| j } }|r<|  } | }| |fS d}t | tjrp| jtj	krp|rl|  } nt | tjrt
| jrt| }tjt| td}| |  t|| < t||  | j| |  kstd|} ntj| td}tj|dd}d}|d| krtdtd	t|}tjt| td} ||  t| | < ||krt| |  t||  tkstd|dkr|dkrtj| jtd}np|dkr|}n`t |tjr|jtj	kr|dk	r||B }n|r"| }n tj|td}|dk	r"||B }| j|jkr8td
| |fS )a  
    Coerce the input values array to numpy arrays with a mask.

    Parameters
    ----------
    values : 1D list-like
    mask : bool 1D array, optional
    copy : bool, default False
        if True, copy the input

    Returns
    -------
    tuple of (values, mask)
    Nz'cannot pass mask for BooleanArray inputr/   zNeed to pass bool-like valuesTskipna)Zfloatingintegerzmixed-integer-float)r   emptyznpt.NDArray[np.bool_]z&values.shape and mask.shape must match)r2   r%   
ValueError_data_maskrE   r   ndarrayr#   r   r   r   r7   r6   r"   astypeallr1   asarrayobjectr   Zinfer_dtyper   floatshaper,   )valuesr;   rE   Zmask_valuesZvalues_boolZvalues_objectZinferred_dtypeZinteger_liker   r   r   coerce_to_array   sh    






rU   c                	      s   e Zd ZdZdZdZdZdddddhZd	d
dddhZd.ddddd fddZ	e
ddddZeddddddddddd dddZejejeejfZedd d!dd"d#d$d%Zd&d' Zdd(d)dd*d+d,d-Z  ZS )/r%   a=  
    Array of boolean (True/False) data with missing values.

    This is a pandas Extension array for boolean data, under the hood
    represented by 2 numpy arrays: a boolean array with the data and
    a boolean array with the mask (True indicating missing).

    BooleanArray implements Kleene logic (sometimes called three-value
    logic) for logical operations. See :ref:`boolean.kleene` for more.

    To construct an BooleanArray from generic array-like input, use
    :func:`pandas.array` specifying ``dtype="boolean"`` (see examples
    below).

    .. warning::

       BooleanArray is considered experimental. The implementation and
       parts of the API may change without warning.

    Parameters
    ----------
    values : numpy.ndarray
        A 1-d boolean-dtype array with the data.
    mask : numpy.ndarray
        A 1-d boolean-dtype array indicating missing values (True
        indicates missing).
    copy : bool, default False
        Whether to copy the `values` and `mask` arrays.

    Attributes
    ----------
    None

    Methods
    -------
    None

    Returns
    -------
    BooleanArray

    Examples
    --------
    Create an BooleanArray with :func:`pandas.array`:

    >>> pd.array([True, False, None], dtype="boolean")
    <BooleanArray>
    [True, False, <NA>]
    Length: 3, dtype: boolean
    FTTrueTRUEtrue1z1.0FalseFALSEfalse0z0.0z
np.ndarrayr"   None)rT   r;   rE   r   c                   s>   t |tjr|jtjks tdt | _t j	|||d d S )NzIvalues should be boolean numpy array. Use the 'pd.array' function insteadrE   )
r2   r   rM   r#   r   r1   r   _dtypesuper__init__)r   rT   r;   rE   	__class__r   r   rb   &  s    zBooleanArray.__init__r   r   c                 C  s   | j S r   )r`   r   r   r   r   r#   1  s    zBooleanArray.dtypeN)r#   rE   true_valuesfalse_valuesz	list[str]zDtype | Nonezlist[str] | None)stringsr#   rE   re   rf   r   c          	        st   | j |pg | j|pg  dd fdd}tj|td}t|}tt|||  || < | j	|||dS )Nr"   r   c                   s*   | krdS |  krdS t |  dd S )NTFz cannot be cast to bool)rJ   )sZfalse_values_unionZtrue_values_unionr   r   
map_stringB  s
    z:BooleanArray._from_sequence_of_strings.<locals>.map_stringr/   )r#   rE   )
_TRUE_VALUESunion_FALSE_VALUESr   r,   rQ   r   listmapZ_from_sequence)	r&   rg   r#   rE   re   rf   rj   Zscalarsr;   r   ri   r   _from_sequence_of_strings5  s    
z&BooleanArray._from_sequence_of_stringsr_   r   rD   )r#   rE   r   c                C  s   |r|dkst t||dS )Nr   r_   )AssertionErrorrU   )r&   valuer#   rE   r   r   r   _coerce_to_arrayQ  s    zBooleanArray._coerce_to_arrayc                 C  s<  |j dkstt|}d }t|tr6|j|j }}nNt|rpt	j
|dd}|jdkr^tdt|dd\}}nt|t	jr| }|r|tjk	rt|stdt|j  d	|st| t|krtd
|j dkrt| j|| j|\}}n>|j dkrt| j|| j|\}}nt| j|| j|\}}| ||S )N>   and_rand_or_rxorror_xorr"   r/   r-   z(can only perform ops with 1-d structuresFr_   z+'other' should be pandas.NA or a bool. Got z	 instead.zLengths must match>   rx   rv   >   rt   ru   )r=   rq   r   Z	is_scalarr2   r%   rK   rL   r
   r   rP   ndimNotImplementedErrorrU   r   item
libmissingZNAZis_boolr1   r   r6   rJ   r   Z	kleene_orZ
kleene_andZ
kleene_xorZ_maybe_mask_result)r   otheropZother_is_scalarr;   resultr   r   r   _logical_methodY  s0    



zBooleanArray._logical_methodrF   r   r   )rA   rG   r   c                K  s|   | j }| j}|dkrJtt|}|||fd|i|\}}t| ||ddS ddlm} ||t|j	|fd|i|S d S )N)ZcumminZcummaxrG   Fr_   r   )IntegerArray)
rK   rL   getattrr   r   Zpandas.core.arraysr   rN   int_accumulate)r   rA   rG   kwargsr:   r;   r   r   r   r   r   r   |  s    
zBooleanArray._accumulate)F)r=   r>   r?   r@   Z_internal_fill_valueZ_truthy_valueZ_falsey_valuerk   rm   rb   rB   r#   rC   rp   r   rM   numbersNumberr"   r   Z_HANDLED_TYPESrs   r   r   __classcell__r   r   rc   r   r%      s.   4 $r%   )NF)#
__future__r   r   typingr   r   Znumpyr   Zpandas._libsr   r   r}   Zpandas._typingr   r   r	   Zpandas.core.dtypes.commonr
   r   Zpandas.core.dtypes.dtypesr   Zpandas.core.dtypes.missingr   Zpandas.corer   Zpandas.core.array_algosr   Zpandas.core.arrays.maskedr   r   r0   r   r   rU   r%   r   r   r   r   <module>   s(   h   W