U
    c{%                     @   sj   d dl mZ d dlZd dlZd dlZd dlmZmZm	Z	m
Z
 d dlmZ d dlmZmZ G dd dZdS )    	timedeltaN)	TimedeltaTimedeltaIndextimedelta_rangeto_timedelta)TimedeltaArraysequence_to_td64nsc                   @   s   e Zd Zdd Zejd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dd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'S )(TestTimedeltaIndexc              	   C   s   t dd}t j|gtd}d}tjt|d t| W 5 Q R X tjt|d t	| W 5 Q R X tjt|d t
| W 5 Q R X d S )NNaTnsZdtypez!Invalid type for timedelta scalarmatch)npZ
datetime64arrayobjectpytestraises
ValueErrorr   r   Z_from_sequencer	   )selfZnatarrmsg r   U/tmp/pip-unpacked-wheel-g7fro6k3/pandas/tests/indexes/timedeltas/test_constructors.pytest_array_of_dt64_nat_raises   s    z0TestTimedeltaIndex.test_array_of_dt64_nat_raisesunitYyMc              	   C   s2   d}t jt|d tdddg| W 5 Q R X d S )Nz/Units 'M', 'Y', and 'y' are no longer supportedr            r   r   r   r   )r   r   r   r   r   r   test_unit_m_y_raises%   s    z'TestTimedeltaIndex.test_unit_m_y_raisesc                 C   s2   t jdt jd}t|dd}|jjj|ks.td S )N
   r   F)copy)r   ZarangeZint64r   _databaseAssertionError)r   r   tdir   r   r   test_int64_nocopy+   s    z$TestTimedeltaIndex.test_int64_nocopyc                 C   sB   t dddd}t|dd}|j|jks*tdt|di ks>td S )	N1 secondi 1speriodsfreqZinferr0   Zinferred_freq_cache)r   r   r0   r)   getattr)r   r*   resultr   r   r   test_infer_from_tdi2   s    z&TestTimedeltaIndex.test_infer_from_tdic              	   C   s   t dddd}d}tjt|d t|dd W 5 Q R X tjt|d t|dd W 5 Q R X tjt|d t|jdd W 5 Q R X tjt|d t|jdd W 5 Q R X d S )	Nr,   d   r-   r.   zMInferred frequency .* from passed values does not conform to passed frequencyr   Dr1   )r   r   r   r   r   r   r'   )r   r*   r   r   r   r   test_infer_from_tdi_mismatch?   s    z/TestTimedeltaIndex.test_infer_from_tdi_mismatchc              	   C   s   t jddd}d}tjt|d t|d W 5 Q R X tjt|d t| W 5 Q R X tjt|d tt| W 5 Q R X d S )Nz
2016-01-01r!   r/   z"cannot be converted to timedelta64r   zEurope/Brussels)	pdZ
date_ranger   r   	TypeErrorr   Ztz_localizer   Zasarray)r   Zdtir   r   r   r   test_dt64_data_invalidV   s    z)TestTimedeltaIndex.test_dt64_data_invalidc                 C   sz   t ddg}t ddg}t|| t ddg}t ddg}t|| t dtjg}t tddtjg}t|| d S )Ngffffff@gffffff#@   	   g       @g      "@)Znanoseconds)r   tmassert_index_equalr   nanr   r:   r   r   r*   expectedr   r   r   test_float64_ns_roundedf   s    z*TestTimedeltaIndex.test_float64_ns_roundedc                 C   s8   t ddgdd}t tddtddg}t|| d S )Ng      ?g      @r7   r   )days)r   r   r?   r@   rB   r   r   r   test_float64_unit_conversionw   s    z/TestTimedeltaIndex.test_float64_unit_conversionc                 C   s   t dtjt dg}tt|t| ttt|tt| tj	tjt dg}tt|t| ttt|tt| d S )N1 days3 days)
r   r:   r   r?   r@   Indexr   r   r   rA   )r   r   r   r   r   "test_construction_base_constructor}   s    "z5TestTimedeltaIndex.test_construction_base_constructorc                 C   s   t dddddg}t ddtddtddd	tjd
g}t|| t ddtddtddd	tjd
g}t dddg}tt t	d
dd| t dddg}tt dddgdd| t dddg}tt dddgdd| d S )NrH   z1 days 00:00:052 daysz2 days 00:00:02z0 days 00:00:03z1 days, 00:00:05r=   r7   )rF   secondsr!   z0 days 00:00:00z0 days 00:00:01z0 days 00:00:02srE   z0 days 00:00:05z0 days 00:00:09r      r>   z0 days 00:00:00.400z0 days 00:00:00.450z0 days 00:00:01.200i  i  i  ms)
r   r   timedelta64r   r:   offsetsZSecondr?   r@   range)r   rC   r4   r   r   r   test_constructor   sJ    	


	



