U
    +if1                     @   s   d Z ddlmZmZ ddlmZ ddlZddl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mZmZmZmZmZ ddlmZ ddlmZm Z m!Z! G dd	 d	Z"G d
d dZ#G dd dZ$G dd dZ%dS )z7
Tests for the Index constructor conducting inference.
    )datetime	timedelta)DecimalN)is_unsigned_integer_dtype)NACategoricalCategoricalIndexDatetimeIndexIndexIntervalIndex
MultiIndexNaTPeriodIndexSeriesTimedeltaIndex	Timestamparray
date_rangeperiod_rangetimedelta_range)Float64Index
Int64IndexUInt64Indexc                   @   s6  e Zd Zejddejgejdee	e
gdd Zejdeddd	d
ddddg	dd Zejdddgejddddgejdddgedgdd Zdd Zdd Zdd Zejddd gejd!ed"ed#fed$ed#fgd%d& Zejd'ddgd(d) Zejd'ddgd*d+ ZdS ),TestIndexConstructorInferencena_valueNvtypec                 C   s2   dd|fg}t ||}t|}t|| d S )N)   two      @)r
   r   from_tuplestmassert_index_equal)selfr   r   valuesresultexpected r&   V/home/mars/bis/venv/lib/python3.8/site-packages/pandas/tests/indexes/test_index_new.py!test_construction_list_tuples_nan*   s    
z?TestIndexConstructorInference.test_construction_list_tuples_nandtypeint64Zint32Zint16Zint8uint64Zuint32Zuint16Zuint8c                 C   sF   t |rt}nt}|ddddg}tddddg|d	}t|| d S )
Nr   r         g              ?       @r   r)   )r   r   r   r
   r    r!   )r"   r)   Z
index_typer%   r$   r&   r&   r'    test_constructor_int_dtype_float3   s    z>TestIndexConstructorInference.test_constructor_int_dtype_float
cast_indexTFvalsr0   c                 C   s<   |rt |td}nt |}t|t ks*t|jtks8td S Nr0   )r
   booltypeAssertionErrorr)   objectr"   r2   r3   indexr&   r&   r'   !test_constructor_dtypes_to_objectB   s
    z?TestIndexConstructorInference.test_constructor_dtypes_to_objectc                 C   s*   t td}t|td}t|t r&td S N   r0   )r   ranger
   r8   
isinstancer7   )r"   cir$   r&   r&   r'   &test_constructor_categorical_to_objectO   s    zDTestIndexConstructorInference.test_constructor_categorical_to_objectc                 C   s4   t dddd}t|}t|| t|ts0td S )Nz2012-1-1Mr-   )freqperiods)r   r
   r    r!   r?   r   r7   )r"   Zxprsr&   r&   r'   "test_constructor_infer_periodindexU   s    z@TestIndexConstructorInference.test_constructor_infer_periodindexc                 C   s0   t dddd}t|}t|}t|ts,td S )Nz1/1/2000   DrD   rC   )r   listr
   r?   r   r7   )r"   rngrD   r$   r&   r&   r'   test_from_list_of_periods[   s    z7TestIndexConstructorInference.test_from_list_of_periodsposr   r   zklass,dtype,ctordatetime64[ns]natztimedelta64[ns]c              	   C   s   t |trd S |ttg}|j|ks(t|g}||| d }	|tkrptttg}tj	j
dd}
|j|
 t}	t|}t|	 t|| W 5 Q R X ttj|td}t|	 t|| W 5 Q R X d S )Nz%Broken with np.NaT ctor; see GH 31884)reasonr0   )r?   r   r   r)   r7   insertr   r
   pytestmarkZxfailnodeZ
add_markerDeprecationWarningr    Zassert_produces_warningr!   npr   r8   )r"   rM   klassr)   ZctorZnulls_fixturerequestr%   datawarnrS   r$   r&   r&   r'   "test_constructor_infer_nat_dt_likeb   s$    
z@TestIndexConstructorInference.test_constructor_infer_nat_dt_like	swap_objsc                 C   s`   t dt dg}|r&|d d d }t|td}tt|| ttt j|td| d S )NrO   r0   )rV   
datetime64timedelta64r
   r8   r    r!   r   r"   r\   rY   r%   r&   r&   r'   -test_constructor_mixed_nat_objs_infers_object   s    zKTestIndexConstructorInference.test_constructor_mixed_nat_objs_infers_objectc                 C   sb   t dddddtdg}|r,|d d d }t|}tt|| tttj|td| d S )	Ni        	   *   nowr]   r0   )	r   rV   r^   r	   r    r!   r
   r   r8   r`   r&   r&   r'   (test_constructor_datetime_and_datetime64   s    zFTestIndexConstructorInference.test_constructor_datetime_and_datetime64)__name__
