o
    ҷhT                     @   s   d dl Zd dlZd dlm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 d dlmZ d dlmZ G dd dZG dd	 d	ZG d
d dZdS )    N)IncompatibleFrequency)PeriodDtype)IndexNaTPeriodPeriodIndexSeries
date_rangeoffsetsperiod_range)PeriodArrayc                   @   s  e Zd Zdd Zdd Zejdee	j
eej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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 ejd.d/d0ge	
d/d0ggd1d2 Z!d3d4 Z"d5d6 Z#d7d8 Z$ejd9g d:ejd;g d<d=d> Z%d?d@ Z&dAdB Z'ejd9g dCej(dDej(dEdFdG Z)dHdI Z*dJS )KTestPeriodIndexc                 C   s   t dddtt dddg}tt|t| ttt|tt| tjtt dddg}tt|t| ttt|tt| t dddtt dddg}tt|t|t	d ttt|tt|t	d d S )N2011-01Mfreq2011-03Ddtype)
r   r   tmassert_index_equalr   r   nparraynanobject)selfarr r   `/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/indexes/period/test_constructors.py"test_construction_base_constructor   s     (z2TestPeriodIndex.test_construction_base_constructorc                 C   s8   t d}g d}t||d}t||d}t|| d S )Nr   )
2011-01-01z
2012-03-04z
2014-05-01r   )r   r   r   r   r   )r   r   valuesresultexpectedr   r   r   'test_base_constructor_with_period_dtype&   s
   z7TestPeriodIndex.test_base_constructor_with_period_dtypevalues_constructorc                 C   sV   t dddtt dddg}||}t|td}t|tu sJ t|jt	| d S )Nr   r   r   r   r   )
r   r   r   r   typer   assert_numpy_array_equalr"   r   r   )r   r&   periodsr"   r#   r   r   r   test_index_object_dtype.   s   

z'TestPeriodIndex.test_index_object_dtypec                 C   s   d}t jt|d tddd}W d    n1 sw   Y  d}t jt|d tdddd}W d    n1 s:w   Y  t jt|d t|dd	}W d    n1 sWw   Y  t || d S )
N#Period with BDay freq is deprecatedmatchz4/2/2012Br   zPeriodDtype\[B\] is deprecated
   startr)   r   r1   r)   )r   assert_produces_warningFutureWarningr   r   r   )r   msg1pmsg2r$   indexr   r   r   test_constructor_use_start_freq>   s   z/TestPeriodIndex.test_constructor_use_start_freqc           
      C   s\  t ddddd }t t ddddd }t||d	d
}tddd	d}t|| t||dd
}t|j	|j	 t||d}t|| g d}ddg}d}t
jt|d t||dd W d    n1 smw   Y  t
jt|d t||dd W d    n1 sw   Y  g d}g d}t||dd}tdddd}	t||	 d S )Ni  i                 Q-DECyearquarterr   1990Q32009Q2r   z2Q-DECrB   rC   )  rG   rG   zMismatched Period array lengthsr,   r   )rB   monthr   2M)r=   r;      2007-01rJ   r)   r   )r   arangerepeattiler   r   r   r   r(   asi8pytestraises
ValueError)
r   yearsquartersr8   r$   index2monthsmsgidxexpr   r   r   test_constructor_field_arraysK   s.   z-TestPeriodIndex.test_constructor_field_arraysc                 C   sB   t jtdd tdddd W d    d S 1 sw   Y  d S )NzInvalid frequency: Xr,   z2007-1-1i  XrL   rQ   rR   rS   r   )r   r   r   r   test_constructor_Uj   s   "z"TestPeriodIndex.test_constructor_Uc                 C   sb   t tdddtddddd}ttdddtdddtdddtdddgdd}t|| d S )	Nr=   N)ordinalr   r:   r1   endr   r;   rJ   r   )r   r   r   r   r   r   rY   rZ   r   r   r   test_constructor_nanoo   s   



	z%TestPeriodIndex.test_constructor_nanoc                 C   sh   t jddt jdd}t t jg dt jdd}t||d}t|j	t
| t|jt
| d S )Ni    r   r:   )r=   r;   rJ   r:   (   rF   )r   rM   int64rN   rO   r   r   r   r   rB   r   rC   )r   rT   rU   pindexr   r   r   %test_constructor_arrays_negative_year~   s
   z5TestPeriodIndex.test_constructor_arrays_negative_yearc                 C   sT   d}t jt|d ttddttddd W d    d S 1 s#w   Y  d S )NzQuarter must be 1 <= q <= 4r,   re   i  r:   r@   rA   )rQ   rR   rS   r   rangelistr   rX   r   r   r   !test_constructor_invalid_quarters   s   "z1TestPeriodIndex.test_constructor_invalid_quartersc                 C   s,   t dddd}t dddd}t|| d S )NrK   g      %@r   rL   r/   )r   r   r   )r   r#   rZ   r   r   r   test_constructor_corner   s   z'TestPeriodIndex.test_constructor_cornerc                 C   s8   t ddd}t|d|jd}t|dd}t|| d S )Nz2002-01-01 00:0030Tr   r>   r0   r2   )r   r   r   r   r   )r   r1   rZ   r#   r   r   r   "test_constructor_with_without_freq   s   z2TestPeriodIndex.test_constructor_with_without_freqc                 C   s  t dddd}tt|j| ttt|j| d}tjt|d t|j	 W d    n1 s4w   Y  tjt|d tt|j	 W d    n1 sRw   Y  d}tjt
|d ttdd	d
d W d    n1 stw   Y  tt|}t|| t|}t|| t|dd
}t|| t|t d
}t|| |jdksJ t|dd
}t||d |jdksJ t|tdd
}t||d |jdksJ t|dd
}|dd}t|| d S )NrK   r?   r   rL   z)freq not specified and cannot be inferredr,   z'Period' object is not iterable2007Ar   )datarI   r;   r   e)r   r   r   r   r"   rk   rQ   rR   rS   rP   	TypeErrorr   iterr
   MonthEndr   asfreq)r   rY   rX   r#   rZ   r   r   r   test_constructor_fromarraylike   s@   z.TestPeriodIndex.test_constructor_fromarraylikec                 C   sR   t jdddt jd}|t d}t|dd}t|ddd}t|| d S )	Ni i d   r   zM8[us]r   r   zM8[ns])	r   rM   rg   viewr   r   astyper   r   )r   valspir$   r   r   r   test_constructor_datetime64arr   s
   z.TestPeriodIndex.test_constructor_datetime64arrbox)Nseriesr8   c                 C   sZ   t dddd}|d u r|j}n|dkrt|}t|dd}tg ddd}t|| d S )	N2017r:   r   rL   r   r   r   )z
2017-01-31z
2017-02-28z
2017-03-31z
2017-04-30)r	   _valuesr   r   r   r   )r   r   rs   r#   r$   r   r   r   !test_constructor_datetime64arr_ok   s   z1TestPeriodIndex.test_constructor_datetime64arr_okc                 C   s(  t ddgdd}t ddgdd}t|| |jdksJ t ddgd	d}t ddgd
d}t|| |jd	ks:J t ddgdd}t |dd}t ddgdd}t|| |jdks]J t |dd}t|| |jdkspJ d}tjt|d t dgddd W d    d S 1 sw   Y  d S )Nz2013-01z2013-03z	period[M]r   r   r   z
2013-01-05z
2013-03-05z
period[3D]3Dz
2013-01-01z
2013-01-02r   z&specified freq and dtype are differentr,   r   z	period[D])r   r   )r   r   r   r   rQ   rR   r   )r   rY   rZ   resrX   r   r   r   test_constructor_dtype   s(   "z&TestPeriodIndex.test_constructor_dtypec                 C   st   t g dd}t|t sJ t|dksJ |jdksJ tjtdd t g  W d    d S 1 s3w   Y  d S )Nr   r   r   freq not specifiedr,   )r   
isinstancelenr   rQ   rR   rS   )r   rY   r   r   r   test_constructor_empty   s   
"z&TestPeriodIndex.test_constructor_emptyc              
   C   s  t tdddttdddg}t g ddd}t|| t ttdddttdddg}t|| t tttdddtdddg}t g ddd}t|| t ttttdddtdddg}t|| t ttddgdd}t|| tjt	dd t ttg W d    n1 sw   Y  tjt	dd t tttg W d    n1 sw   Y  tjt	dd t ddg W d    n1 sw   Y  tjt	dd t tddg W d    d S 1 sw   Y  d S )	Nr   r   r   r   r   r   )r   r   r   r   r   r,   r   )
r   r   r   r   r   r   r   rQ   rR   rS   rc   r   r   r   test_constructor_pi_nat   sD   "z'TestPeriodIndex.test_constructor_pi_natc              
   C   sF  d}t jt|d ttdddttdddg W d    n1 s#w   Y  t jt|d tttdddttdddg W d    n1 sLw   Y  t jt|d tttdddtdddg W d    n1 srw   Y  t jt|d ttttdddtdddg W d    d S 1 sw   Y  d S )Nz5Input has different freq=D from PeriodIndex\(freq=M\)r,   r   r   r   r   )rQ   rR   r   r   r   r   r   r   rl   r   r   r   test_constructor_incompat_freq(  s*     "z.TestPeriodIndex.test_constructor_incompat_freqc                 C   s   t dttdddg}t g ddd}t|| t dttdddg}t g ddd}t|| t tdddtd	g}t g d
dd}t|| d S )Nr   r   r   r   r   )r   r   r   r!   r   
2012-01-01)r!   r   r   )r   r   r   r   r   rc   r   r   r   test_constructor_mixed@  s   z&TestPeriodIndex.test_constructor_mixedc                 C   s   t ddddd}tjtdd |j|dd W d    n1 s!w   Y  |j|jdd}t|| d	}tjt|d t|jjt	|j
|jd
 W d    n1 sVw   Y  t|jj|j
|jd
}|j|dd}t|| d S )NrK   r6   r;   r   )namer)   r   <class .*PeriodIndex'>r,   r   z Should be numpy array of type i8r   )r   rQ   rR   AssertionError_simple_new_datar   r   r'   r   rP   r   )r   rY   r#   rX   r   r   r   r   test_constructor_simple_newM  s   z+TestPeriodIndex.test_constructor_simple_newc                 C   sj   t g ddd}tjtdd |j|dd W d    n1 s w   Y  |j|jdd}t|| d S )Nr   r6   )r   r   r   r,   r   )r   rQ   rR   r   r   r   r   r   )r   rY   r#   r   r   r   !test_constructor_simple_new_empty_  s   z1TestPeriodIndex.test_constructor_simple_new_emptyfloatsg?g @c                 C   sx   t jtdd t| W d    n1 sw   Y  d}t jt|d t| W d    d S 1 s5w   Y  d S )Nz<class r,   z9PeriodIndex does not allow floating point in construction)rQ   rR   r   r   r   ru   )r   r   rX   r   r   r   test_constructor_floatsh  s   
"z'TestPeriodIndex.test_constructor_floatsc                 C   s   d}t jt|d tdddd W d    n1 sw   Y  t jt|d tdddd W d    d S 1 s:w   Y  d S )Nzstart and end must not be NaTr,   r   r!   r   ra   r]   rl   r   r   r   test_constructor_natq  s   "z$TestPeriodIndex.test_constructor_natc                 C   sZ   t g d}|d }t||d}dd t||D }ttt|}t|}t|| d S )N)i  i  i  re   rF   c                 S   s&   g | ]}|d  dd|d dqS )r   dQr=   r   ).0tr   r   r   
<listcomp>|  s   & zETestPeriodIndex.test_constructor_year_and_quarter.<locals>.<listcomp>)r   r   ziprk   mapr   r   r   )r   rB   rC   rY   strslopsr6   r   r   r   !test_constructor_year_and_quarterx  s   z1TestPeriodIndex.test_constructor_year_and_quarterc                 C   sD  t dddd}tg ddd}t|| t ddd	d
}tg dd	d}t|| t dddd}tg ddd}t|| d}tjt|d tdgdd W d    n1 sZw   Y  d}tjt|d tdgdd W d    n1 szw   Y  d}tjt|d t dddd W d    d S 1 sw   Y  d S )N2014-01rI   r:   r1   r   r)   )r   z2014-03z2014-05z2014-07r   
2014-01-02z
2014-01-15r   ra   )r   z
2014-01-05z
2014-01-08z
2014-01-11z
2014-01-142014-01-01 17:004HrJ   )rb   r   r)   )z2014-01-01 09:00z2014-01-01 13:00r   z;Frequency must be positive, because it represents span: -1Mr,   r   z-1Mz:Frequency must be positive, because it represents span: 0M0MrL   )r   r   r   r   rQ   rR   rS   )r   pidxr$   rX   r   r   r   test_constructor_freq_mult  s4   "z*TestPeriodIndex.test_constructor_freq_multr   )rr   r   r   TSmult)r=   r;   rJ   r:   r>   c                 C   s>   t || }td|dd}td|dd|}t|| d S )Nz
2014-04-01r/   r   )strr   r	   	to_periodr   r   )r   r   r   freqstrr   r$   r   r   r   %test_constructor_freq_mult_dti_compat  s   z5TestPeriodIndex.test_constructor_freq_mult_dti_compatc                 C   sb   dD ]}t ddg|d}t ddgdd}qdD ]}tdd|d	}t dd
gdd}t|| qd S )N)1D1H1H1Dz
2016-01-01z
2016-01-02r   z2016-01-01 00:00z2016-01-02 00:0025Hr;   r0   z2016-01-02 01:00)r   r   r   r   )r   r   r   r$   r   r   r   test_constructor_freq_combined  s   z.TestPeriodIndex.test_constructor_freq_combinedc                 C   s  t dddd}t|dksJ t dddd}t|dksJ t dddd}t|d	ks-J t d
ddd}t|dks<J d}tjt|d t dddd}W d    n1 sWw   Y  t|dksdJ t dddd}t|dkssJ t dddd}t|dksJ t dddd}t|dksJ tjt|d tdd}t |dd}W d    n1 sw   Y  t|dksJ |j|jksJ |d |ksJ tdd}t |d d!}t|d ksJ |j|jksJ |d" |ksJ tdd#}t |d d!}t|t|ksJ ||k sJ |j|jksJ tjt|d td$d}t ||d%}t|td&dg}W d    n	1 s=w   Y  t|d'ksKJ |d |ksTJ tjt|d tt	
|td&dg}W d    n	1 ssw   Y  t|d'ksJ |d |ksJ |tdd(g}d)}tjt|d t| W d    n	1 sw   Y  t	
|}tjt|d t| W d    n	1 sw   Y  tjtd*d tdd+ W d    d S 1 sw   Y  d S ),Nrr   z1/1/2001z	12/1/2009)r   r1   rb   	   r   $   r   l   r   z
12/31/2009i  r+   r,   r.   i-	  Hz12/31/2001 23:00i8"  Minz1/1/2001 23:59i  r   z1/1/2001 23:59:59iQ z02-Apr-2005r?   r2   r   z
2006-12-31Wr/   )rb   r)   1wz
2005-05-01)r1   rb   z
2005-05-05r;   wz9Input has different freq=W-SUN from PeriodIndex\(freq=B\)zpass as a string instead)r   r=   )r   r   r   r3   r4   r   r   allr   r   r   rQ   rR   r   ru   )r   r~   rX   r1   i1end_intvi2r}   r   r   r   test_constructor  sx   






$z TestPeriodIndex.test_constructor)r   r   rr   r   r.   r   r   LUr_   r   z8ignore:Period with BDay freq is deprecated:FutureWarningz3ignore:PeriodDtype\[B\] is deprecated:FutureWarningc                 C   s,   t d|dd}t|j|d}t|| d S )Nz
2001/04/01r=   r   r   )r   r   r"   r   r   )r   r   orgrY   r   r   r   test_recreate_from_data  s   z'TestPeriodIndex.test_recreate_from_datac                 C   sd   g d}t |dd}tdd |D }|t}t|tsJ tdd |D s*J t|| d S )N)i  rG   i  rr   r   c                 S   s   g | ]}t |qS r   )r   )r   numr   r   r   r     s    zDTestPeriodIndex.test_map_with_string_constructor.<locals>.<listcomp>c                 s   s    | ]}t |tV  qd S )N)r   r   )r   resir   r   r   	<genexpr>  s    zCTestPeriodIndex.test_map_with_string_constructor.<locals>.<genexpr>)r   r   r   r   r   r   r   r   )r   rawr8   r$   r   r   r   r    test_map_with_string_constructor  s   
z0TestPeriodIndex.test_map_with_string_constructorN)+__name__
__module____qualname__r    r%   rQ   markparametrizerk   r   r   r   r   _from_sequencer*   r9   r[   r^   rd   ri   rm   rn   rp   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   filterwarningsr   r   r   r   r   r   r      sZ    
	*	
	(	
		I
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestShallowCopyc                 C   s(   t g dd}| }|}t|| d S )Nr   r   )r   _viewr   r   )r   rY   r#   r$   r   r   r   test_shallow_copy_empty  s   z'TestShallowCopy.test_shallow_copy_emptyc                 C   sN   t dddd}tjtdd ||j W d    d S 1 s w   Y  d S )N
2018-01-01rJ   2DrL   ndarrayr,   )r   rQ   rR   r   _shallow_copyrP   r   r~   r   r   r   test_shallow_copy_disallow_i8"  s   "z-TestShallowCopy.test_shallow_copy_disallow_i8c                 C   sL   t dddd}tjtdd || W d    d S 1 sw   Y  d S )Nr   rJ   r   rL   r   r,   )r   rQ   rR   r   r   r   r   r   r   0test_shallow_copy_requires_disallow_period_index(  s   "z@TestShallowCopy.test_shallow_copy_requires_disallow_period_indexN)r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   s   e Zd Zdd Zdd ZdS )TestSeriesPeriodc                 C   sN   d}t jt|d ttddddtd W d    d S 1 s w   Y  d S )Nz(Cannot cast PeriodIndex to dtype float64r,   z
2000-01-01r/   r   rL   r   )rQ   rR   ru   r   r   floatrl   r   r   r   !test_constructor_cant_cast_period/  s   "z2TestSeriesPeriod.test_constructor_cant_cast_periodc                 C   s8   t tdddtdd}t tddd}t|| d S )Nz1/1/2000r/   )r)   r   r   )r   r   r   r   assert_series_equal)r   srZ   r   r   r   test_constructor_cast_object4  s   z-TestSeriesPeriod.test_constructor_cast_objectN)r   r   r   r   r   r   r   r   r   r   .  s    r   )numpyr   rQ   pandas._libs.tslibs.periodr   pandas.core.dtypes.dtypesr   pandasr   r   r   r   r   r	   r
   r   pandas._testing_testingr   pandas.core.arraysr   r   r   r   r   r   r   r   <module>   s    (
    