U
    Mf/?                     @  s(  d dl mZ d dlmZmZ d dl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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mZm Z m!Z!m"Z" d d
l#m$Z$ d dl%m&Z&m'Z' G dd de&Z(i Z)de)d< G dd de(Z*G dd de*Z+G dd de*Z,G dd de(Z-dS )    )annotations)CallableHashableN)indexlib)DtypeDtypeObjnpt)cache_readonlydoc)find_stack_level)astype_nansafe)is_dtype_equalis_extension_array_dtypeis_floatis_float_dtypeis_integer_dtypeis_numeric_dtype	is_scalaris_signed_integer_dtypeis_unsigned_integer_dtypeneeds_i8_conversionpandas_dtype)	ABCSeries)Indexmaybe_extract_namec                      sj  e Zd ZU dZdZded< dZded< edfZd	ed
< dZ	dZ
dZded< eddddZeejejeejejeejejeejejeejejeejejeejejeejej eej!ej"eej#ej$i
Z%ded< e&dd Z'eddddZ(dFddddZ)e*ddddZ+e*dd d!d"d#Z,e*ddd!d$d%Z-dd fd&d'Z.e/e0j1dGdd fd(d)Z1ee/e0j2ddd*d+Z2e/e0j3d,dd- fd.d/Z3e/e0j4e5j6fdd0d1d2Z4e/e0j7e5j6fd3d4 fd5d6Z7 fd7d8Z8d9dd!d:d;Z9e*ddd d<d=d>Z:e&ddd?d@Z;dAddBddC fdDdE
Z<  Z=S )HNumericIndexa  
    Immutable sequence used for indexing and alignment. The basic object
    storing axis labels for all pandas objects. NumericIndex is a special case
    of `Index` with purely numpy int/uint/float labels.

    .. versionadded:: 1.4.0

    Parameters
    ----------
    data : array-like (1-dimensional)
    dtype : NumPy dtype (default: None)
    copy : bool
        Make a copy of input ndarray.
    name : object
        Name to be stored in the index.

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

    Methods
    ----------
    None

    See Also
    --------
    Index : The base pandas Index type.
    Int64Index : Index of purely int64 labels (deprecated).
    UInt64Index : Index of purely uint64 labels (deprecated).
    Float64Index : Index of  purely float64 labels (deprecated).

    Notes
    -----
    An NumericIndex instance can **only** contain numpy int64/32/16/8, uint64/32/16/8 or
    float64/32/16 dtype. In particular, ``NumericIndex`` *can not* hold Pandas numeric
    dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
    Znumericindexz
np.ndarray_valuesNznp.dtype | None_default_dtypeznumeric typeztuple[Callable[..., bool], str]_dtype_validation_metadataTFbool(_is_backward_compat_public_numeric_indexreturnc                 C  s   t | jrdS dS d S )NTF)r   dtypeself r'   ?/tmp/pip-unpacked-wheel-eb6vo0j3/pandas/core/indexes/numeric.py_can_hold_nac   s    
zNumericIndex._can_hold_naz*dict[np.dtype, type[libindex.IndexEngine]]_engine_typesc                 C  s   | j | j S N)r*   r$   r%   r'   r'   r(   _engine_typew   s    zNumericIndex._engine_typestrc                 C  s   dddd| j j S )NintegerZfloating)iuf)r$   kindr%   r'   r'   r(   inferred_type}   s    zNumericIndex.inferred_typezDtype | Noner$   c                 C  s(   t ||| }| |||}| j||dS )Nname)r   _ensure_array_simple_new)clsdatar$   copyr6   subarrr'   r'   r(   __new__   s    zNumericIndex.__new__r;   c              	   C  sB  |  | t|tjtfst|r,| |t|ttt	fsDt|}|}tj
||d}|dkr|jjdkr| tkr|dk rtj
|tjd}t|jjtr| | | |}|st|j|sz tj|||d}|  |j W n* ttfk
r   td| j Y nX | || n|}|jdkr4tdt
|}|S )	zF
        Ensure we have a valid array to pass to _simple_new.
        r4   Nr1   r   r$   r;   zdata is not compatible with    z Index data must be 1-dimensional)_validate_dtype
