U
    >vf5                     @   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	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 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)NACategoricalCategoricalIndexDatetimeIndexIndexIntervalIndex
MultiIndexNaTPeriodIndexSeriesTimedeltaIndex	Timestamparray
date_rangeperiod_rangetimedelta_rangec                   @   sj  e Zd Zdd Zdd Zejdede	j
edgd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d0d1 Zejd/ddgd2d3 ZdS )4TestIndexConstructorInferencec                 C   s>   t jddgtd}t|}|jtks(tt|jtks:td S )NTFdtype)npr   objectr	   r   AssertionErrorr   selfarrres r   G/tmp/pip-unpacked-wheel-vdrwu74i/pandas/tests/indexes/test_index_new.pytest_object_all_bools#   s    z3TestIndexConstructorInference.test_object_all_boolsc                 C   sF   t jtdtdgtd}t|}|jtks0tt|jtksBtd S )N      r   )r   r   complexr   r	   r   r   r   r   r   r   r    test_object_all_complex,   s    z5TestIndexConstructorInference.test_object_all_complexvalNnanc                 C   s   t |g}t|}|jdkr&|  s*tt|d d d }|jdkrR|  sVtttj|td}|jdkr~|  stttj|tdd d d }|jdkr|  std S )Ndatetime64[ns]r   )	r   r	   r   Zisnaallr   r   r   r   )r   r&   valuesidxr   r   r    test_infer_nat5   s    z,TestIndexConstructorInference.test_infer_natna_valuevtypec                 C   s2   dd|fg}t ||}t|}t|| d S )N)r"   Ztwo      @)r	   r   from_tuplestmassert_index_equal)r   r.   r/   r+   resultexpectedr   r   r    !test_construction_list_tuples_nanG   s    
z?TestIndexConstructorInference.test_construction_list_tuples_nanr   int64Zint32Zint16Zint8uint64Zuint32Zuint16Zuint8c                 C   s8   t ddddg|d}t dddd	g|d}t|| d S )
Nr   r"   r#      r   g              ?       @r0   r	   r2   r3   )r   r   r5   r4   r   r   r     test_constructor_int_dtype_floatP   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typer   r   r   r>   r?   indexr   r   r    !test_constructor_dtypes_to_objectZ   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	   r   
isinstancer   )r   cir4   r   r   r    &test_constructor_categorical_to_objectg   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Mr9   )freqperiods)r   r	   r2   r3   rI   r   r   )r   Zxprsr   r   r    "test_constructor_infer_periodindexm   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rN   rM   )r   listr	   rI   r   r   )r   rngrN   r4   r   r   r    test_from_list_of_periodss   s    z7TestIndexConstructorInference.test_from_list_of_periodsposr   r"   zklass,dtype,ctorr(   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   )rI   r   r   r   r   insertr   r	   pytestmarkZxfailnodeZ
add_markerDeprecationWarningr2   Zassert_produces_warningr3   r   r   r   )r   rW   klassr   ZctorZnulls_fixturerequestr5   datawarnr\   r4   r   r   r    "test_constructor_infer_nat_dt_likez   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 )NrX   r)   r   )r   
datetime64timedelta64r	   r   r2   r3   r   r   rd   ra   r5   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   r   re   r   r2   r3   r	   r   r   rg   r   r   r    (test_constructor_datetime_and_datetime64   s    zFTestIndexConstructorInference.test_constructor_datetime_and_datetime64) __name__
__module____qualname__r!   r%   r[   r\   parametrizer   r   r'   floatr-   rT   tupleiterr6   intr=   r   rA   rE   rK   rP   rV   r   re   r   rf   rc   rh   rn   r   r   r   r    r   "   sB   		

 	 

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   r   )r   Zany_numeric_ea_dtyper   r,   r   r   r    *test_constructor_object_dtype_with_ea_data   s    z<TestDtypeEnforced.test_constructor_object_dtype_with_ea_datar   Zfloat64r8   categoryc                 C   sH   t td}t ||d}|j|ks&tt td|d}|j|ksDtd S rF   )r	   rH   r   r   )r   r   rU   r4   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#   r9   r   )r   r	   r   r   )r   r   catr4   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-01r9   rN   z
Asia/TokyorG   r   )
r   r   r	   r   r2   r3   Ztz_localizer
   from_breaksrH   )r   dtir{   r4   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 )Nr}   r9   r~   ry   r   z