__module____qualname__rR   rS   parametrizerV   nanrJ   tupleiterr(   intr1   r   r5   r;   rA   rF   rL   r	   r^   r   r_   r[   ra   rg   r&   r&   r&   r'   r   )   s:   
 	 

r   c                   @   sP  e Zd Zdd Zejdedddgdd Zejdedddgd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zejdddgdd Zejddddgedddgejdddgeddd d!gejdd d!gedgd"d# Zejddddgdd d!gedd d!gejdddgedejdd d!gedgd$d% Zejddddgejdddgedejd&d'gd(ded)dded)ddggd*d+ Zejd,d-d.gejdeeed&ed'geed)dded)ddggd/d0 Zejd,d-d.gejdeedd1edd1gededggd2d3 Zd4S )5TestDtypeEnforcedc                 C   s,   t dg|d}t|td}|jtks(td S )Nr   r0   )r   r
   r8   r)   r7   )r"   Zany_numeric_ea_dtypeZarridxr&   r&   r'   *test_constructor_object_dtype_with_ea_data   s    z<TestDtypeEnforced.test_constructor_object_dtype_with_ea_datar)   Zfloat64r+   categoryc                 C   sH   t td}t ||d}|j|ks&tt td|d}|j|ksDtd S r<   )r
   r>   r)   r7   )r"   r)   rK   r$   r&   r&   r'   .test_constructor_range_values_mismatched_dtype   s
    z@TestDtypeEnforced.test_constructor_range_values_mismatched_dtypec                 C   s,   t dddg}t||d}|j|ks(td S )Nr   r,   r-   r0   )r   r
   r)   r7   )r"   r)   catr$   r&   r&   r'   ;test_constructor_categorical_values_mismatched_non_ea_dtype   s    zMTestDtypeEnforced.test_constructor_categorical_values_mismatched_non_ea_dtypec                 C   s   t ddd}t|}t||j}t|| |d}t|}t||j}t|| tt	d}t|}t||jd}t|| d S )N
2016-01-01r-   rD   z
Asia/Tokyor=   r0   )
r   r   r
   r)   r    r!   Ztz_localizer   from_breaksr>   )r"   dtiru   r$   dti2cat2iiZcat3r&   r&   r'   4test_constructor_categorical_values_mismatched_dtype   s    
zFTestDtypeEnforced.test_constructor_categorical_values_mismatched_dtypec                 C   s^   t ddd}t|dd}t|}t|| t dddd}t|dd}t|}t|| d S )Nrw   r-   rx   rs   r0   z
US/Pacific)rD   tz)r   r
   r   r    r!   )r"   rz   r$   r%   r{   r&   r&   r'   7test_constructor_ea_values_mismatched_categorical_dtype   s    zITestDtypeEnforced.test_constructor_ea_values_mismatched_categorical_dtypec                 C   s2   t dddd}t|dd}t|}t|| d S )Nrw   r-   rH   rI   rs   r0   )r   r
   r   r    r!   )r"   pir$   r%   r&   r&   r'   /test_constructor_period_values_mismatched_dtype   s    zATestDtypeEnforced.test_constructor_period_values_mismatched_dtypec                 C   s0   t ddd}t|dd}t|}t|| d S )Nz4 Daysr=   rx   rs   r0   )r   r
   r   r    r!   )r"   Ztdir$   r%   r&   r&   r'   4test_constructor_timedelta64_values_mismatched_dtype   s    zFTestDtypeEnforced.test_constructor_timedelta64_values_mismatched_dtypec                 C   s:   t ddd}t|}t|dd}t|}t|| d S )Nrw   r-   rx   rs   r0   )r   r   ry   r
   r   r    r!   )r"   rz   r}   r$   r%   r&   r&   r'   1test_constructor_interval_values_mismatched_dtype   s
    
zCTestDtypeEnforced.test_constructor_interval_values_mismatched_dtypec                 C   s2   t ddd}t|dd}|d}t|| d S )Nrw   r-   rx   z	Period[D]r0   rH   )r   r
   Z	to_periodr    r!   )r"   rz   r$   r%   r&   r&   r'   :test_constructor_datetime64_values_mismatched_period_dtype   s    
