o
    7?e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   n/home/www/facesmatcher.com/pyenv/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   Zmsg1pZ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   Z1990Q3Z2009Q2r   z2Q-DECr@   rA   )  rC   rC   zMismatched Period array lengthsr,   r   )r@   monthr   2M)r;   r9      2007-01rF   r)   r   )r   arangerepeattiler   r   r   r   r(   asi8pytestraises
ValueError)
r   yearsquartersr6   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  XrH   rM   rN   rO   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)Zordinalr   r8   r1   endr   r9   rF   r   )r   r   r   r   r   r   rU   rV   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   r8   )r;   r9   rF   r8   (   rB   )r   rI   int64rJ   rK   r   r   r   r   r@   r   rA   )r   rP   rQ   Z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,   r`   i  r8   r>   r?   )rM   rN   rO   r   rangelistr   rT   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 )NrG   g      %@r   rH   r/   )r   r   r   )r   r#   rV   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:00Z30Tr   r<   r0   r2   )r   r   r   r   r   )r   r1   rV   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 )NrG   r=   r   rH   z)freq not specified and cannot be inferredr,   z'Period' object is not iterableZ2007Ar   )datarE   r9   r   e)r   r   r   r   r"   re   rM   rN   rO   rL   	TypeErrorr   iterr
   ZMonthEndr   Zasfreq)r   rU   rT   r#   rV   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   rI   rb   viewr   r   Zastyper   r   )r   valspir$   r   r   r   test_constructor_datetime64arr   s
   z.TestPeriodIndex.test_constructor_datetime64arrbox)Nseriesr6   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 )	NZ2017r8   r   rH   rv   r   r   )z
2017-01-31z
2017-02-28z
2017-03-31z
2017-04-30)r	   Z_valuesr   r   r   r   )r   ru   rk   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   rM   rN   r   )r   rU   rV   resrT   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   rM   rN   rO   )r   rU   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   rM   rN   rO   r^   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   )rM   rN   r   r   r   r   r   r   rf   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   r^   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 )NrG   r5   r9   r   )namer)   r   <class .*PeriodIndex'>r,   r   z Should be numpy array of type i8r   )r   rM   rN   AssertionError_simple_new_datar   r   r'   r   rL   r   )r   rU   r#   rT   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   r5   )r   r   r   r,   r   )r   rM   rN   r   r   r   r   r   )r   rU   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)rM   rN   r   r   r   rm   )r   r   rT   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   r\   rY   rf   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  r`   rB   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   zipre   mapr   r   r   )r   r@   rA   rU   strsZlopsr5   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-01rE   r8   r1   r   r)   )r   z2014-03z2014-05z2014-07r   
2014-01-02z
2014-01-15rx   r\   )r   z
2014-01-05z
2014-01-08z
2014-01-11z
2014-01-142014-01-01 17:00Z4HrF   )r]   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: 0MZ0MrH   )r   r   r   r   rM   rN   rO   )r   pidxr$   rT   r   r   r   test_constructor_freq_mult  s4   "z*TestPeriodIndex.test_constructor_freq_multr   )rj   r   r   TSmult)r;   r9   rF   r8   r<   c                 C   s>   t || }td|dd}td|dd|}t|| d S )Nz
2014-04-01r/   r   )strr   r	   Z	to_periodr   r   )r   r   r   Z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)Z1D1HZ1H1Dz
2016-01-01z
2016-01-02r   z2016-01-01 00:00z2016-01-02 00:00Z25Hr9   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 ),Nrj   z1/1/2001z	12/1/2009)r   r1   r]   	   r   $   r   l   r   z
12/31/2009i  r+   r,   r.   i-	  Hz12/31/2001 23:00i8"  Z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/   )r]   r)   Z1wz
2005-05-01)r1   r]   z
2005-05-05r9   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   rM   rN   r   rm   )r   rs   rT   r1   i1Zend_intvi2rr   r   r   r   test_constructor  sx   






$z TestPeriodIndex.test_constructor)r   r   rj   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   orgrU   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  rC   i  rj   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   Zresir   r   r   	<genexpr>  s    zCTestPeriodIndex.test_map_with_string_constructor.<locals>.<genexpr>)r   r   r   r   r|   r   r   r   )r   rawr6   r$   ry   r   r   r    test_map_with_string_constructor  s   
z0TestPeriodIndex.test_map_with_string_constructorN)+__name__
__module____qualname__r    r%   rM   markZparametrizere   r   r   r   r   Z_from_sequencer*   r7   rW   rZ   r_   rc   rg   rh   ri   ro   rt   rw   rz   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   Z_viewr   r   )r   rU   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-01rF   2DrH   Zndarrayr,   )r   rM   rN   r   _shallow_copyrL   r   rs   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   rF   r   rH   r   r,   )r   rM   rN   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   rH   r   )rM   rN   rm   r   r   floatrf   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   Zassert_series_equal)r   srV   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   rM   Zpandas._libs.tslibs.periodr   Zpandas.core.dtypes.dtypesr   Zpandasr   r   r   r   r   r	   r
   r   Zpandas._testingZ_testingr   Zpandas.core.arraysr   r   r   r   r   r   r   r   <module>   s    (
    