a
    Of0                     @  s   d dl mZ d dlZd dlmZmZmZ d dl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 d d
lmZmZ erd dlZd dlmZmZmZmZ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ClassVarcast)libmissing)is_list_like)register_extension_dtype)isna)ops)masked_accumulations)BaseMaskedArrayBaseMaskedDtype)DtypeDtypeObjSelfnpttype_tc                   @  s   e Zd ZU dZdZded< 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
    booleanzClassVar[str]nametypereturnc                 C  s   t jS N)npbool_self r   S/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/core/arrays/boolean.pyr   D   s    zBooleanDtype.typestrc                 C  s   dS )Nbr   r   r   r   r    kindH   s    zBooleanDtype.kindznp.dtypec                 C  s
   t dS )Nbool)r   dtyper   r   r   r    numpy_dtypeL   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_typeP   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_numericb   s    zBooleanDtype._is_numericz$pyarrow.Array | pyarrow.ChunkedArrayr'   )arrayr   c                 C  sr  ddl }|j| kr6|j|js6td|j dt||jrR|g}t|}n|j	}|
 }|j|jrtj|td}tj|td}t||S 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||}
||
 q|sdt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r%      )offsetF)Zzero_copy_only)pyarrowr   r   typesZis_null	TypeError
isinstanceZArraylenchunkslengthr   Zonesr$   emptyr'   buffersZfrom_buffersr1   Zto_numpyZ
null_countzerosappendr.   Z_concat_same_type)r   r.   r2   r7   r8   maskdataresultsZarrZbuflistZbool_arrr   r   r    __from_arrow__f   sF    


zBooleanDtype.__from_arrow__N)__name__
__module____qualname____doc__r   __annotations__propertyr   r#   r&   classmethodr)   r*   r,   r-   r@   r   r   r   r    r   (   s    

r   Fr$   tuple[np.ndarray, np.ndarray])copyr   c                 C  sB  t | trD|durtd| j| j } }|r<|  } | }| |fS d}t | tjrp| jtj	krp|rl|  } nt | tjr| jj
dv rt| }tjt| td}| |  t|| < t||  | j| |  kstd|} ntj| td}tj|dd}d}|d	| vrtdtd
t|}tjt| td} ||  t| | < ||v rt| |  t||  tkstd|du r|du rtj| jtd}np|du r|}n`t |tjr|jtj	kr|dur||B }n|r$| }n tj|td}|dur$||B }| j|jkr:t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 inputZiufcbr/   zNeed to pass bool-like valuesTskipna)Zfloatingintegerzmixed-integer-float)r   r9   npt.NDArray[np.bool_]z&values.shape and mask.shape must match)r5   r'   
ValueError_data_maskrI   r   ndarrayr%   r   r#   r
   r;   r6   r$   astypeallr4   asarrayobjectr   Zinfer_dtyper   floatshaper.   )valuesr=   rI   Zmask_valuesZvalues_boolZvalues_objectZinferred_dtypeZinteger_liker   r   r    coerce_to_array   sh    






rY   c                	      s   e Zd ZdZdZdZdZh dZh dZe	dddd	 fd
d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>   1TRUEz1.0Truetrue>   falsez0.0FALSE0Falsez
np.ndarrayrM   r   )rX   r=   r   c                   s   t  ||}t |_|S r   )super_simple_newr   _dtype)r(   rX   r=   result	__class__r   r    rc   -  s    zBooleanArray._simple_newr$   None)rX   r=   rI   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rI   )
r5   r   rQ   r%   r   r4   r   rd   rb   __init__)r   rX   r=   rI   rf   r   r    rj   3  s    zBooleanArray.__init__r   r   c                 C  s   | j S r   )rd   r   r   r   r    r%   >  s    zBooleanArray.dtypeN)r%   rI   true_valuesfalse_valuesz	list[str]zDtype | Nonezlist[str] | None)stringsr%   rI   rk   rl   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*   | v rdS |  v rdS t |  dd S )NTFz cannot be cast to bool)rN   )sZfalse_values_unionZtrue_values_unionr   r    
map_stringO  s
    z:BooleanArray._from_sequence_of_strings.<locals>.map_stringr/   )r%   rI   )
_TRUE_VALUESunion_FALSE_VALUESr   r.   rU   r
   listmapZ_from_sequence)	r(   rm   r%   rI   rk   rl   rp   Zscalarsr=   r   ro   r    _from_sequence_of_stringsB  s    
z&BooleanArray._from_sequence_of_stringsri   r   rH   )r%   rI   r   c                C  s   |r|dksJ t ||dS )Nr   ri   )rY   )r(   valuer%   rI   r   r   r    _coerce_to_array^  s    zBooleanArray._coerce_to_arrayc                 C  s<  |j dv sJ 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urt|stdt|j  d	|st| t|krtd
|j dv rt| j|| j|\}}n>|j dv rt| j|| j|\}}nt| j|| j|\}}| ||S )N>   Zrxorrand_xoror_and_ror_r$   r/   r0   z(can only perform ops with 1-d structuresFri   z+'other' should be pandas.NA or a bool. Got z	 instead.zLengths must match>   r}   r{   >   r|   ry   )rA   r   Z	is_scalarr5   r'   rO   rP   r   r   rT   ndimNotImplementedErrorrY   r   item
libmissingZNAZis_boolr4   r   r6   rN   r   Z	kleene_orZ
kleene_andZ
kleene_xorZ_maybe_mask_result)r   otheropZother_is_scalarr=   re   r   r   r    _logical_methodf  s4    



zBooleanArray._logical_methodrJ   r!   r   )r   rK   r   c                K  sv   | j }| j}|dv rDtt|}|||fd|i|\}}| ||S ddlm} ||t|j	|fd|i|S d S )N)ZcumminZcummaxrK   r   )IntegerArray)
rO   rP   getattrr   rc   Zpandas.core.arraysr   rR   int_accumulate)r   r   rK   kwargsr>   r=   r   r   r   r   r    r     s    
zBooleanArray._accumulate)F)rA   rB   rC   rD   Z_internal_fill_valueZ_truthy_valueZ_falsey_valuerq   rs   rG   rc   rj   rF   r%   rv   r   rQ   numbersNumberr$   r   Z_HANDLED_TYPESrx   r   r   __classcell__r   r   rf   r    r'      s2   4 $r'   )NF)$
__future__r   r   typingr   r   r   numpyr   Zpandas._libsr   r   r   Zpandas.core.dtypes.commonr   Zpandas.core.dtypes.dtypesr	   Zpandas.core.dtypes.missingr
   Zpandas.corer   Zpandas.core.array_algosr   Zpandas.core.arrays.maskedr   r   r2   Zpandas._typingr   r   r   r   r   r   rY   r'   r   r   r   r    <module>   s$   	p W