US/Pacific)rN   tz)r   r	   r   r2   r3   )r   r   r4   r5   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 )Nr}   r9   rR   rS   ry   r   )r   r	   r   r2   r3   )r   pir4   r5   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 DaysrG   r~   ry   r   )r   r	   r   r2   r3   )r   Ztdir4   r5   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 )Nr}   r9   r~   ry   r   )r   r
   r   r	   r   r2   r3   )r   r   r   r4   r5   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 )Nr}   r9   r~   z	Period[D]r   rR   )r   r	   Z	to_periodr2   r3   )r   r   r4   r5   r   r   r    :test_constructor_datetime64_values_mismatched_period_dtype  s    
zLTestDtypeEnforced.test_constructor_datetime64_values_mismatched_period_dtyper7   c              	   C   s6   t jg}d}tjt|d t||d W 5 Q R X d S )Nzcannot convertmatchr   )r   r'   r[   raises
ValueErrorr	   )r   r   ra   msgr   r   r    %test_constructor_int_dtype_nan_raises  s    z7TestDtypeEnforced.test_constructor_int_dtype_nan_raisesr?   r"   r#   r9   r   r:   r;   r0   c                 C   s"   |}t ||d}|j|kstd S r@   r	   r   r   )r   r?   Zany_int_numpy_dtyper   rD   r   r   r    test_constructor_dtypes_to_int  s    z0TestDtypeEnforced.test_constructor_dtypes_to_intc                 C   s"   |}t ||d}|j|kstd S r@   r   )r   r?   Zfloat_numpy_dtyper   rD   r   r   r     test_constructor_dtypes_to_float   s    z2TestDtypeEnforced.test_constructor_dtypes_to_floatz
2011-01-01z
2011-01-02r(   i  c                 C   s   t |dd}t|tstd S )Nry   r   )r	   rI   r   r   )r   r?   rD   r   r   r    &test_constructor_dtypes_to_categorical/  s    
z8TestDtypeEnforced.test_constructor_dtypes_to_categoricalr>   TFc                 C   sH   |r.t |td}t|t st|jtksDtnt |}t|tsDtd S r@   )r	   r   rI   r   r   r   rC   r   r   r    #test_constructor_dtypes_to_datetime<  s    	z5TestDtypeEnforced.test_constructor_dtypes_to_datetimerR   c                 C   sH   |r.t |td}t|t st|jtksDtnt |}t|tsDtd S r@   )r	   r   rI   r   r   r   rC   r   r   r    $test_constructor_dtypes_to_timedeltaM  s    	z6TestDtypeEnforced.test_constructor_dtypes_to_timedeltaN)ro   rp   rq   rx   r[   r\   rr   r   rz   r|   r   r   r   r   r   r   r   r   r   rv   rs   r   r   r   r   r	   re   r   rf   r   r   r   r   r   r    rw      st   
	




	
	rw   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 )TestIndexConstructorUnwrappingr_   c                 C   s>   t dt dt dg}t|}t|}||}t|| d S )NZ20110101Z20120101Z20130101)r   r   r   r2   r3   )r   r_   Zstampsr5   serr4   r   r   r    !test_constructor_from_series_dt64b  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#   r9   )r   r	   r   r2   r3   )r   r   r4   r5   r   r   r     test_constructor_no_pandas_arrayj  s    
z?TestIndexConstructorUnwrapping.test_constructor_no_pandas_arrayr   rG   abcz
2000-01-01r9   r~   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__}  s    zXTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLike.__init__c                 S   s   | j S r   r   )r   r   r   r   r    	__array__  s    zYTestIndexConstructorUnwrapping.test_constructor_ndarray_like.<locals>.ArrayLike.__array__)N)ro   rp   rq   r   r   Zndarrayr   r   r   r   r    	ArrayLike|  s   r   r<   )r   r   r   r5   r4   r   r   r    test_constructor_ndarray_likep  s    z<TestIndexConstructorUnwrapping.test_constructor_ndarray_likeN)ro   rp   rq   r[   r\   rr   r	   r   r   r   r   Zaranger   r   r+   r   r   r   r   r    r   _  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"   r7   r   )r[   r   OverflowErrorr	   r   Ziinfor8   max)r   r   r   r   r    test_constructor_overflow_int64  s    z;TestIndexConstructionErrors.test_constructor_overflow_int64N)ro   rp   rq   r   r   r   r   r    r     s   r   ) __doc__r   r   decimalr   Znumpyr   r[   Zpandasr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   Zpandas._testingZ_testingr2   r   rw   r   r   r   r   r   r    <module>   s   H  ()