o
    5cO                     @   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                   @   sv  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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-d.ge	
d-d.ggd/d0 Z d1d2 Z!d3d4 Z"d5d6 Z#ejd7g d8ejd9g d:d;d< Z$d=d> Z%d?d@ Z&ejd7g dAdBdC Z'dDdE Z(dFS )G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   c/var/www/html/gps/gps/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   s6   t ddd}tdddd}t|dd}t|| d S )Nz4/2/2012Br   
   startr)   r   r.   r)   )r   r   r   r   )r   pr$   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r;   r<   )  r@   r@   zMismatched Period array lengthsmatchr   )r;   monthr   2M)r6   r4      2007-01rE   r)   r   )r   arangerepeattiler   r   r   r   r(   asi8pytestraises
ValueError)
r   yearsquartersr1   r$   index2monthsmsgidxexpr   r   r   test_constructor_field_arraysF   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: XrA   z2007-1-1i  XrG   rL   rM   rN   r   )r   r   r   r   test_constructor_Ue   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 )	Nr6   N)ordinalr   r3   r.   endr   r4   rE   r   )r   r   r   r   r   r   rT   rU   r   r   r   test_constructor_nanoj   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   r3   )r6   r4   rE   r3   (   r?   )r   rH   int64rI   rJ   r   r   r   r   r;   r   r<   )r   rO   rP   pindexr   r   r   %test_constructor_arrays_negative_yeary   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 <= 4rA   r`   i  r3   r9   r:   )rL   rM   rN   r   rangelistr   rS   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 )NrF   g      %@r   rG   r,   )r   r   r   )r   r#   rU   r   r   r   test_constructor_corner   s   z'TestPeriodIndex.test_constructor_cornerc                 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 )NrF   r8   r   rG   z)freq not specified and cannot be inferredrA   z'Period' object is not iterable2007Ar   )datarD   r4   r   e)r   r   r   r   r"   rf   rL   rM   rN   rK   	TypeErrorr   iterr
   MonthEndr   asfreq)r   rT   rS   r#   rU   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   rH   rb   viewr   r   astyper   r   )r   valspir$   r   r   r   test_constructor_datetime64arr   s
   z.TestPeriodIndex.test_constructor_datetime64arrbox)Nseriesr1   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2017r3   r   rG   rz   r   r   )z
2017-01-31z
2017-02-28z
2017-03-31z
2017-04-30)r	   _valuesr   r   r   r   )r   ry   rl   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 differentrA   r   z	period[D])r   r   )r   r   r   r   rL   rM   r   )r   rT   rU   resrS   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 specifiedrA   )r   
isinstancelenr   rL   rM   rN   )r   rT   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   rA   r   )
r   r   r   r   r   r   r   rL   rM   rN   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\)rA   r   r   r   r   )rL   rM   r   r   r   r   r   r   rg   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_mixed4  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 )NrF   r0   r4   r   )namer)   r   <class .*PeriodIndex'>rA   r   z Should be numpy array of type i8r   )r   rL   rM   AssertionError_simple_new_datar   r   r'   r   rK   r   )r   rT   r#   rS   r   r   r   r   test_constructor_simple_newA  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   r0   )r   r   r   rA   r   )r   rL   rM   r   r   r   r   r   )r   rT   r#   r   r   r   !test_constructor_simple_new_emptyS  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 rA   z9PeriodIndex does not allow floating point in construction)rL   rM   r   r   r   rn   )r   r   rS   r   r   r   test_constructor_floats\  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 NaTrA   r   r!   r   r\   rX   rg   r   r   r   test_constructor_nate  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`   r?   c                 S   s&   g | ]}|d  dd|d dqS )r   dQr6   r   ).0tr   r   r   
<listcomp>p  s   & zETestPeriodIndex.test_constructor_year_and_quarter.<locals>.<listcomp>)r   r   ziprf   mapr   r   r   )r   r;   r<   rT   strslopsr0   r   r   r   !test_constructor_year_and_quarterl  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-01rD   r3   r.   r   r)   )r   z2014-03z2014-05z2014-07r   
2014-01-02z
2014-01-15r~   r\   )r   z
2014-01-05z
2014-01-08z
2014-01-11z
2014-01-142014-01-01 17:004HrE   )r]   r   r)   )z2014-01-01 09:00z2014-01-01 13:00r   z;Frequency must be positive, because it represents span: -1MrA   r   z-1Mz:Frequency must be positive, because it represents span: 0M0MrG   )r   r   r   r   rL   rM   rN   )r   pidxr$   rS   r   r   r   test_constructor_freq_multu  s4   "z*TestPeriodIndex.test_constructor_freq_multr   )rk   r   r   TSmult)r6   r4   rE   r3   r7   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25Hr4   r-   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 t dddd}t|dksKJ t dddd}t|dksZJ t dddd}t|dksiJ t dddd}t|dksxJ tdd}t |dd}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sJ ||k sJ |j|jksJ td"d}t ||d#}t|td$dg}t|d%ksJ |d |ksJ tt|td$dg}t|d%ksJ |d |ks&J |tdd&g}d'}tj	t
|d( t| W d    n	1 sFw   Y  t|}tj	t
|d( t| W d    n	1 sgw   Y  tj	td)d( tdd* W d    d S 1 sw   Y  d S )+Nrk   z1/1/2001z	12/1/2009)r   r.   r]   	   r   $   r   l   r   z
12/31/2009i  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-2005r8   r/   r   z
2006-12-31Wr,   )r]   r)   1wz
2005-05-01)r.   r]   z
2005-05-05r4   wz9Input has different freq=W-SUN from PeriodIndex\(freq=B\)rA   zpass as a string instead)r   r6   )r   r   r   r   allr   r   r   rL   rM   r   rn   )r   rw   r.   i1end_intvi2rv   rS   r   r   r   test_constructor  sf   






$z TestPeriodIndex.test_constructor)r   r   rk   r   r+   r   r   LUrZ   r   c                 C   s,   t d|dd}t|j|d}t|| d S )Nz
2001/04/01r6   r   r   )r   r   r"   r   r   )r   r   orgrT   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  r@   i  rk   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   rawr1   r$   r   r   r   r    test_map_with_string_constructor  s   
z0TestPeriodIndex.test_map_with_string_constructorN))__name__
__module____qualname__r    r%   rL   markparametrizerf   r   r   r   r   _from_sequencer*   r2   rV   rY   r_   rd   rh   ri   rr   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sP    
	*	
	(	
		D
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   rT   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-01rE   2DrG   ndarrayrA   )r   rL   rM   r   _shallow_copyrK   r   rw   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   rE   r   rG   r   rA   )r   rL   rM   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 float64rA   z
2000-01-01r,   r   rG   r   )rL   rM   rn   r   r   floatrg   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   srU   r   r   r   test_constructor_cast_object  s   z-TestSeriesPeriod.test_constructor_cast_objectN)r   r   r   r   r   r   r   r   r   r     s    r   )numpyr   rL   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    (
   q