U
    c                      @  s   d dl mZ d dlZd dlmZmZmZ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 d dlmZ d dlmZmZmZmZmZmZ d d	lmZm Z  erd dl!Z!ed
ddZ"G dd de Z#dd Z$G dd deZ%dS )    )annotationsN)TYPE_CHECKINGAnyCallableMappingTypeVar)libmissing)DtypeDtypeObjnptAbstractMethodError)cache_readonly)is_bool_dtypeis_float_dtypeis_integer_dtypeis_object_dtypeis_string_dtypepandas_dtype)BaseMaskedArrayBaseMaskedDtypeTNumericArray)boundc                   @  s   e Zd ZU ded< ded< 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
eddddZedd dddZeddd	ddddZd S )!NumericDtypeznp.dtype_default_np_dtypezCallable[[Any], bool]_checkerstrreturnc                 C  s   | j  dS )NzDtype())nameself r$   >/tmp/pip-unpacked-wheel-g7fro6k3/pandas/core/arrays/numeric.py__repr__3   s    zNumericDtype.__repr__boolc                 C  s
   | j dkS )Nikindr"   r$   r$   r%   is_signed_integer6   s    zNumericDtype.is_signed_integerc                 C  s
   | j dkS )Nur)   r"   r$   r$   r%   is_unsigned_integer:   s    z NumericDtype.is_unsigned_integerc                 C  s   dS )NTr$   r"   r$   r$   r%   _is_numeric>   s    zNumericDtype._is_numericz$pyarrow.Array | pyarrow.ChunkedArrayr   )arrayr    c                 C  s  ddl }ddlm} |  }|| j}|j|snt|j }|j	dkrdt
d|  d|j d||}t||jr|g}n|j}g }|D ]4}	||	| jd\}
}||
 | d	d
}|| q|s|tjg | jdtjg tjdS t|dkr|d S ||S dS )zW
        Construct IntegerArray/FloatingArray from pyarrow Array/ChunkedArray.
        r   N)pyarrow_array_to_numpy_and_mask)r(   r,   fzExpected array of z type, got z instead)dtypeFcopy   )pyarrowZ%pandas.core.arrays.arrow._arrow_utilsr0   construct_array_typeZfrom_numpy_dtypetypeequalsr   Zto_pandas_dtyper*   	TypeErrorcast
isinstanceArraychunksnumpy_dtyper4   appendnpr/   Zbool_lenZ_concat_same_type)r#   r/   r6   r0   Zarray_classZpyarrow_typeZrt_dtyper>   resultsZarrdatamaskZnum_arrr$   r$   r%   __from_arrow__B   s6    

 zNumericDtype.__from_arrow__zMapping[str, NumericDtype]c                 C  s   t | d S Nr   )clsr$   r$   r%   _str_to_dtype_mappingt   s    z"NumericDtype._str_to_dtype_mappingzNumericDtype | str | np.dtype)r2   r    c              
   C  s|   t |tr|dr| }t |tsx|  }z|tt| }W n2 tk
rv } zt	d| |W 5 d}~X Y nX |S )zS
        Convert a string representation or a numpy dtype to NumericDtype.
        )ZIntZUIntZFloatzinvalid dtype specified N)
r<   r   
startswithlowerr   rI   rA   r2   KeyError
ValueError)rH   r2   mappingerrr$   r$   r%   _standardize_dtypex   s    
"zNumericDtype._standardize_dtype
np.ndarray)valuesr2   r4   r    c                 C  s   t | dS )z{
        Safely cast the values to the given dtype.

        "safe" in this context means the casting is lossless.
        Nr   )rH   rR   r2   r4   r$   r$   r%   
_safe_cast   s    zNumericDtype._safe_castN)__name__
__module____qualname____annotations__r&   r   r+   r-   propertyr.   rF   classmethodrI   rP   rS   r$   r$   r$   r%   r   /   s    
2r   c           
      C  s  |j }d }|d kr,t| dr,|| jr,| j}|d k	r>||}| }t| |r| j| j } }|d k	rv| j|j	dd} |r| 
 } |
 }| |||fS tj| |d} d }t| jst| jrtj| dd}|dkrn*|dkr\|jd}	t| j d	|	 nZt| r(||r(tj| ||d
} n4t| s\t| s\|jd}	t| j d	|	 | jdkrptd|d krt| }nt|t| kst|jdkrtd|d kr|}n|j}| r| 
 } |j| |< |dkr| j||d} n|j| |dd} | |||fS )Nr2   Fr3   T)Zskipnaemptyboolean_z cannot be converted to r2   r4   r5   zvalues must be a 1D list-likezmask must be a 1D list-like)stringunicode) r   hasattrr2   rP   r7   r<   _dataZ_maskZastyper?   r4   rA   r/   r   r   r   Zinfer_dtyperT   stripr:   r   r   r   ndim
libmissingZis_numeric_narB   AssertionErrorr8   anyZ_internal_fill_valuerS   )
rR   rE   r2   r4   	dtype_clsdefault_dtypecheckerZinferred_typerH   r!   r$   r$   r%   _coerce_to_data_and_mask   s^    








rj   c                      s   e Zd ZU dZded< dddddd	 fd
dZeddddZeddddddddZ	eddddddddddZ
ejejfZ  ZS )r   z8
    Base class for IntegerArray and FloatingArray.
    ztype[NumericDtype]
_dtype_clsFrQ   znpt.NDArray[np.bool_]r'   None)rR   rE   r4   r    c                   sl   | j j}t|tjr||jsB| j jdkr.dnd}td| d|jtjkrVtdt	 j
|||d d S )Nr1   Zfloatingintegerzvalues should be z1 numpy array. Use the 'pd.array' function insteadz0FloatingArray does not support np.float16 dtype.r3   )rk   r   r<   rA   ndarrayr2   r*   r:   Zfloat16super__init__)r#   rR   rE   r4   ri   descr	__class__r$   r%   rp      s    

zNumericArray.__init__r   r   c                 C  s   | j  }|t| jj S rG   )rk   rI   r   ra   r2   )r#   rN   r$   r$   r%   r2      s    
zNumericArray.dtyper3   r   ztuple[np.ndarray, np.ndarray])r2   r4   r    c          	      C  s2   | j }|j}d }t||||||\}}}}||fS rG   )rk   r   rj   )	rH   valuer2   r4   rg   rh   rE   rR   r\   r$   r$   r%   _coerce_to_array   s         zNumericArray._coerce_to_arrayNr]   ztype[T]zDtype | Noner   )rH   r2   r4   r    c                C  s(   ddl m} ||dd}| j|||dS )Nr   )
to_numericraise)errorsr]   )Zpandas.core.tools.numericrv   Z_from_sequence)rH   stringsr2   r4   rv   Zscalarsr$   r$   r%   _from_sequence_of_strings  s    z&NumericArray._from_sequence_of_strings)F)rT   rU   rV   __doc__rW   rp   r   r2   rY   ru   rz   rA   rn   numbersNumberZ_HANDLED_TYPES__classcell__r$   r$   rr   r%   r      s   
  )&
__future__r   r|   typingr   r   r   r   r   ZnumpyrA   Zpandas._libsr   r	   rd   Zpandas._typingr
   r   r   Zpandas.errorsr   Zpandas.util._decoratorsr   Zpandas.core.dtypes.commonr   r   r   r   r   r   Zpandas.core.arrays.maskedr   r   r6   r   r   rj   r   r$   r$   r$   r%   <module>   s    	eG