o
    ҷh{0                     @  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 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rXd 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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dZdZ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
    booleanreturntypec                 C  s   t jS N)npbool_self r   M/var/www/html/venv/lib/python3.10/site-packages/pandas/core/arrays/boolean.pyr   C      zBooleanDtype.typestrc                 C     dS )Nbr   r   r   r   r   kindG      zBooleanDtype.kindnp.dtypec                 C  s
   t dS )Nbool)r   dtyper   r   r   r   numpy_dtypeK   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_typeO   s   	z!BooleanDtype.construct_array_typec                 C  r    )Nr   r   r   r   r   r   __repr__Z   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_numerica   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_buffersr4   to_numpy
null_countzerosappendr0   _concat_same_type)r   r0   r6   r=   r>   maskdataresultsarrbuflistbool_arrr   r   r   __from_arrow__e   sF   




zBooleanDtype.__from_arrow__N)r   r   )r   r   )r   r$   )r   r(   r   r%   )r0   r1   r   r)   )__name__
__module____qualname____doc__namepropertyr   r"   r'   classmethodr+   r,   r.   r/   rN   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iufcbr2   zNeed to pass bool-like valuesTskipna)floatingintegerzmixed-integer-float)r   r@   npt.NDArray[np.bool_]z&values.shape and mask.shape must match)r:   r)   
ValueError_data_maskrW   r   ndarrayr&   r   r"   r	   rE   r<   r%   astypeallr9   asarrayobjectr   infer_dtyper   floatshaper0   )valuesrH   rW   mask_valuesvalues_boolvalues_objectinferred_dtypeinteger_liker   r   r   coerce_to_array   sl   


rp   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falserj   
np.ndarrayrH   r^   r   r   c                   s   t  ||}t |_|S r   )super_simple_newr   _dtype)r*   rj   rH   result	__class__r   r   r}   ,  s   zBooleanArray._simple_newrW   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rW   )
r:   r   rb   r&   r   r9   r   r~   r|   __init__)r   rj   rH   rW   r   r   r   r   2  s   zBooleanArray.__init__r   c                 C  s   | j S r   )r~   r   r   r   r   r&   =  r   zBooleanArray.dtypeN)r&   rW   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)r_   )sfalse_values_uniontrue_values_unionr   r   
map_stringN  s
   z:BooleanArray._from_sequence_of_strings.<locals>.map_stringr2   )r&   rW   rO   )
_TRUE_VALUESunion_FALSE_VALUESr   r0   rf   r	   listmap_from_sequence)	r*   r   r&   rW   r   r   r   scalarsrH   r   r   r   _from_sequence_of_stringsA  s   
z&BooleanArray._from_sequence_of_stringsr   r   rX   c                C  s   |r|dksJ t ||dS )Nr   r   )rp   )r*   valuer&   rW   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%   r2   r3   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   )rP   r   	is_scalarr:   r)   r`   ra   r   r   re   ndimNotImplementedErrorrp   r   item
libmissingNAis_boolr9   r   r<   r_   r
   	kleene_or
kleene_and
kleene_xor_maybe_mask_result)r   otheropother_is_scalarrH   r   r   r   r   _logical_methode  s4   




zBooleanArray._logical_methodrZ   rT   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)
r`   ra   getattrr   r}   pandas.core.arraysr   rc   int_accumulate)r   rT   r[   kwargsrI   rH   r   r   r   r   r   r     s   
zBooleanArray._accumulate)rj   r{   rH   r^   r   r   )F)rj   r{   rH   r{   rW   r%   r   r   )r   r   )r   r   r&   r   rW   r%   r   r   r   r   r   r)   )r&   r   rW   r%   r   rX   )rT   r   r[   r%   r   r   )rP   rQ   rR   rS   _internal_fill_value_truthy_value_falsey_valuer   r   rV   r}   r   rU   r&   r   r   rb   numbersNumberr%   r   _HANDLED_TYPESr   r   r   __classcell__r   r   r   r   r)      s4    4$r)   )NF)rW   r%   r   rX   )#
__future__r   r   typingr   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   r6   pandas._typingr   r   r   r   r   r   rp   r)   r   r   r   r   <module>   s&    	pW