zLTestDtypeEnforced.test_constructor_datetime64_values_mismatched_period_dtyper*   c              	   C   s6   t jg}d}tjt|d t||d W 5 Q R X d S )Nzcannot convertmatchr0   )rV   rl   rR   raises
ValueErrorr
   )r"   r)   rY   msgr&   r&   r'   %test_constructor_int_dtype_nan_raises   s    z7TestDtypeEnforced.test_constructor_int_dtype_nan_raisesr3   r   r,   r-   r0   r.   r/   r   c                 C   s   t |td}t|tstd S r4   )r
   ro   r?   r   r7   r"   r3   r:   r&   r&   r'    test_constructor_dtypes_to_int64   s    z2TestDtypeEnforced.test_constructor_dtypes_to_int64c                 C   s   t |td}t|tstd S r4   )r
   floatr?   r   r7   r   r&   r&   r'   "test_constructor_dtypes_to_float64  s    z4TestDtypeEnforced.test_constructor_dtypes_to_float64z
2011-01-01z
2011-01-02rN   i  c                 C   s   t |dd}t|tstd S )Nrs   r0   )r
   r?   r   r7   r   r&   r&   r'   &test_constructor_dtypes_to_categorical  s    
z8TestDtypeEnforced.test_constructor_dtypes_to_categoricalr2   TFc                 C   sH   |r.t |td}t|t st|jtksDtnt |}t|tsDtd S r4   )r
   r8   r?   r7   r)   r	   r9   r&   r&   r'   #test_constructor_dtypes_to_datetime"  s    	z5TestDtypeEnforced.test_constructor_dtypes_to_datetimerH   c                 C   sH   |r.t |td}t|t st|jtksDtnt |}t|tsDtd S r4   )r
   r8   r?   r7   r)   r   r9   r&   r&   r'   $test_constructor_dtypes_to_timedelta3  s    	z6TestDtypeEnforced.test_constructor_dtypes_to_timedeltaN)rh   ri   rj   rr   rR   rS   rk   r8   rt   rv   r~   r   r   r   r   r   r   rV   r   ro   r   r   r   r   r   r
   r^   r   r_   r   r   r&   r&   r&   r'   rp      st   
	




	
	rp   c                	   @   sf   e Zd Zejdeegdd Zdd Z	ejde
de
dd	d
gedddjgdd ZdS )TestIndexConstructorUnwrappingrW   c                 C   s>   t dt dt dg}t|}t|}||}t|| d S )NZ20110101Z20120101Z20130101)r   r	   r   r    r!   )r"   rW   Zstampsr%   serr$   r&   r&   r'   !test_constructor_from_series_dt64H  s
    z@TestIndexConstructorUnwrapping.test_constructor_from_series_dt64c                 C   s6   t dddg}t|j}tdddg}t|| d S )Nr   r,   r-   )r   r
   r   r    r!   )r"   r   r$   r%   r&   r&   r'    test_constructor_no_pandas_arrayP  s    
z?TestIndexConstructorUnwrapping.test_constructor_no_pandas_arrayr   r=   abcz
2000-01-01r-   rx   c                 C   s2   G dd d}t |}t ||}t|| d S )Nc                   @   s&   e Zd Zdd ZdejdddZdS )zOTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLikec                 S   s
   || _ d S Nr   )r"   r   r&   r&   r'   __init__c  s    zXTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLike.__init__N)returnc                 S   s   | j S r   r   )r"   r)   r&   r&   r'   	__array__f  s    zYTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLike.__array__)N)rh   ri   rj   r   rV   Zndarrayr   r&   r&   r&   r'   	ArrayLikeb  s   r   )r
   r    r!   )r"   r   r   r%   r$   r&   r&   r'   test_constructor_ndarray_likeV  s    z<TestIndexConstructorUnwrapping.test_constructor_ndarray_likeN)rh   ri   rj   rR   rS   rk   r
   r	   r   r   rV   Zaranger   r   r#   r   r&   r&   r&   r'   r   E  s   
r   c                   @   s   e Zd Zdd ZdS )TestIndexConstructionErrorsc              	   C   s>   d}t jt|d" tttjjd gdd W 5 Q R X d S )NzIThe elements provided in the data cannot all be casted to the dtype int64r   r   r*   r0   )rR   r   OverflowErrorr
   rV   Ziinfor+   max)r"   r   r&   r&   r'   test_constructor_overflow_int64o  s    z;TestIndexConstructionErrors.test_constructor_overflow_int64N)rh   ri   rj   r   r&   r&   r&   r'   r   n  s   r   )&__doc__r   r   decimalr   numpyrV   rR   Zpandas.core.dtypes.commonr   Zpandasr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Zpandas._testingZ_testingr    Zpandas.core.apir   r   r   r   rp   r   r   r&   r&   r&   r'   <module>   s   Hw &)