isinstancenpZndarrayr   r   Z_scalar_data_errorr   listtupleZasarrayr$   r2   UInt64Indexalluint64
issubclasstyper-   Z_string_data_error_ensure_dtyper   array	TypeError
ValueError__name___assert_safe_castingndim)r9   r:   r$   r;   origr<   r'   r'   r(   r7      s4    




zNumericIndex._ensure_arrayNone)r$   r#   c                 C  s6   |d krd S | j \}}||s2td| d| d S )Nz#Incorrect `dtype` passed: expected z, received )r   rN   )r9   r$   Zvalidation_funcexpectedr'   r'   r(   rA      s    
zNumericIndex._validate_dtypec                 C  s:   |dkr| j S t|}t|tjs&t| jr0|S | j S dS )z
        Ensure int64 dtype for Int64Index etc. but allow int32 etc. for NumericIndex.

        Assumes dtype has already been validated.
        N)r   r   rB   rC   r$   AssertionErrorr!   )r9   r$   r'   r'   r(   rK      s    zNumericIndex._ensure_dtypec              
     sf   t | jst |S t| z&t|r:t||kr:W dS || jkW S  tt	t
fk
r`   Y dS X dS )zU
        Check if key is a float and has a decimal. If it has, return False.
        FN)r   r$   super__contains__hashr   intZ_engineOverflowErrorrM   rN   )r&   key	__class__r'   r(   rW      s    
zNumericIndex.__contains__c                   s   t |}t| jrtt|r,td| dqt|rt|st| j|d}t	| t
rbt|| jdS t|| j|dS n&| jrt|st|r| j| ||dS t j||dS )Nz%Cannot convert Float64Index to dtype z,; integer values are required for conversionr4   r5   )r6   r$   r?   r>   )r   r   r$   r   rM   r   r   r   r   rB   Float64Index
Int64Indexr6   r   r!   r   Z_constructorrV   astype)r&   r$   r;   Zarrr\   r'   r(   r`      s    


zNumericIndex.astypec                 C  s   dS )NFr'   r%   r'   r'   r(   _should_fallback_to_positional	  s    z+NumericIndex._should_fallback_to_positionalslice)r[   r2   c                   s:   t | jr*|dkst| |j|j|jS t j||dS )N)locgetitem)r2   )	r   r$   rU   Zslice_indexerstartstopsteprV   _convert_slice_indexer)r&   r[   r2   r\   r'   r(   rh     s    
z#NumericIndex._convert_slice_indexer)sidec                 C  s.   |ddd t jfkst| |dd | |S )Nrc   rd   r2   _maybe_cast_slice_bound)r   
no_defaultrU   Z_deprecated_argZ_maybe_cast_indexer)r&   labelri   r2   r'   r'   r(   rj     s    z$NumericIndex._maybe_cast_slice_boundr   r5   c                   sD   | j s4|jjdkr4|tjkr"| jn|}tj||dS t j	||dS )Nr1   r5   )valuesr6   )
r)   r$   r2   r   rk   _namer^   r8   rV   _shallow_copy)r&   rm   r6   r\   r'   r(   ro   #  s    zNumericIndex._shallow_copyc                   sb   t  ||}t|jtjs^|jdkr@tdt| j	 dntdt| j	 dt
| |S )Nr   ztolerance argument for z1 must contain numeric elements if it is list typez$ must be numeric if it is a scalar: )rV   _convert_tolerancerC   Z
issubdtyper$   numberrQ   rN   rJ   rO   repr)r&   Z	tolerancetargetr\   r'   r(   rp   +  s    
zNumericIndex._convert_tolerancer   c                 C  s   t |S r+   )r   )r&   r$   r'   r'   r(   _is_comparable_dtype;  s    z!NumericIndex._is_comparable_dtype)r:   r<   r#   c                 C  s"   t |jrt||stddS )z
        Ensure incoming data can be represented with matching signed-ness.

        Needed if the process of casting data from some accepted dtype to the internal
        dtype(s) bears the risk of truncation (e.g. float to int).
        z.Unsafe NumPy casting, you must explicitly castN)r   r$   rC   Zarray_equalrM   )r9   r:   r<   r'   r'   r(   rP   ?  s    
z!NumericIndex._assert_safe_castingc                 C  s   dS )zB
        Checks that all the labels are datetime objects.
        Fr'   r%   r'   r'   r(   _is_all_datesK  s    zNumericIndex._is_all_dates .na_repfloat_formatdecimalquotingc                  sP   ddl m} t| jr4|| j||||dd}| S t jf ||||d|S )Nr   )FloatArrayFormatterF)ry   rz   r{   r|   Zfixed_widthrx   )Zpandas.io.formats.formatr}   r   r$   r   Zget_result_as_arrayrV   _format_native_types)r&   ry   rz   r{   r|   kwargsr}   	formatterr\   r'   r(   r~   R  s&    
z!NumericIndex._format_native_types)NNFN)T)>rO   
__module____qualname____doc___typ__annotations__r   r   r   Z_is_numeric_dtypeZ_can_hold_stringsr!   r
   r)   rC   r$   Zint8libindexZ
