o
    5c1+                     @  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mZm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mZ erVd dlZd dl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)DtypeDtypeObjtype_t)is_list_likeis_numeric_dtype)register_extension_dtype)isna)ops)BaseMaskedArrayBaseMaskedDtype)nptc                   @  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 )#BooleanDtypeaf  
    Extension dtype for boolean data.

    .. versionadded:: 1.0.0

    .. 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   P/var/www/html/gps/gps/lib/python3.10/site-packages/pandas/core/arrays/boolean.pyr   F      zBooleanDtype.typestrc                 C     dS )Nbr   r   r   r   r   kindJ      zBooleanDtype.kindnp.dtypec                 C  s
   t dS )Nbool)r   dtyper   r   r   r   numpy_dtypeN   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_typeR   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_numericd   r"   zBooleanDtype._is_numericarray$pyarrow.Array | pyarrow.ChunkedArrayr(   c           
      C  s  ddl }|j| krtd|j dt||jr|g}n|j}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||}	||	 q%|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   )offsetF)zero_copy_onlyr%   )pyarrowr   r   	TypeError
isinstanceArraychunksbuffersr(   from_bufferslenr2   to_numpy
null_countr   zerosr$   appendr/   _concat_same_type)
r   r/   r5   r9   resultsarrbuflistdatamaskbool_arrr   r   r   __from_arrow__h   s:   


zBooleanDtype.__from_arrow__N)r   r   )r   r   )r   r#   )r   r'   )r   r$   )r/   r0   r   r(   )__name__
__module____qualname____doc__namepropertyr   r!   r&   classmethodr*   r+   r-   r.   rH   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rrt
| jrrt| }tjt| td}| |  t|| < t||  | j| |  ksot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 inputr4   zNeed to pass bool-like valuesT)skipna)floatingintegerzmixed-integer-float)r   emptyznpt.NDArray[np.bool_]z&values.shape and mask.shape must match)r7   r(   
ValueError_data_maskrP   r   ndarrayr%   r   r   r   r?   r<   r$   astypeallr6   asarrayobjectr   infer_dtyper   floatshaper/   )valuesrF   rP   mask_valuesvalues_boolvalues_objectinferred_dtypeinteger_liker   r   r   coerce_to_array   sl   


rg   c                      s   e Zd ZdZdZdZdZh dZh dZ	d$d% fddZ	e
d&ddZedddddd'ddZejejeejfZeddd(d d!Zd"d# Z  ZS ))r(   aZ  
    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).

    .. versionadded:: 1.0.0

    .. 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falsera   
np.ndarrayrF   rP   r$   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rP   )
r7   r   rY   r%   r   r6   r   _dtypesuper__init__)r   ra   rF   rP   	__class__r   r   rw   '  s   zBooleanArray.__init__r   c                 C  s   | j S r   )ru   r   r   r   r   r%   2  r   zBooleanArray.dtypeN)r%   rP   true_valuesfalse_valuesstrings	list[str]r%   Dtype | Nonerz   list[str] | Noner{   c                  sP   | j |pg | j|pg   fddfdd|D }| j|||dS )Nc                   s2   t | r| S | v rdS |  v rdS t|  d)NTFz cannot be cast to bool)r   rV   )s)false_values_uniontrue_values_unionr   r   
map_stringC  s   z:BooleanArray._from_sequence_of_strings.<locals>.map_stringc                   s   g | ]} |qS r   r   ).0x)r   r   r   
<listcomp>M  s    z:BooleanArray._from_sequence_of_strings.<locals>.<listcomp>)r%   rP   )_TRUE_VALUESunion_FALSE_VALUES_from_sequence)r)   r|   r%   rP   rz   r{   scalarsr   )r   r   r   r   _from_sequence_of_strings6  s
   

z&BooleanArray._from_sequence_of_stringsrt   r   rQ   c                C  s   |r|dksJ t ||dS )Nr   rt   )rg   )r)   valuer%   rP   r   r   r   _coerce_to_arrayR  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   r1   z(can only perform ops with 1-d structuresFrt   z+'other' should be pandas.NA or a bool. Got z	 instead.zLengths must match>   r   r   >   r   r   )rI   r   	is_scalarr7   r(   rW   rX   r
   r   r\   ndimNotImplementedErrorrg   r   item
libmissingNAis_boolr6   r   r<   rV   r   	kleene_or
kleene_and
kleene_xor_maybe_mask_result)r   otheropother_is_scalarrF   resultr   r   r   _logical_methodZ  s4   




zBooleanArray._logical_method)F)ra   rr   rF   rr   rP   r$   r   rs   )r   r   )r|   r}   r%   r~   rP   r$   rz   r   r{   r   r   r(   )r%   r   rP   r$   r   rQ   )rI   rJ   rK   rL   _internal_fill_value_truthy_value_falsey_valuer   r   rw   rN   r%   rO   r   r   rY   numbersNumberr$   r   _HANDLED_TYPESr   r   __classcell__r   r   rx   r   r(      s,    6r(   )NF)rP   r$   r   rQ   )!
__future__r   r   typingr   r   numpyr   pandas._libsr   r   r   pandas._typingr   r   r	   pandas.core.dtypes.commonr
   r   pandas.core.dtypes.dtypesr   pandas.core.dtypes.missingr   pandas.corer   pandas.core.arrays.maskedr   r   r5   r   r   rg   r(   r   r   r   r   <module>   s&    jW