U
    f/e@                  	   @  s  U d dl mZ d dlZd dlZd dlmZmZmZ	 d dl
mZmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZmZmZmZmZmZmZ d d	lm Z  d d
l!m"Z"m#Z# d dl$m%Z%m&Z& d dl'm(Z( d dl)m*Z* G dd de&Z+ddddZ,d/dddddZ-G dd de%Z.dZ/eG dd de+Z0eG dd de+Z1eG d d! d!e+Z2eG d"d# d#e+Z3eG d$d% d%e+Z4eG d&d' d'e+Z5eG d(d) d)e+Z6eG d*d+ d+e+Z7e0 e1 e2 e3 e4 e5 e6 e7 d,Z8d-e9d.< dS )0    )annotationsN)iNaTlibmissing)	ArrayLikeDtypeDtypeObj)function)cache_readonly)ExtensionDtyperegister_extension_dtype)is_bool_dtypeis_datetime64_dtypeis_floatis_float_dtypeis_integer_dtypeis_list_likeis_object_dtypepandas_dtype)isna)BaseMaskedArrayBaseMaskedDtype)NumericArrayNumericDtype)invalid_comparison)
to_numericc                   @  sv   e Zd ZdZddddZ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dZdS )_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.
    strreturnc                 C  s$   | j r
dnd}| dd| j  dS )NU Int   zDtype())is_unsigned_integeritemsize)selfsign r(   >/tmp/pip-unpacked-wheel-tiezk1ph/pandas/core/arrays/integer.py__repr__:   s    z_IntegerDtype.__repr__boolc                 C  s
   | j dkS )Nikindr&   r(   r(   r)   is_signed_integer>   s    z_IntegerDtype.is_signed_integerc                 C  s
   | j dkS )Nur-   r/   r(   r(   r)   r$   B   s    z!_IntegerDtype.is_unsigned_integerc                 C  s   dS )NTr(   r/   r(   r(   r)   _is_numericF   s    z_IntegerDtype._is_numericztype[IntegerArray]c                 C  s   t S )zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )IntegerArray)clsr(   r(   r)   construct_array_typeJ   s    	z"_IntegerDtype.construct_array_typezlist[DtypeObj]zDtypeObj | None)dtypesr   c                 C  sp   t dd |D sd S tdd |D g }t|tjrFtt| S t|tjrlddlm	} |t| S d S )Nc                 s  s@   | ]8}t |tp6t |tjo6t|tjp6t|tjV  qd S N)
isinstancer   npdtype
issubdtypenumberZbool_.0tr(   r(   r)   	<genexpr>W   s   
z2_IntegerDtype._get_common_dtype.<locals>.<genexpr>c                 S  s    g | ]}t |tr|jn|qS r(   )r8   r   numpy_dtyper=   r(   r(   r)   
<listcomp>e   s   z3_IntegerDtype._get_common_dtype.<locals>.<listcomp>r   )FLOAT_STR_TO_DTYPE)
allr9   Zfind_common_typer;   integerINT_STR_TO_DTYPEr   floatingZpandas.core.arrays.floatingrC   )r&   r6   Znp_dtyperC   r(   r(   r)   _get_common_dtypeU   s     z_IntegerDtype._get_common_dtypeN)__name__
__module____qualname____doc__r*   r
   r0   r$   propertyr2   classmethodr5   rH   r(   r(   r(   r)   r   0   s   	
r   r+   copyc              
   C  s~   z| j |d|dW S  tk
rx } zH| j ||d}|| k rJ| W Y $S td| j dt| |W 5 d}~X Y nX dS )z}
    Safely cast the values to the dtype if they
    are equivalent, meaning floats must be equivalent to the
    ints.

    safe)ZcastingrP   rO   z"cannot safely cast non-equivalent z to N)astype	TypeErrorrD   r:   r9   )valuesr:   rP   errZcastedr(   r(   r)   	safe_castv   s    rV   Ftuple[np.ndarray, np.ndarray]rP   r   c              
   C  sT  |dkr"t | dr"t| jr"| j}|dk	rt|trP|dsH|drP| }tt|t	szt
