o
    ҷhR&                     @   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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   sD   d}t jt|d tg dd W d    d S 1 sw   Y  d S )NzThe 'closed' keywordmatchT)closed)tmassert_produces_warningFutureWarningr   selfmsg r   d/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/indexes/timedeltas/test_constructors.pytest_closed_deprecated   s   "z)TestTimedeltaIndex.test_closed_deprecatedc                 C   s   t dd}t j|gtd}d}tjt|d t| W d    n1 s&w   Y  tjt|d t	| W d    n1 sBw   Y  tjt|d t
| W d    n1 s]w   Y  tjt|d t| W d    d S 1 syw   Y  d S )NNaTnsdtypez!Invalid type for timedelta scalarr   )np
datetime64arrayobjectpytestraises	TypeErrorr   r   _from_sequencer	   r   )r   natarrr   r   r   r   test_array_of_dt64_nat_raises   s   


"z0TestTimedeltaIndex.test_array_of_dt64_nat_raisesunit)YyMc                 C   sF   d}t jt|d tg d| W d    d S 1 sw   Y  d S )Nz/Units 'M', 'Y', and 'y' are no longer supportedr   )         )r   r    
ValueErrorr   )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   arangeint64r   _data_ndarraybase)r   r$   tdir   r   r   test_int64_nocopy3   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freqinferr<   inferred_freq_cache)r   r   r<   getattr)r   r6   resultr   r   r   test_infer_from_tdi:   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 s!w   Y  tjt|d t|dd W d    n1 s>w   Y  tjt|d t|jdd W d    n1 s\w   Y  tjt|d t|jdd W d    d S 1 s{w   Y  d S )	Nr8   d   r9   r:   zMInferred frequency .* from passed values does not conform to passed frequencyr   Dr>   )r   r   r    r-   r   r   r3   )r   r6   r   r   r   r   test_infer_from_tdi_mismatchG   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 s"w   Y  tjt|d t| W d    n1 s=w   Y  tjt|d tt| W d    d S 1 s\w   Y  d S )Nz
2016-01-01r+   r;   z"cannot be converted to timedelta64r   zEurope/Brussels)	pd
date_ranger   r    r!   r   tz_localizer   asarray)r   dtir   r   r   r   test_dt64_data_invalid^   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      "@)nanoseconds)r   r   assert_index_equalr   nanr   rH   r   r   r6   expectedr   r   r   test_float64_ns_roundedn   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      @rE   r&   )days)r   r   r   rQ   rS   r   r   r   test_float64_unit_conversion   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   rH   r   r   rQ   Indexr   r   r   rR   )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 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)rY   z1 days 00:00:052 daysz2 days 00:00:02z0 days 00:00:03rY   z1 days, 00:00:05rN   rE   )rW   secondsr+   )0 days 00:00:00z0 days 00:00:01z0 days 00:00:02srV   )r_   z0 days 00:00:05z0 days 00:00:09)r      rO   )z0 days 00:00:00.400z0 days 00:00:00.450z0 days 00:00:01.200)i  i  i  ms)
r   r   timedelta64r   rH   offsetsSecondr   rQ   range)r   rT   rB   r   r   r   test_constructor   s0   	


	z#TestTimedeltaIndex.test_constructorc                 C   s:   t dddd}dd tddD }t|}t|| d S )	Nr9   rO   r`   r:   c                 S   s   g | ]}d | dqS )P0DT0H0MSr   .0ir   r   r   
<listcomp>   s    z;TestTimedeltaIndex.test_constructor_iso.<locals>.<listcomp>r*   r/   )r   rf   r   r   rQ   )r   rT   	durationsrB   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 s-w   Y  d
}tjt|d td W d    n1 sJw   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 sw   Y  d}tjt|d t ddd W d    d S 1 sw   Y  d S )NrY   g      %@rG   r/   z!periods must be a number, got foor   foorE   )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   rj   r   r   r   	<genexpr>   s    z?TestTimedeltaIndex.test_constructor_coverage.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   r   rj   r   r   r   rm      s    z@TestTimedeltaIndex.test_constructor_coverage.<locals>.<listcomp>)rY   r]   rZ   )r*   rN   r+   drV   zQInferred frequency None from passed values does not conform to passed frequency D)rY   r]   z4 daysr>   zVOf the four parameters: start, end, periods, and freq, exactly three must be specifiedr:   )r   r   rQ   r   r    r!   r   rf   r   r   r   asi8r-   )	r   rngexpr   genrB   rT   strings	from_intsr   r   r   test_constructor_coverage   s@   

"z,TestTimedeltaIndex.test_constructor_coveragec                 C   s<   t ddddd}|jdksJ t|dd}|jdksJ d S )NrY   r*   rE   TEST)rq   r;   r<   namezsomething else)r|   )r   r|   r   )r   idx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 sw   Y  d}t jt|d tjdgdd W d    d S 1 s=w   Y  d S )Nz with no precision is not allowedr   2000rc   r   z3The 'timedelta64' dtype has no unit. Please pass in)r   r    r-   r   rH   r[   r   r   r   r   $test_constructor_no_precision_raises   s   "z7TestTimedeltaIndex.test_constructor_no_precision_raisesc                 C   s`   d}t jt|d tdgdd W d    n1 sw   Y  tdgdd}|jdks.J d S )Nz?dtype timedelta64\[D\] cannot be converted to timedelta64\[ns\]r   r   ztimedelta64[D]r   ztimedelta64[us]zm8[us])r   r    r-   r   r   )r   r   r6   r   r   r   'test_constructor_wrong_precision_raises   s   z:TestTimedeltaIndex.test_constructor_wrong_precision_raisesc                 C   sn   t ddd}|jd usJ t|d d}|jd u sJ t|jd d}|jd u s(J t|d d}|jd u s5J d S )Nr*   ra   rG   r>   )r   r<   r   r3   r   )r   r6   rB   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*   ra   rG   )r   rH   Categoricalr   r   rQ   CategoricalIndex)r   r6   catrB   cir   r   r   test_from_categorical  s   

z(TestTimedeltaIndex.test_from_categoricalN)__name__
__module____qualname__r   r%   r   markparametrizer.   r7   rC   rF   rM   rU   rX   r\   rg   ro   rz   r   r   r   r   r   r   r   r   r   r
      s(    
	".	r
   )datetimer   numpyr   r   pandasrH   r   r   r   r   pandas._testing_testingr   pandas.core.arrays.timedeltasr   r	   r
   r   r   r   r   <module>   s    