HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/arrays/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/arrays/__pycache__/boolean.cpython-36.pyc
3

���h;_�@sBddlZddlmZmZmZmZmZddlZddlZ	ddl
mZmZ
ddlmZddlmZddlmZddlmZmZmZmZmZmZmZmZddlmZdd	l m!Z!m"Z"m#Z#dd
l$m%Z%ddl&m'Z'dd
l(m)Z)m*Z*er�ddl+Z+eGdd�de*��Z,de-ee	j.e	j.fd�dd�Z/Gdd�de)�Z0e0j1�e0j2�e0j3�dS)�N)�
TYPE_CHECKING�List�Tuple�Type�Union)�lib�missing)�	ArrayLike)�set_function_name)�function)�
is_bool_dtype�is_extension_array_dtype�is_float�is_float_dtype�is_integer_dtype�is_list_like�is_numeric_dtype�pandas_dtype)�register_extension_dtype)�ABCDataFrame�
ABCIndexClass�	ABCSeries)�isna)�ops�)�BaseMaskedArray�BaseMaskedDtypec@s�eZdZdZdZeeejd�dd��Z	ee
d�dd��Zeejd�dd	��Z
eed
d�dd��Ze
d�d
d�Zeed�dd��Zeed�dd��Zedd
d�dd�ZdS)�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)�returncCstjS)N)�np�bool_)�self�r#�</tmp/pip-build-5_djhm0z/pandas/pandas/core/arrays/boolean.py�type>szBooleanDtype.typecCsdS)N�br#)r"r#r#r$�kindBszBooleanDtype.kindcCs
tjd�S)N�bool)r �dtype)r"r#r#r$�numpy_dtypeFszBooleanDtype.numpy_dtype�BooleanArraycCstS)zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )r+)�clsr#r#r$�construct_array_typeJs	z!BooleanDtype.construct_array_typecCsdS)Nrr#)r"r#r#r$�__repr__UszBooleanDtype.__repr__cCsdS)NTr#)r"r#r#r$�_is_booleanXszBooleanDtype._is_booleancCsdS)NTr#)r"r#r#r$�_is_numeric\szBooleanDtype._is_numeric�
pyarrow.Array�pyarrow.ChunkedArray)�arrayrcCsXddl}t||j�r|g}n|j}g}x&|D]}tjtj|��}|j|�q,Wtj	|�S)zI
        Construct BooleanArray from pyarrow Array/ChunkedArray.
        rN)