tt| }W n2 tk
r } ztd| |W 5 d}~X Y nX t| tr| j| j } }|dk	r| j|jdd} |r|  } | }| |fS tj| |d} t| r\tj| dd	}|d
kr@tt| } | tj n|dkrt| j dnJt| rt|rtj| t|d} n$t| st| st| j d|dkrt | }nt|t| kst!| j"dkstd|j"dkstd|dkrtd}n|j}|# r>|  } d| |< t$| |dd} nt$| |dd} | |fS )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)
    Nr:   r"   ZUIntzinvalid dtype specified FrO   Tskipnaempty)rG   rE   zmixed-integerz
integer-nazmixed-integer-floatz' cannot be converted to an IntegerDtyper:   rP      zvalues must be a 1D list-likezmask must be a 1D list-likeint64)%hasattrr   r:   r8   r   
startswithlower
issubclasstyper   rF   r9   KeyError
ValueErrorr3   _data_maskrR   rA   rP   arrayr   r   Zinfer_dtyper[   lenfillnanrS   r   intr   r   AssertionErrorndimanyrV   )rT   r:   maskrP   rU   Zinferred_typer(   r(   r)   coerce_to_array   sf    

"







rq   c                      s  e Zd ZdZdZeddddZd0ddd	d
 fddZeddddd	d dddZ	eddddd	d dddZ
ddddZd1d	dd fddZddddZdd  Zdd!d" fd#d$
Zdd!d" fd%d&
Zdd' fd(d)
Zdd' fd*d+
Zd,d-d.d/Z  ZS )2r3   a>  
    Array of integer (optional missing) values.

    .. 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   r   c                 C  s   t t| jj S r7   )rF   r   rf   r:   r/   r(   r(   r)   r:   3  s    zIntegerArray.dtypeFz
np.ndarrayr+   )rT   rp   rP   c                   s6   t |tjr|jjdks tdt j|||d d S )N)r,   r1   zIvalues should be integer numpy array. Use the 'pd.array' function insteadrO   )r8   r9   Zndarrayr:   r.   rS   super__init__)r&   rT   rp   rP   	__class__r(   r)   rs   7  s
    zIntegerArray.__init__Nr\   zDtype | None)r:   rP   r   c                C  s   t |||d\}}t||S )Nr\   )rq   r3   )r4   scalarsr:   rP   rT   rp   r(   r(   r)   _from_sequence?  s    zIntegerArray._from_sequencec                C  s   t |dd}| j|||dS )Nraise)errorsr\   )r   rw   )r4   stringsr:   rP   rv   r(   r(   r)   _from_sequence_of_stringsF  s    z&IntegerArray._from_sequence_of_stringsrW   c                 C  s   t || jdS )Nr:   )rq   r:   )r&   valuer(   r(   r)   _coerce_to_arrayM  s    zIntegerArray._coerce_to_arrayTr   rX   c                   s\   t |}t|tr"t j||dS t|r2tj}nt|rFt	d}nt
j}| j||dd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
        rO   ZNaTF)r:   na_valuerP   )r   r8   r   rr   rR   r   r9   rk   r   Z
datetime64r   Z
no_defaultZto_numpy)r&   r:   rP   r   rt   r(   r)   rR   P  s    
zIntegerArray.astypec                 C  s*   | j  }| j r&| 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 : Return the indices that would sort this array.
        r]   )rf   rP   rg   ro   min)r&   datar(   r(   r)   _values_for_argsortz  s    

z IntegerArray._values_for_argsortc              
   C  s.  ddl m} d }t|tr*|j|j }}n<t|rft|}|j	dkrNt
