o
    �¨Êh˜0  ã                   @  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   úL/var/www/html/env/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|s°t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|  ¡ } nŒt | 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r�tdƒ‚td
t|ƒƒ}tjt| ƒtd�} ||   t¡| | < ||v rÃt | |   t¡||   t¡k¡sÃtdƒ‚|du rÔ|du rÔt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 r‹t | 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