�pyarrow�
isinstance�Array�chunksr+�_from_sequencer r3�appendZ_concat_same_type)r"r3r4r7�resultsZarrZbool_arrr#r#r$�__from_arrow__`s
zBooleanDtype.__from_arrow__N)r1r2)�__name__�
__module__�__qualname__�__doc__�name�propertyrr r!r%�strr'r)r*�classmethodr-r.r(r/r0rr;r#r#r#r$r"s"
rF)�copyrcCsNt|t�rD|dk	rtd��|j|j}}|r<|j�}|j�}||fSd}t|tj�rp|jtj	krp|rl|j�}�nt|tj�r�t
|j�r�t|�}tjt
|�td�}||jt�||<tj||j|j�||k�s�td��|}n�tj|td�}tj|dd�}d}|d|k�rtd��t|�}tjt
|�td�}||jt�||<||k�r�tj||jt�||jt�k��s�td��|dk�r�|dk�r�tjt
|�td�}np|dk�r�|}n`t|tj��r�|jtj	k�r�|dk	�r�||B}n|�r|j�}n tj|td�}|dk	�r||B}|jdk�s2td
��|jdk�sFtd��||fS)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)r)zNeed to pass bool-like valuesT)�skipna�floating�integer�mixed-integer-floatr�emptyrzvalues must be a 1D list-likezmask must be a 1D list-like)rFrGrH)rrI)r5r+�
ValueError�_data�_maskrDr �ndarrayr)r!rr�zeros�lenr(�astype�all�	TypeError�asarray�objectrZinfer_dtype�floatr3�ndim)�values�maskrDZmask_valuesZvalues_boolZ
values_objectZinferred_dtypeZinteger_liker#r#r$�coerce_to_arraywsd






rYcs<eZdZdZdZd+ejejed��fdd�
Ze	e
d�dd��Zed,edd
�dd��Z
ed-eeedd
�dd��ZejejeejfZed�dd�Zeejejfd�dd�Zd.eed
�dd�Zejd�dd�Zd/ed�dd�Zd0ed�dd�Zedd ��Zed!d"��Zd1eed#��fd$d%�
Zed&�d'd(�Z ed)d*��Z!�Z"S)2r+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
    F)rWrXrDcs>t|tj�o|jtjks td��t�|_t�j	|||d�dS)NzIvalues should be boolean numpy array. Use the 'pd.array' function instead)rD)
r5r rMr)r!rRr�_dtype�super�__init__)r"rWrXrD)�	__class__r#r$r\s
zBooleanArray.__init__)rcCs|jS)N)rZ)r"r#r#r$r)szBooleanArray.dtypeN)rDrcCs*|r|dkst�t||d�\}}t||�S)Nr)rD)�AssertionErrorrYr+)r,�scalarsr)rDrWrXr#r#r$r8szBooleanArray._from_sequence)�stringsrDrcs(dd���fdd�|D�}|j|||�S)NcSs6t|�r|S|dkrdS|dkr$dSt|�d
���dS)N�True�TRUE�true�1�1.0T�False�FALSE�false�0�0.0Fz cannot be cast to bool)rarbrcrdre)rfrgrhrirj)rrJ)�sr#r#r$�
map_stringsz:BooleanArray._from_sequence_of_strings.<locals>.map_stringcsg|]}�|��qSr#r#)�.0�x)rlr#r$�
<listcomp>(sz:BooleanArray._from_sequence_of_strings.<locals>.<listcomp>)r8)r,r`r)rDr_r#)rlr$�_from_sequence_of_stringss
z&BooleanArray._from_sequence_of_strings)�methodc	s|dkrtd��|jdf�}x&||D]}t||jtf�s&tSq&Wtj|||f|�|�}|tk	rf|Stj	t
|�td��g}x8|D]0}t|t�r��|jO�|j
|j�q�|j
|�q�W�fdd��t||�||�}t|t�r�t�fdd�|D��n�|�SdS)	N�reducez%The 'reduce' method is not supported.�out)r)cs*t|j�r�j�}t||�Stj|�<|S)N)rr)rDr+r �nan)rn�m)rXr#r$�reconstructIs



z1BooleanArray.__array_ufunc__.<locals>.reconstructc3s|]}�|�VqdS)Nr#)rmrn)rvr#r$�	<genexpr>Vsz/BooleanArray.__array_ufunc__.<locals>.<genexpr>)�NotImplementedError�getr5�_HANDLED_TYPESr+�NotImplementedrZ!maybe_dispatch_ufunc_to_dunder_opr rNrOr(rLr9rK�getattr�tuple)	r"Zufuncrq�inputs�kwargsrsrn�resultZinputs2r#)rXrvr$�__array_ufunc__-s,



zBooleanArray.__array_ufunc__cCst|�S)N)rY)r"�valuer#r#r$�_coerce_to_arrayZszBooleanArray._coerce_to_arrayTcCs�ddlm}t|�}t|t�r<t||d�\}}t||dd�St||�rX|j�j|dd�St	|�r�|j
rptd��n|jj
||d�St|�r�t|�r�ddlm}||jj
|j�|jj�dd�St|�r�|j
r�td��|j}t|�r�tj}|j||dd�S)	a�
        Cast to a NumPy array or ExtensionArray with 'dtype'.

        Parameters
        ----------
        dtype : str or dtype
            Typecode or data-type to which the array is cast.
        copy : bool, default True
            Whether to copy the data, even if not necessary. If False,
            a copy is made only if the old dtype does not match the
            new dtype.

        Returns
        -------
        ndarray or ExtensionArray
            NumPy ndarray, BooleanArray or IntegerArray with 'dtype' for its dtype.

        Raises
        ------
        TypeError
            if incompatible type with an BooleanDtype, equivalent of same_kind
            casting
        r)�StringDtype)rDFz cannot convert float NaN to bool)�IntegerArrayzcannot convert NA to integer)r)�na_valuerD)Zpandas.core.arrays.string_r�rr5rrYr+r-r8rZ_hasnarJrKrPr
r�pandas.core.arraysr�r*rLrDZ	_na_valuerr rtZto_numpy)r"r)rDr�rWrXr�r�r#r#r$rP]s,


zBooleanArray.astypecCs|jj�}d||j<|S)a
        Return values for sorting.

        Returns
        -------
        ndarray
            The transformed values should maintain the ordering between values
            within the array.

        See Also
        --------
        ExtensionArray.argsort
        r���)rKrDrL)r"�datar#r#r$�_values_for_argsort�s