dt| t|krftd|tjkrtj| jjdd}tj| jjdd}nnt ` tdd	t tjdd
$ t| jd|j d}||}W 5 Q R X |tkrt| j||}W 5 Q R X |d kr| j }n
| j|B }|||S )Nr   )BooleanArrayr]   z(can only perform ops with 1-d structureszLengths must match to comparer+   r|   ignoreZelementwise)rD   __)pandas.core.arraysr   r8   r   rf   rg   r   r9   Zasarrayrn   NotImplementedErrorri   re   
libmissingZNAzerosshapeZoneswarningscatch_warningsfilterwarningsFutureWarningZerrstategetattrrI   NotImplementedr   rP   )r&   otheropr   rp   resultmethodr(   r(   r)   _cmp_method  s0    






zIntegerArray._cmp_methodr   rZ   	min_countc                  s   t d| t jd||dS )Nr(   sumr   )nvZvalidate_sumrr   _reducer&   rZ   r   kwargsrt   r(   r)   r     s    zIntegerArray.sumc                  s   t d| t jd||dS )Nr(   prodr   )r   Zvalidate_prodrr   r   r   rt   r(   r)   r     s    zIntegerArray.prodrY   c                  s   t d| t jd|dS )Nr(   r   rY   )r   Zvalidate_minrr   r   r&   rZ   r   rt   r(   r)   r     s    zIntegerArray.minc                  s   t d| t jd|dS )Nr(   maxrY   )r   Zvalidate_maxrr   r   r   rt   r(   r)   r     s    zIntegerArray.maxr   )op_namec                 C  sl   t |st|s|dkr2ddlm} |||ddS |jdkrZddlm} t||< ||S t| ||ddS )z
        Parameters
        ----------
        result : array-like
        mask : array-like bool
        other : scalar or array-like
        op_name : str
        )Zrtruedivtruedivr   )FloatingArrayFrO   ztimedelta64[ns])TimedeltaArray)	r   r   r   r   r:   r   r   Z_simple_newrc   )r&   r   rp   r   r   r   r   r(   r(   r)   _maybe_mask_result  s    

zIntegerArray._maybe_mask_result)F)T)rI   rJ   rK   rL   Z_internal_fill_valuer
   r:   rs   rN   rw   r{   r~   rR   r   r   r   r   r   r   r   __classcell__r(   r(   rt   r)   r3      s,   D  *-r3   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"   e Zd ZejZdZejddZ	dS )	Int8DtypeZInt8int8r|   N)
rI   rJ   rK   r9   r   rc   name_dtype_docstringformatrL   r(   r(   r(   r)   r     s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )
Int16DtypeZInt16int16r|   N)
rI   rJ   rK   r9   r   rc   r   r   r   rL   r(   r(   r(   r)   r     s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )
Int32DtypeZInt32int32r|   N)
rI   rJ   rK   r9   r   rc   r   r   r   rL   r(   r(   r(   r)   r     s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )
Int64DtypeZInt64r^   r|   N)
rI   rJ   rK   r9   r^   rc   r   r   r   rL   r(   r(   r(   r)   r     s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )
UInt8DtypeZUInt8uint8r|   N)
rI   rJ   rK   r9   r   rc   r   r   r   rL   r(   r(   r(   r)   r     s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )UInt16DtypeZUInt16uint16r|   N)
rI   rJ   rK   r9   r   rc   r   r   r   rL   r(   r(   r(   r)   r     s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )UInt32DtypeZUInt32uint32r|   N)
rI   rJ   rK   r9   r   rc   r   r   r   rL   r(   r(   r(   r)   r   $  s   r   c                   @  s"   e Zd ZejZdZejddZ	dS )UInt64DtypeZUInt64uint64r|   N)
rI   rJ   rK   r9   r   rc   r   r   r   rL   r(   r(   r(   r)   r   +  s   r   )r   r   r   r^   r   r   r   r   zdict[str, _IntegerDtype]rF   )NF):
__future__r   r   Znumpyr9   Zpandas._libsr   r   r   r   Zpandas._typingr   r   r   Zpandas.compat.numpyr	   r   Zpandas.util._decoratorsr
   Zpandas.core.dtypes.baser   r   Zpandas.core.dtypes.commonr   r   r   r   r   r   r   r   Zpandas.core.dtypes.missingr   Zpandas.core.arrays.maskedr   r   Zpandas.core.arrays.numericr   r   Zpandas.core.opsr   Zpandas.core.tools.numericr   r   rV   rq   r3   r   r   r   r   r   r   r   r   r   rF   __annotations__r(   r(   r(   r)   <module>   s^    (
F   b {