U
    sVc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)   Ztwo      @)r
   r   from_tuplestmassert_index_equal)selfr   r   valuesresultexpected r%   G/tmp/pip-unpacked-wheel-xj8nt62q/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valsr/   c                 C   s<   |rt |td}nt |}t|t ks*t|jtks8td S Nr/   )r
   booltypeAssertionErrorr(   r!   r1   r2   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   r/   )r   ranger
   object
isinstancer6   )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   r6   )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rC   rB   )r   listr
   r>   r   r6   )r!   rngrC   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)reasonr/   )r>   r   r   r(   r6   insertr   r
   pytestmarkZxfailnodeZ
add_markerDeprecationWarningr   Zassert_produces_warningr    npr   r=   )r!   rL   klassr(   ZctorZnulls_fixturerequestr$   datawarnrR   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 )NrN   r/   )rU   
datetime64timedelta64r
   r=   r   r    r   r!   r[   rX   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\   r/   )	r   rU   r]   r	   r   r    r
   r   r=   r_   r%   r%   r&   (test_constructor_datetime_and_datetime64   s    zFTestIndexConstructorInference.test_constructor_datetime_and_datetime64)__name__
__module____qualname__rQ   rR   parametrizerU   nanrI   tupleiterr'   intr0   r   r4   r9   r@   rE   rK   r	   r]   r   r^   rZ   r`   rf   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   r/   )r   r
   r=   r(   r6   )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(   r6   )r!   r(   rJ   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,   r/   )r   r
   r(   r6   )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,   rC   z
Asia/Tokyor;   r/   )
r   r   r
   r(   r   r    Ztz_localizer   from_breaksr<   )r!   dtirt   r#   dti2Z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 )Nrv   r,   rw   rr   r/   z
US/Pacific)rC   tz)r   r
   r   r   r    )r!   ry   r#   r$   rz   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 )Nrv   r,   rG   rH   rr   r/   )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;   rw   rr   r/   )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 )Nrv   r,   rw   rr   r/   )r   r   rx   r
   r   r   r    )r!   ry   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 )Nrv   r,   rw   z	Period[D]r/   rG   )r   r
   Z	to_periodr   r    )r!   ry   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matchr/   )rU   rk   rQ   raises
ValueErrorr
   )r!   r(   rX   msgr%   r%   r&   %test_constructor_int_dtype_nan_raises   s    z7TestDtypeEnforced.test_constructor_int_dtype_nan_raisesr2   r   r+   r,   r/   r-   r.   r   c                 C   s   t |td}t|tstd S r3   )r
   rn   r>   r   r6   r!   r2   r8   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 r3   )r
   floatr>   r   r6   r   r%   r%   r&   "test_constructor_dtypes_to_float64  s    z4TestDtypeEnforced.test_constructor_dtypes_to_float64z
2011-01-01z
2011-01-02rM   i  c                 C   s   t |dd}t|tstd S )Nrr   r/   )r
   r>   r   r6   r   r%   r%   r&   &test_constructor_dtypes_to_categorical  s    
z8TestDtypeEnforced.test_constructor_dtypes_to_categoricalr1   TFc                 C   sH   |r.t |td}t|t st|jtksDtnt |}t|tsDtd S r3   )r
   r=   r>   r6   r(   r	   r7   r%   r%   r&   #test_constructor_dtypes_to_datetime"  s    	z5TestDtypeEnforced.test_constructor_dtypes_to_datetimerG   c                 C   sH   |r.t |td}t|t st|jtksDtnt |}t|tsDtd S r3   )r
   r=   r>   r6   r(   r   r7   r%   r%   r&   $test_constructor_dtypes_to_timedelta3  s    	z6TestDtypeEnforced.test_constructor_dtypes_to_timedeltaN)rg   rh   ri   rq   rQ   rR   rj   r=   rs   ru   r|   r~   r   r   r   r   r   rU   r   rn   r   r   r   r   r   r
   r]   r   r^   r   r   r%   r%   r%   r&   ro      st   
	




	
	ro   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 )TestIndexConstructorUnwrappingrV   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!   rV   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,   rw   c                 C   s2   G dd d}t |}t ||}t|| d S )Nc                   @   s,   e Zd ZddddZdejdddZdS )zOTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLikeN)returnc                 S   s
   || _ d S Nr   )r!   r   r%   r%   r&   __init__c  s    zXTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLike.__init__c                 S   s   | j S r   r   )r!   r(   r%   r%   r&   	__array__f  s    zYTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLike.__array__)N)rg   rh   ri   r   rU   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)rg   rh   ri   rQ   rR   rj   r
   r	   r   r   rU   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)   r/   )rQ   r   OverflowErrorr
   rU   Ziinfor*   max)r!   r   r%   r%   r&   test_constructor_overflow_int64o  s    z;TestIndexConstructionErrors.test_constructor_overflow_int64N)rg   rh   ri   r   r%   r%   r%   r&   r   n  s   r   )&__doc__r   r   decimalr   ZnumpyrU   rQ   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   ro   r   r   r%   r%   r%   r&   <module>   s   Hw &)