Int8EngineZint16ZInt16EngineZint32ZInt32Engineint64Int64EngineZuint8ZUInt8EngineZuint16ZUInt16EngineZuint32ZUInt32EnginerH   UInt64EngineZfloat32ZFloat32Enginefloat64Float64Enginer*   propertyr,   r3   r=   classmethodr7   rA   rK   rW   r   r   r`   ra   rh   rj   r   rk   ro   rp   rt   rP   ru   r~   __classcell__r'   r'   r\   r(   r   0   s   
&
 
 
 
 
 
 
 
 
 
 
0

	   r   a  
    Immutable sequence used for indexing and alignment. The basic object
    storing axis labels for all pandas objects. %(klass)s is a special case
    of `Index` with purely %(ltype)s labels. %(extra)s.

    .. deprecated:: 1.4.0
        In pandas v2.0 %(klass)s will be removed and :class:`NumericIndex` used instead.
        %(klass)s will remain fully functional for the duration of pandas 1.x.

    Parameters
    ----------
    data : array-like (1-dimensional)
    dtype : NumPy dtype (default: %(dtype)s)
    copy : bool
        Make a copy of input ndarray.
    name : object
        Name to be stored in the index.

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

    Methods
    ----------
    None

    See Also
    --------
    Index : The base pandas Index type.
    NumericIndex : Index of numpy int/uint/float data.

    Notes
    -----
    An Index instance can **only** contain hashable objects.
class_descrc                      s@   e Zd ZU dZdZded< eddddZ fd	d
Z  Z	S )IntegerIndexz@
    This is an abstract class for Int64Index, UInt64Index.
    Fr    r!   znpt.NDArray[np.int64]r"   c                 C  s    t jdtt d | j| jS )NzAIndex.asi8 is deprecated and will be removed in a future version.)
stacklevel)warningswarnFutureWarningr   r   viewr   r%   r'   r'   r(   asi8  s    zIntegerIndex.asi8c                   sD   t  | t|dr@t|jr@|| j}||k r<|S t|S )Nr$   )rV   _validate_fill_valuehasattrr   r$   r`   rG   rM   )r&   valueZ	convertedr\   r'   r(   r     s    z!IntegerIndex._validate_fill_value)
rO   r   r   r   r!   r   r   r   r   r   r'   r'   r\   r(   r     s
   
	r   c                   @  sD   e Zd Zd ddddZed e ZdZejZ	e
e
jZedfZdS )	r_   r.   r   rv   klassltyper$   extrar   Z
int64indexzsigned integerN)rO   r   r   _index_descr_args_num_index_shared_docsr   r   r   r   r,   rC   r$   r   r   r   r   r'   r'   r'   r(   r_     s   r_   c                      sT   e Zd Zd ddddZed e ZdZejZ	e
e
jZedfZ fddZ  ZS )	rF   zunsigned integerrH   rv   r   r   Zuint64indexc                   s@   t  | t|dr<t|jr<|dk r8|| jS t|S )Nr$   r   )rV   r   r   r   r$   rG   r`   rM   )r&   r   r\   r'   r(   r     s    z UInt64Index._validate_fill_value)rO   r   r   r   r   r   r   r   r   r,   rC   r$   rH   r   r   r   r   r   r'   r'   r\   r(   rF     s   rF   c                   @  sR   e Zd ZU d ddddZed e ZdZejZ	e
e
jZedfZdZded	< d
S )r^   r   floatrv   )r   r$   r   r   r   Zfloat64indexFr    r!   N)rO   r   r   r   r   r   r   r   r   r,   rC   r$   r   r   r   r   r!   r   r'   r'   r'   r(   r^     s   
r^   ).
__future__r   typingr   r   r   ZnumpyrC   Zpandas._libsr   r   r   Zpandas._typingr   r   r	   Zpandas.util._decoratorsr
   r   Zpandas.util._exceptionsr   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr   r   r   r   r   r   r   r   r   r   r   Zpandas.core.dtypes.genericr   Zpandas.core.indexes.baser   r   r   r   r   r_   rF   r^   r'   r'   r'   r(   <module>   s.   4  ='