z BooleanArray._values_for_argsort)rEcKsn|jdd�tjf|�|jj�}tj||jd�|j�}|rB|S|s^t	|�dks^|jj�rb|S|j
jSdS)a8
        Return whether any element is True.

        Returns False unless there is at least one element that is True.
        By default, NAs are skipped. If ``skipna=False`` is specified and
        missing values are present, similar :ref:`Kleene logic <boolean.kleene>`
        is used as for logical operations.

        Parameters
        ----------
        skipna : bool, default True
            Exclude NA values. If the entire array is NA and `skipna` is
            True, then the result will be False, as for an empty array.
            If `skipna` is False, the result will still be True if there is
            at least one element that is True, otherwise NA will be returned
            if there are NA's present.
        **kwargs : any, default None
            Additional keywords have no effect but might be accepted for
            compatibility with NumPy.

        Returns
        -------
        bool or :attr:`pandas.NA`

        See Also
        --------
        numpy.any : Numpy version of this method.
        BooleanArray.all : Return whether all elements are True.

        Examples
        --------
        The result indicates whether any element is True (and by default
        skips NAs):

        >>> pd.array([True, False, True]).any()
        True
        >>> pd.array([True, False, pd.NA]).any()
        True
        >>> pd.array([False, False, pd.NA]).any()
        False
        >>> pd.array([], dtype="boolean").any()
        False
        >>> pd.array([pd.NA], dtype="boolean").any()
        False

        With ``skipna=False``, the result can be NA if this is logically
        required (whether ``pd.NA`` is True or False influences the result):

        >>> pd.array([True, False, pd.NA]).any(skipna=False)
        True
        >>> pd.array([False, False, pd.NA]).any(skipna=False)
        <NA>
        �axisNFr)�pop�nvZvalidate_anyrKrDr �putmaskrL�anyrOr)r�)r"rErrWr�r#r#r$r��s6
zBooleanArray.anycKsp|jdd�tjf|�|jj�}tj||jd�|j�}|rB|S|s`t	|�dks`|jj
�rd|S|jjSdS)a�
        Return whether all elements are True.

        Returns True unless there is at least one element that is False.
        By default, NAs are skipped. If ``skipna=False`` is specified and
        missing values are present, similar :ref:`Kleene logic <boolean.kleene>`
        is used as for logical operations.

        Parameters
        ----------
        skipna : bool, default True
            Exclude NA values. If the entire array is NA and `skipna` is
            True, then the result will be True, as for an empty array.
            If `skipna` is False, the result will still be False if there is
            at least one element that is False, otherwise NA will be returned
            if there are NA's present.
        **kwargs : any, default None
            Additional keywords have no effect but might be accepted for
            compatibility with NumPy.

        Returns
        -------
        bool or :attr:`pandas.NA`

        See Also
        --------
        numpy.all : Numpy version of this method.
        BooleanArray.any : Return whether any element is True.

        Examples
        --------
        The result indicates whether any element is True (and by default
        skips NAs):

        >>> pd.array([True, True, pd.NA]).all()
        True
        >>> pd.array([True, False, pd.NA]).all()
        False
        >>> pd.array([], dtype="boolean").all()
        True
        >>> pd.array([pd.NA], dtype="boolean").all()
        True

        With ``skipna=False``, the result can be NA if this is logically
        required (whether ``pd.NA`` is True or False influences the result):

        >>> pd.array([True, True, pd.NA]).all(skipna=False)
        <NA>
        >>> pd.array([True, False, pd.NA]).all(skipna=False)
        False
        r�NTr)
r�r�Zvalidate_allrKrDr r�rLrQrOr�r)r�)r"rErrWr�r#r#r$rQ�s4
zBooleanArray.allcs&�fdd�}d�j�d�}t|||�S)Ncspt|tttf�rtS�jdks"t�tj|�}t|t	�}tj
|�}d}|rX|j|j}}nNt
|�r�tj|dd�}|jd	kr�td
��t|dd�\}}nt|tj�r�|j�}|r�|tjkp�tj|�r�td
t|�j�d���|o�t|�t|�k�r�td���jdk�rtj|j||j|�\}}nJ�jdk�rBtj|j||j|�\}}n$�jdk�rftj|j||j|�\}}t	||�S)N�or_�ror_�and_�rand_�xor�rxorr()r)rz(can only perform ops with 1-d structuresF)rDz+'other' should be pandas.NA or a bool. Got z	 instead.zLengths must match to compare>r�r�r�r�r�r�>r�r�>r�r�>r�r�) r5rrrr{r<r^r�item_from_zerodimr+Z	is_scalarrKrLrr rSrVrxrYr!�item�
libmissing�NAZis_boolrRr%rOrJrZ	kleene_orZ
kleene_andZ
kleene_xor)r"�otherZother_is_booleanarrayZother_is_scalarrXr�)�opr#r$�logical_method2s:



