a
    Gb-%                     @   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g d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 d    n1 sL0    Y  tjt|d t	| W d    n1 s0    Y  tjt|d t
| W d    n1 s0    Y  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   Plib/python3.9/site-packages/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   sD   d}t jt|d tg d| W d    n1 s60    Y  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|u s.J d S )N
   r   F)copy)r   ZarangeZint64r   _database)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*J dt|di vs>J d S )	N1 secondi 1speriodsfreqZinferr/   Zinferred_freq_cache)r   r   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 d    n1 sB0    Y  tjt|d t|dd W d    n1 s|0    Y  tjt|d t|jdd W d    n1 s0    Y  tjt|d t|jdd W d    n1 s0    Y  d S )	Nr+   d   r,   r-   zMInferred frequency .* from passed values does not conform to passed frequencyr   Dr0   )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 d    n1 sD0    Y  tjt|d t| W d    n1 sz0    Y  tjt|d tt| W d    n1 s0    Y  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 g d}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 g d}tt t	dd	d
| t g d}tt g dd	d
| t g d}tt g ddd
| d S )N)rH   z1 days 00:00:052 daysz2 days 00:00:02z0 days 00:00:03rH   z1 days, 00:00:05r=   r7   )rF   Zsecondsr!   )0 days 00:00:00z0 days 00:00:01z0 days 00:00:02srE   )rM   z0 days 00:00:05z0 days 00:00:09)r      r>   )z0 days 00:00:00.400z0 days 00:00:00.450z0 days 00:00:01.200)i  i  i  Zms)
r   r   timedelta64r   r:   ZoffsetsZSecondr?   r@   range)r   rC   r4   r   r   r   test_constructor   s@    	


	



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>       z;TestTimedeltaIndex.test_constructor_iso.<locals>.<listcomp>r    r%   )r   rQ   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 d    n1 sZ0    Y  d
}tjt|d td W d    n1 s0    Y  dd tdD }t|}tdd tdD }t|| t	g d}t|}t
g ddd}t|| t|j}t|| d}tjt|d  tg ddd W d    n1 s\0    Y  d}tjt|d t ddd W d    n1 s0    Y  d S )NrH   g      %@r9   r%   z!periods must be a number, got foor   Zfoor7   )startr.   r/   z[TimedeltaIndex\(\.\.\.\) must be called with a collection of some kind, '1 days' was passedc                 s   s   | ]}t |V  qd S )Nr   rT   r   r   r   	<genexpr>   rX   z?TestTimedeltaIndex.test_constructor_coverage.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   r   rT   r   r   r   rW      rX   z@TestTimedeltaIndex.test_constructor_coverage.<locals>.<listcomp>)rH   rL   rI   )r    r=   r!   drE   zQInferred frequency None from passed values does not conform to passed frequency D)rH   rL   z4 daysr0   zVOf the four parameters: start, end, periods, and freq, exactly three must be specifiedr-   )r   r?   r@   r   r   r;   r   rQ   r   r   r   Zasi8r   )	r   rngZexpr   genr4   rC   ZstringsZ	from_intsr   r   r   test_constructor_coverage   s8    ,&
0z,TestTimedeltaIndex.test_constructor_coveragec                 C   s<   t ddddd}|jdksJ t|dd}|jdks8J d S )NrH   r    r7   ZTEST)rZ   r.   r/   namezsomething else)r`   )r   r`   r   )r   idxZidx2r   r   r   test_constructor_name   s    z(TestTimedeltaIndex.test_constructor_namec                 C   s   d}t jt|d tdgdd W d    n1 s60    Y  t jt|d  tjdgdd W d    n1 st0    Y  d S )Nz with no precision is not allowedr   2000rP   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   sD   d}t jt|d tdgdd W d    n1 s60    Y  d S )Nz@dtype timedelta64\[us\] cannot be converted to timedelta64\[ns\]r   rc   ztimedelta64[us]r   r#   rd   r   r   r   'test_constructor_wrong_precision_raises   s    z:TestTimedeltaIndex.test_constructor_wrong_precision_raisesc                 C   sT   t ddd}|jd usJ t|d d}|jd u s4J t|jd d}|jd u sPJ d S )Nr    rO   r9   r0   )r   r/   r   r'   r3   r   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   Zcir   r   r   test_from_categorical
  s    

z(TestTimedeltaIndex.test_from_categoricalN)__name__
__module____qualname__r   r   ZmarkZparametrizer$   r*   r5   r8   r<   rD   rG   rK   rR   rY   r_   rb   re   rf   rg   ri   r   r   r   r   r
      s$   
	-.
r
   )Z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   