a
    DBìj˜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r´d 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   úY/mnt/bigstor/www/usecases/.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¡r”t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 rèt| ƒ}tjt| ƒtd�}| |   t¡|| < t ||   | j¡| |  k¡sâtdƒ‚|} n¦t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�}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>   ÚTrueÚTRUEz1.0Ú1Útrue>   z0.0Ú0ÚFalseÚFALSEÚ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r²t |¡s²tdt|ƒj › d	�ƒ‚|sÎt| ƒt|ƒkrÎtd
ƒ‚|j dv ròt | j|| j|¡\}}n>|j dv �rt | j|| j|¡\}}nt | j|| j|¡\}}|  ||¡S )N>   Úror_ZrxorÚxorÚand_Úor_Úrand_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|   ry   >   r{   r}   )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