z;BooleanArray._create_logical_method.<locals>.logical_method�__)r<r
)r,r�r�r@r#)r�r$�_create_logical_method0s)z#BooleanArray._create_logical_methodcs$�fdd�}d�j��}t|||�S)Ncsddlm}t|ttt|f�r"tStj|�}d}t|t	�rJ|j
|j}}n<t|�r�t
j|�}|jdkrntd��t|�t|�kr�td��|tjkr�t
j|j
�}t
j|j
�}nftj��8tjddt�t
jdd���|j
|�}WdQRXWdQRX|dk�r|jj�}n
|j|B}t	||d	d
�S)Nr)r�rz(can only perform ops with 1-d structureszLengths must match to compare�ignoreZelementwise)rQF)rD)Z
pandas.arraysr�r5rrrr{rr�r+rKrLrr rSrVrxrOrJr�r�Z
zeros_likeZ	ones_like�warnings�catch_warnings�filterwarnings�
FutureWarning�errstaterD)r"r�r�rXr�)r�r#r$�
cmp_method`s4





 

z:BooleanArray._create_comparison_method.<locals>.cmp_methodr�)r<r
)r,r�r�r@r#)r�r$�_create_comparison_method^s-z&BooleanArray._create_comparison_method)r@rEcs2|dkr t||�fd|i|��St�j||f|�S)Nr�rQrE>r�rQ)r|r[�_reduce)r"r@rEr)r]r#r$r��szBooleanArray._reduce)�op_namecCspt|�st|�s|dkr&tj||<|St|�r<t||dd�St|�r^ddlm}|||dd�Stj||<|SdS)	z�
        Parameters
        ----------
        result : array-like
        mask : array-like bool
        other : scalar or array-like
        op_name : str
        �rtruediv�truedivF)rDr)r�N)r�r�)	rrr rtrr+rr�r�)r"r�rXr�r�r�r#r#r$�_maybe_mask_result�s

zBooleanArray._maybe_mask_resultcs,�j���fdd�}d��d�}t|||�S)Nc
s<t|tttf�rtStj|�}d}t|t�r<|j|j	}}n<t
|�rxtj|�}|j
dkr`td��t|�t|�krxtd��|dkr�|j	}|tjkr�|dO}n
|j	|B}|tjkrֈdkr�d}nd}tjt|j�|d
�}n$tjdd���|j|�}WdQRX�dk�r,|\}}|j|||d�|j|||d�fS|j|||��S)Nrz(can only perform ops with 1-d structureszLengths must matchT�floordiv�	rfloordiv�mod�rmod�pow�rpowZint8r()r)r�)rQ�divmod>r�r�r�r�r�r�)r5rrrr{rr�r+rKrLrr rSrVrxrOrJr�r�rNr�r�)r"r�rXr)r��divr�)r�r�r#r$�boolean_arithmetic_method�s<








zIBooleanArray._create_arithmetic_method.<locals>.boolean_arithmetic_methodr�)r<r
)r,r�r�r@r#)r�r�r$�_create_arithmetic_method�s3z&BooleanArray._create_arithmetic_method)F)NF)NF)T)T)T)T)#r<r=r>r?Z_internal_fill_valuer rMr(r\rArr)rCr8rrBrp�numbers�Numberr!rzr�rr�r	rPr�r�rQr�r�r�r�r��
__classcell__r#r#)r]r$r+�s,4	
-:DC.2r+)NF)4r�Ztypingrrrrrr�Znumpyr Zpandas._libsrrr�Zpandas._typingr	Z
pandas.compatr
Zpandas.compat.numpyrr�Zpandas.core.dtypes.commonrr
rrrrrrZpandas.core.dtypes.dtypesrZpandas.core.dtypes.genericrrrZpandas.core.dtypes.missingrZpandas.corerZmaskedrrr4rr(rMrYr+Z_add_logical_opsZ_add_comparison_opsZ_add_arithmetic_opsr#r#r#r$�<module>s6(
UT&