U
    c1+                     @  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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)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 )BooleanDtypeaf  
    Extension dtype for boolean data.

    .. versionadded:: 1.0.0

    .. 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-g7fro6k3/pandas/core/arrays/boolean.pyr   F   s    zBooleanDtype.typestrc                 C  s   dS )Nbr   r   r   r   r   kindJ   s    zBooleanDtype.kindznp.dtypec                 C  s
   t dS )Nbool)r   dtyper   r   r   r   numpy_dtypeN   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_typeR   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_numericd   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+   r/   r3   resultsZarrZbuflistdatamaskZbool_arrr   r   r   __from_arrow__h   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)Zskipna)Zfloatingintegerzmixed-integer-float)r   emptyznpt.NDArray[np.bool_]z&values.shape and mask.shape must match)r1   r$   
ValueError_data_maskrD   r   ndarrayr"   r   r   r   r6   r5   r!   Zastypeallr0   asarrayobjectr   Zinfer_dtyper   floatshaper+   )valuesr:   rD   Zmask_valuesZvalues_boolZvalues_objectZinferred_dtypeZinteger_liker   r   r   coerce_to_array   sh    






rQ   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  ZS ))r$   aZ  
    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).

    .. versionadded:: 1.0.0

    .. 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)rP   r:   rD   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rD   )
r1   r   rJ   r"   r   r0   r   _dtypesuper__init__)r   rP   r:   rD   	__class__r   r   r^   '  s    zBooleanArray.__init__r   r   c                 C  s   | j S r   )r\   r   r   r   r   r"   2  s    zBooleanArray.dtypeN)r"   rD   true_valuesfalse_valuesz	list[str]zDtype | Nonezlist[str] | None)stringsr"   rD   ra   rb   r   c                  sP   | j |pg | j|pg   fddfdd|D }| j|||dS )Nc                   s6   t | r| S | krdS |  kr$dS t|  dd S )NTFz cannot be cast to bool)r   rG   )s)false_values_uniontrue_values_unionr   r   
map_stringC  s    z:BooleanArray._from_sequence_of_strings.<locals>.map_stringc                   s   g | ]} |qS r   r   ).0x)rg   r   r   
<listcomp>M  s     z:BooleanArray._from_sequence_of_strings.<locals>.<listcomp>)r"   rD   )_TRUE_VALUESunion_FALSE_VALUESZ_from_sequence)r%   rc   r"   rD   ra   rb   Zscalarsr   )re   rg   rf   r   _from_sequence_of_strings6  s
    

z&BooleanArray._from_sequence_of_stringsr[   r   rC   )r"   rD   r   c                C  s   |r|dkst t||dS )Nr   r[   )AssertionErrorrQ   )r%   valuer"   rD   r   r   r   _coerce_to_arrayR  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>   rand_ror_rxorxorand_or_r!   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>   rs   rw   >   rv   rr   )r<   ro   r   Z	is_scalarr1   r$   rH   rI   r
   r   rL   ndimNotImplementedErrorrQ   r   item
libmissingZNAZis_boolr0   r   r5   rG   r   Z	kleene_orZ
kleene_andZ
kleene_xorZ_maybe_mask_result)r   otheropZother_is_scalarr:   resultr   r   r   _logical_methodZ  s0    



zBooleanArray._logical_method)F)r<   r=   r>   r?   Z_internal_fill_valueZ_truthy_valueZ_falsey_valuerk   rm   r^   rA   r"   rB   rn   r   rJ   numbersNumberr!   r   Z_HANDLED_TYPESrq   r   __classcell__r   r   r_   r   r$      s*   6 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.arrays.maskedr   r   r/   r   r   rQ   r$   r   r   r   r   <module>   s&   j   W