U
    f/e3                     @  s8  d dl mZ d dlZd dl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 d dlmZ d dlmZmZmZmZmZmZmZ d d	lmZmZ d d
l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ddZ(G dd de!Z)dZ*eG dd de'Z+eG dd de'Z,e+ e, dZ-dS )    )annotationsN)libmissing)	ArrayLikeDtypeObj)function)cache_readonly)astype_nansafe)is_bool_dtypeis_datetime64_dtypeis_float_dtypeis_integer_dtypeis_list_likeis_object_dtypepandas_dtype)ExtensionDtyperegister_extension_dtype)isna)NumericArrayNumericDtype)invalid_comparison)
to_numericc                   @  sR   e Zd Z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 )FloatingDtypea  
    An ExtensionDtype to hold a single size of floating dtype.

    These specific implementations are subclasses of the non-public
    FloatingDtype. For example we have Float32Dtype to represent float32.

    The attributes name & type are set when these subclasses are created.
    strreturnc                 C  s   | j  dS )NzDtype())nameself r   ?/tmp/pip-unpacked-wheel-tiezk1ph/pandas/core/arrays/floating.py__repr__4   s    zFloatingDtype.__repr__boolc                 C  s   dS )NTr   r   r   r   r    _is_numeric7   s    zFloatingDtype._is_numericztype[FloatingArray]c                 C  s   t S )zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )FloatingArray)clsr   r   r    construct_array_type;   s    	z"FloatingDtype.construct_array_typezlist[DtypeObj]zDtypeObj | None)dtypesr   c                 C  sJ   t dd |D sd S tdd |D g }t|tjrFtt| S d S )Nc                 s  s   | ]}t |tV  qd S N)
isinstancer   .0tr   r   r    	<genexpr>H   s     z2FloatingDtype._get_common_dtype.<locals>.<genexpr>c                 S  s   g | ]
}|j qS r   )numpy_dtyper*   r   r   r    
<listcomp>M   s     z3FloatingDtype._get_common_dtype.<locals>.<listcomp>)allnpZfind_common_typeZ
issubdtypefloatingFLOAT_STR_TO_DTYPEr   )r   r'   Znp_dtyper   r   r    _get_common_dtypeF   s    zFloatingDtype._get_common_dtypeN)
__name__
__module____qualname____doc__r!   propertyr#   classmethodr&   r4   r   r   r   r    r   *   s   	
r   Fr"   tuple[np.ndarray, np.ndarray]copyr   c              
   C  sL  |dkr"t | dr"t| jr"| j}|dk	rt|trF|drF| }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| rRtj| dd}|d	kr6tt| } | tj n|d
krt| j dnJt| rxt|rxt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}|# r6|  } tj| |< | j|dd} n| j|dd} | |fS )a,  
    Coerce the input values array to numpy arrays with a mask.

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

    Returns
    -------
    tuple of (values, mask)
    NdtypeZFloatzinvalid dtype specified Fr=   Tskipnaempty)r2   integerzmixed-integerz
integer-nazmixed-integer-floatz' cannot be converted to a FloatingDtyper>   r=      zvalues must be a 1D list-likezmask must be a 1D list-likefloat64)$hasattrr   r>   r)   r   
startswithlower
issubclasstyper   r3   r1   KeyError
ValueErrorr$   _data_maskastyper.   r=   arrayr   r   Zinfer_dtyperB   lenfillnan	TypeErrorr
   floatr   r   AssertionErrorndimany)valuesr>   maskr=   errZinferred_typer   r   r    coerce_to_arrayU   s^    
"








r]   c                      s  e Zd ZdZdZeddddZd.ddd	d
 fddZedddd	d dddZ	edddd	d dddZ
ddddZd/d	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 )0r$   a  
    Array of floating (optional missing) values.

    .. versionadded:: 1.2.0

    .. warning::

       FloatingArray is currently experimental, and its API or internal
       implementation may change without warning. Especially the behaviour
       regarding NaN (distinct from NA missing values) is subject to change.

    We represent a FloatingArray with 2 numpy arrays:

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

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

    See :ref:`integer_na` for more.

    Parameters
    ----------
    values : numpy.ndarray
        A 1-d float-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
    -------
    FloatingArray

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

    >>> pd.array([0.1, None, 0.3], dtype=pd.Float32Dtype())
    <FloatingArray>
    [0.1, <NA>, 0.3]
    Length: 3, dtype: Float32

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

    >>> pd.array([0.1, None, 0.3], dtype="Float32")
    <FloatingArray>
    [0.1, <NA>, 0.3]
    Length: 3, dtype: Float32
    g        r   r   c                 C  s   t t| jj S r(   )r3   r   rN   r>   r   r   r   r    r>      s    zFloatingArray.dtypeFz
