o
    ŀg0                     @  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rZd 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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Zed d
dZed!ddZ	e
d"ddZd ddZed#ddZed#ddZ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returntypec                 C  s   t jS N)npbool_self r   N/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/arrays/boolean.pyr   D      zBooleanDtype.typestrc                 C     dS )Nbr   r   r   r   r   kindH      zBooleanDtype.kindnp.dtypec                 C  s
   t dS )Nbool)r   dtyper   r   r   r   numpy_dtypeL   s   
zBooleanDtype.numpy_dtype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  r"   )Nr   r   r   r   r   r   __repr__[   s   zBooleanDtype.__repr__r'   c                 C  r"   NTr   r   r   r   r   _is_boolean^   r%   zBooleanDtype._is_booleanc                 C  r"   r/   r   r   r   r   r   _is_numericb   r%   zBooleanDtype._is_numericarray$pyarrow.Array | pyarrow.ChunkedArrayr+   c                 C  sj  ddl }|j| kr|j|jstd|j dt||jr)|g}t|}n|j	}|
 }|j|jrJtj|td}tj|td}t||S g }|D ]N}| }	|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||}
||
 qN|sttjg tjdtjg tjdS t|S )
zI
        Construct BooleanArray from pyarrow Array/ChunkedArray.
        r   Nz$Expected array of boolean type, got z insteadr(      )offsetF)zero_copy_only)pyarrowr   r   typesis_null	TypeError
isinstanceArraylenchunkslengthr   onesr'   emptyr+   buffersfrom_buffersr6   to_numpy
null_countzerosappendr2   _concat_same_type)r   r2   r8   r?   r@   maskdataresultsarrbuflistbool_arrr   r   r   __from_arrow__f   sF   




zBooleanDtype.__from_arrow__N)r   r   )r   r!   )r   r&   )r   r*   r   r'   )r2   r3   r   r+   )__name__
__module____qualname____doc__r   __annotations__propertyr   r$   r)   classmethodr-   r.   r0   r1   rP   r   r   r   r   r   (   s"   
 

r   Fcopyr'   r   tuple[np.ndarray, np.ndarray]c                 C  s,  t | tr"|durtd| j| j} }|r|  } | }| |fS d}t | tjr7| jtj	kr7|r6|  } nt | tjrs| jj
dv rst| }tjt| td}| |  t|| < t||  | j| |  ksptd|} nPtj| td}tj|dd}d}|d	| vrt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}n3|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 inputiufcbr4   zNeed to pass bool-like valuesTskipna)floatingintegerzmixed-integer-float)r   rB   npt.NDArray[np.bool_]z&values.shape and mask.shape must match)r<   r+   
ValueError_data_maskrY   r   ndarrayr(   r   r$   r
   rG   r>   r'   astypeallr;   asarrayobjectr   infer_dtyper   floatshaper2   )valuesrJ   rY   mask_valuesvalues_boolvalues_objectinferred_dtypeinteger_liker   r   r   coerce_to_array   sl   


rr   c                      s   e Zd ZdZdZdZdZh dZh dZe	d/ fddZ
	d0d1 fddZed2ddZe	dddddd3dd ZejejeejfZe	dd!d4d$d%Zd&d' Zdd(d5d-d.Z  ZS )6r+   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.01TRUETruetrue>   0.00FALSEFalsefalserl   
np.ndarrayrJ   r`   r   r   c                   s   t  ||}t |_|S r   )super_simple_newr   _dtype)r,   rl   rJ   result	__class__r   r   r   -  s   zBooleanArray._simple_newrY   r'   Nonec                   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rY   )
r<   r   rd   r(   r   r;   r   r   r~   __init__)r   rl   rJ   rY   r   r   r   r   3  s   zBooleanArray.__init__r   c                 C  s   | j S r   )r   r   r   r   r   r(   >  r    zBooleanArray.dtypeN)r(   rY   true_valuesfalse_valuesstrings	list[str]r(   Dtype | Noner   list[str] | Noner   c          	        sp   | j |pg | j|pg  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)NTFz cannot be cast to bool)ra   )sfalse_values_uniontrue_values_unionr   r   
map_stringO  s
   z:BooleanArray._from_sequence_of_strings.<locals>.map_stringr4   )r(   rY   rQ   )
_TRUE_VALUESunion_FALSE_VALUESr   r2   rh   r
   listmap_from_sequence)	r,   r   r(   rY   r   r   r   scalarsrJ   r   r   r   _from_sequence_of_stringsB  s   
z&BooleanArray._from_sequence_of_stringsr   r   rZ   c                C  s   |r|dksJ t ||dS )Nr   r   )rr   )r,   valuer(   rY   r   r   r   _coerce_to_array^  s   zBooleanArray._coerce_to_arrayc                 C  s:  |j dv sJ t|}d }t|tr|j|j}}n't|r8tj	|dd}|j
dkr/tdt|dd\}}n
t|tjrB| }|rY|tjurYt|sYtdt|j  d	|sgt| t|krgtd
|j dv ryt| j|| j|\}}n|j dv rt| j|| j|\}}nt| j|| j|\}}| ||S )N>   or_xorand_ror_rxorrand_r'   r4   r5   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>   r   r   >   r   r   )rR   r   	is_scalarr<   r+   rb   rc   r   r   rg   ndimNotImplementedErrorrr   r   item
libmissingNAis_boolr;   r   r>   ra   r   	kleene_or
kleene_and
kleene_xor_maybe_mask_result)r   otheropother_is_scalarrJ   r   r   r   r   _logical_methodf  s4   




zBooleanArray._logical_methodr\   r   r!   r]   r   c                K  sr   | j }| j}|dv r"tt|}|||fd|i|\}}| ||S ddlm} ||t|j	|fd|i|S )N)cummincummaxr]   r   )IntegerArray)
rb   rc   getattrr   r   pandas.core.arraysr   re   int_accumulate)r   r   r]   kwargsrK   rJ   r   r   r   r   r   r     s   
zBooleanArray._accumulate)rl   r}   rJ   r`   r   r   )F)rl   r}   rJ   r}   rY   r'   r   r   )r   r   )r   r   r(   r   rY   r'   r   r   r   r   r   r+   )r(   r   rY   r'   r   rZ   )r   r!   r]   r'   r   r   )rR   rS   rT   rU   _internal_fill_value_truthy_value_falsey_valuer   r   rX   r   r   rW   r(   r   r   rd   numbersNumberr'   r   _HANDLED_TYPESr   r   r   __classcell__r   r   r   r   r+      s4    4$r+   )NF)rY   r'   r   rZ   )$
__future__r   r   typingr   r   r   numpyr   pandas._libsr   r   r   pandas.core.dtypes.commonr   pandas.core.dtypes.dtypesr	   pandas.core.dtypes.missingr
   pandas.corer   pandas.core.array_algosr   pandas.core.arrays.maskedr   r   r8   pandas._typingr   r   r   r   r   r   rr   r+   r   r   r   r   <module>   s&    	pW