z#TestTimedeltaIndex.test_constructorc                 C   s:   t dddd}dd tddD }t|}t|| d S )	Nr-   r>   rN   r.   c                 S   s   g | ]}d | dqS )ZP0DT0H0MSr   .0ir   r   r   
<listcomp>   s     z;TestTimedeltaIndex.test_constructor_iso.<locals>.<listcomp>r    r%   )r   rS   r   r?   r@   )r   rC   Z	durationsr4   r   r   r   test_constructor_iso   s    z'TestTimedeltaIndex.test_constructor_isoc           	   	   C   s\  t ddd}t ddd}t|| d}tjt|d t dddd	 W 5 Q R X d
}tjt|d td W 5 Q R X dd tdD }t|}tdd tdD }t|| t	dddg}t|}t
dddgdd}t|| t|j}t|| d}tjt|d tdddgdd W 5 Q R X d}tjt|d t ddd W 5 Q R X d S )NrH   g      %@r9   r%   z!periods must be a number, got foor   Zfoor7   )startr/   r0   z[TimedeltaIndex\(\.\.\.\) must be called with a collection of some kind, '1 days' was passedc                 s   s   | ]}t |V  qd S )Nr   rV   r   r   r   	<genexpr>   s     z?TestTimedeltaIndex.test_constructor_coverage.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   r   rV   r   r   r   rY      s     z@TestTimedeltaIndex.test_constructor_coverage.<locals>.<listcomp>rL   rI   r    r=   r!   drE   zQInferred frequency None from passed values does not conform to passed frequency Dz4 daysr1   zVOf the four parameters: start, end, periods, and freq, exactly three must be specifiedr.   )r   r?   r@   r   r   r;   r   rS   r   r   r   Zasi8r   )	r   rngexpr   genr4   rC   stringsZ	from_intsr   r   r   test_constructor_coverage   s8    
z,TestTimedeltaIndex.test_constructor_coveragec                 C   s<   t ddddd}|jdkstt|dd}|jdks8td S )NrH   r    r7   ZTEST)r[   r/   r0   namezsomething else)rc   )r   rc   r)   r   )r   idxZidx2r   r   r   test_constructor_name   s    z(TestTimedeltaIndex.test_constructor_namec              	   C   sZ   d}t jt|d tdgdd W 5 Q R X t jt|d tjdgdd W 5 Q R X d S )Nz with no precision is not allowedr   2000rQ   r   )r   r   r   r   r:   rJ   r   r   r   r   r   $test_constructor_no_precision_raises   s
    z7TestTimedeltaIndex.test_constructor_no_precision_raisesc              	   C   s0   d}t jt|d tdgdd W 5 Q R X d S )Nz@dtype timedelta64\[us\] cannot be converted to timedelta64\[ns\]r   rf   ztimedelta64[us]r   r#   rg   r   r   r   'test_constructor_wrong_precision_raises   s    z:TestTimedeltaIndex.test_constructor_wrong_precision_raisesc                 C   sn   t ddd}|jd k	stt|d d}|jd ks4tt|jd d}|jd ksPtt|d d}|jd ksjtd S )Nr    rO   r9   r1   )r   r0   r)   r   r'   r   )r   r*   r4   Ztdar   r   r   test_explicit_none_freq   s    z*TestTimedeltaIndex.test_explicit_none_freqc                 C   sL   t ddd}t|}t|}t|| t|}t|}t|| d S )Nr    rO   r9   )r   r:   ZCategoricalr   r?   r@   ZCategoricalIndex)r   r*   catr4   cir   r   r   test_from_categorical  s    

z(TestTimedeltaIndex.test_from_categoricalN)__name__
__module____qualname__r   r   markZparametrizer$   r+   r5   r8   r<   rD   rG   rK   rT   rZ   rb   re   rh   ri   rj   rm   r   r   r   r   r
      s$   
	-.
r
   )datetimer   Znumpyr   r   Zpandasr:   r   r   r   r   Zpandas._testingZ_testingr?   Zpandas.core.arrays.timedeltasr   r	   r
   r   r   r   r   <module>   s   