np.ndarrayr"   )rZ   r[   r=   c                   s6   t |tjr|jjdks tdt j|||d d S )NfzJvalues should be floating numpy array. Use the 'pd.array' function insteadr?   )r)   r1   Zndarrayr>   kindrU   super__init__)r   rZ   r[   r=   	__class__r   r    ra      s
    zFloatingArray.__init__NrD   r<   c                C  s   t |||d\}}t||S )NrD   )r]   r$   )r%   scalarsr>   r=   rZ   r[   r   r   r    _from_sequence  s    zFloatingArray._from_sequencec                C  s   t |dd}| j|||dS )Nraise)errorsrD   )r   re   )r%   stringsr>   r=   rd   r   r   r    _from_sequence_of_strings  s    z'FloatingArray._from_sequence_of_stringsr;   c                 C  s   t || jdS )Nr>   )r]   r>   )r   valuer   r   r    _coerce_to_array  s    zFloatingArray._coerce_to_arrayTr   c                   st   t |}t|tr"t j||dS t|r6dtji}nt|rNdt	di}ni }| j
f d|i|}t||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, or BooleanArray, IntegerArray or FloatingArray with
            'dtype' for its dtype.

        Raises
        ------
        TypeError
            if incompatible type with an FloatingDtype, equivalent of same_kind
            casting
        r?   Zna_valueZNaTr>   F)r   r)   r   r`   rP   r   r1   rT   r   Z
datetime64Zto_numpyr	   )r   r>   r=   kwargsdatarb   r   r    rP     s    
zFloatingArray.astypec                 C  s   | j S r(   )rN   r   r   r   r    _values_for_argsort@  s    z!FloatingArray._values_for_argsortc              
   C  s   ddl m}m} d }t|||tfr4|j|j }}n$t|rXt	|}|j
dkrXt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   )BooleanArrayIntegerArrayrE   z(can only perform ops with 1-d structuresr"   rj   ignoreZelementwise)r0   __)Zpandas.arraysrp   rq   r)   r$   rN   rO   r   r1   ZasarrayrX   NotImplementedError
libmissingZNAzerosshapeZoneswarningscatch_warningsfilterwarningsFutureWarningZerrstategetattrr5   NotImplementedr   r=   )r   otheroprp   rq   r[   resultmethodr   r   r    _cmp_methodC  s,    





zFloatingArray._cmp_methodr   rA   	min_countc                  s   t d| t jd||dS )Nr   sumr   )nvZvalidate_sumr`   _reducer   rA   r   rm   rb   r   r    r   q  s    zFloatingArray.sumc                  s   t d| t jd||dS )Nr   prodr   )r   Zvalidate_prodr`   r   r   rb   r   r    r   u  s    zFloatingArray.prodr@   c                  s   t d| t jd|dS )Nr   minr@   )r   Zvalidate_minr`   r   r   rA   rm   rb   r   r    r   y  s    zFloatingArray.minc                  s   t d| t jd|dS )Nr   maxr@   )r   Zvalidate_maxr`   r   r   rb   r   r    r   }  s    zFloatingArray.maxr   )op_namec                 C  s   t | ||ddS )z
        Parameters
        ----------
        result : array-like
        mask : array-like bool
        other : scalar or array-like
        op_name : str
        Fr?   )rK   )r   r   r[   r~   r   r   r   r    _maybe_mask_result  s    z FloatingArray._maybe_mask_result)F)T)r5   r6   r7   r8   Z_internal_fill_valuer   r>   ra   r:   re   ri   rl   rP   ro   r   r   r   r   r   r   __classcell__r   r   rb   r    r$      s,   <  ..r$   z
An ExtensionDtype for {dtype} data.

This dtype uses ``pd.NA`` as missing value indicator.

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

Methods
-------
None
c                   @  s"   e Zd ZejZdZejddZ	dS )Float32DtypeZFloat32float32rj   N)
r5   r6   r7   r1   r   rK   r   _dtype_docstringformatr8   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 )Float64DtypeZFloat64rF   rj   N)
r5   r6   r7   r1   rF   rK   r   r   r   r8   r   r   r   r    r     s   r   )r   rF   )NNF).
__future__r   rx   Znumpyr1   Zpandas._libsr   r   ru   Zpandas._typingr   r   Zpandas.compat.numpyr   r   Zpandas.util._decoratorsr   Zpandas.core.dtypes.castr	   Zpandas.core.dtypes.commonr
   r   r   r   r   r   r   Zpandas.core.dtypes.dtypesr   r   Zpandas.core.dtypes.missingr   Zpandas.core.arrays.numericr   r   Zpandas.core.opsr   Zpandas.core.tools.numericr   r   r]   r$   r   r   r   r3   r   r   r   r    <module>   s8   $	,     a b