3
UdwM                 @   s   d dl Zd dlZd dlmZ d dlmZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZ d dljZd dlm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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jdddd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ejjd.d/d0ge	j
d/d0ggd1d2 Z d3d4 Z!d5d6 Z"d7d8 Z#ejjd9d:d;d<d=d>gejjd?d@dAdBdCdDgdEdF Z$dGdH Z%dIdJ Z&ejjd9d;dKd:d<dLd=d>dMdNdOdPgdQdR Z'dSdT Z(dS )UTestPeriodIndexc             C   s   t dddtt dddg}tjt|t| tjttj|ttj| tjtt dddg}tjt|t| tjttj|ttj| t dddtt dddg}tjt|t|t	d tjttj|ttj|t	d d S )Nz2011-01M)freqz2011-03D)dtype)
r   r   tmassert_index_equalr   r   nparraynanobject)selfarr r   a/var/www/html/virt/lib64/python3.6/site-packages/pandas/tests/indexes/period/test_constructors.py"test_construction_base_constructor   s      z2TestPeriodIndex.test_construction_base_constructorc             C   s:   t d}dddg}t||d}t||d}tj|| d S )Nr   z
2011-01-01z
2012-03-04z
2014-05-01)r   )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ks>ttj|jt	j
| d S )Nz2011-01r   )r   z2011-03)r   )r   r   r   r   typeAssertionErrorr   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j|| d S )Nz4/2/2012B)r   
   )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 jddjddd }t jt jddddd }t||dd	}td
ddd}tj|| t||dd	}tj|j	|j	 t||d}tj|| dddg}ddg}d}t
jt|d t||dd W d Q R X t
jt|d t||dd W d Q R X dddg}dddg}t||dd}tdddd}	tj||	 d S )Ni  i                 zQ-DEC)yearquarterr   Z1990Q3Z2009Q2)r   z2Q-DEC)r2   r3   i  zMismatched Period array lengths)matchr   )r2   monthr   2M   z2007-01)r%   r   r8   )r   arangerepeattiler   r   r   r   r$   asi8pytestraises
ValueError)
r   yearsquartersr+   r   index2monthsmsgidxexpr   r   r   test_constructor_field_arraysG   s*    


z-TestPeriodIndex.test_constructor_field_arraysc             C   s,   t jtdd tdddd W d Q R X d S )NzInvalid frequency: X)r4   z2007-1-1i  X)r%   r   )r=   r>   r?   r   )r   r   r   r   test_constructor_Uf   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j|| d S )	Nr/   N)Zordinalr   r-   )r)   endr   r.   r7   )r   )r   r   r   r   r   )r   rE   rF   r   r   r   test_constructor_nanok   s    


z%TestPeriodIndex.test_constructor_nanoc             C   sl   t jddt jdjd}t jt jddddgt jdd}t||d	}tj|j	t
| tj|jt
| d S )
Ni  i  )r   r-   r/   r.   r7   (   )r2   r3   )r   r9   int64r:   r;   r   r   r   r   r2   r   r3   )r   r@   rA   Zpindexr   r   r   %test_constructor_arrays_negative_yearz   s
     z5TestPeriodIndex.test_constructor_arrays_negative_yearc             C   s>   d}t jt|d" ttddttddd W d Q R X d S )NzQuarter must be 1 <= q <= 4)r4   i  i  r-   zQ-DEC)r2   r3   r   )r=   r>   r?   r   rangelist)r   rD   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j|| d S )Nz2007-01g      %@r   )r%   r   r(   )r   r   r   )r   r   rF   r   r   r   test_constructor_corner   s    z'TestPeriodIndex.test_constructor_cornerc             C   s  t dddd}tjt|j| tjtt|j| d}tjt|d t|j	 W d Q R X tjt|d tt|j	 W d Q R X d}tjt
|d ttdd	d
d W d Q R X tt|}tj|| t|}tj|| t|dd
}tj|| t|tj d
}tj|| |jdks&tt|dd
}tj||jd |jdksTtt|tjdd
}tj||jd |jdkstt|dd
}|jdd}tj|| d S )Nz2007-01r1   r   )r%   r   z)freq not specified and cannot be inferred)r4   z'Period' object is not iterableZ2007A)r   )datar6   r.   r   e)r   r   r   r   r   rQ   r=   r>   r?   r<   	TypeErrorr   iterr
   ZMonthEndr   r#   Zasfreq)r   rE   rD   r   rF   r   r   r   test_constructor_fromarraylike   s:    z.TestPeriodIndex.test_constructor_fromarraylikec             C   sR   t jdd
dt jd}|jt jd}d}tjt|d t|dd	 W d Q R X d S )Ni i'  d   )r   zM8[us]zWrong dtype: datetime64\[us\])r4   r   )r   i )	r   r9   rN   viewr   r=   r>   r?   r   )r   valsrD   r   r   r   test_constructor_datetime64arr   s
    z.TestPeriodIndex.test_constructor_datetime64arrboxNseriesr+   c             C   s`   t dddd}|d kr|j}n|dkr0tj|}t|dd}tdd	d
dgdd}tj|| d S )NZ2017r-   r   )r%   r   r_   r   )r   z
2017-01-31z
2017-02-28z
2017-03-31z
2017-04-30)r	   Z_valuespdr   r   r   r   )r   r^   rU   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|| |jdks:tt ddgd	d}t ddgd
d}tj|| |jd	ksttt ddgdd}t |dd}t ddgdd}tj|| |jdkstt |dd}tj|| |jdkstd}tjt|d t dgddd W d Q R X 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 different)r4   z2011-01z	period[D])r   r   )r   r   r   r   r#   r=   r>   r   )r   rE   rF   resrD   r   r   r   test_constructor_dtype   s&    z&TestPeriodIndex.test_constructor_dtypec          	   C   s^   t g dd}t|t stt|dks*t|jdks8ttjtdd t g  W d Q R X d S )Nr   )r   r   zfreq not specified)r4   )r   
isinstancer#   lenr   r=   r>   r?   )r   rE   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 dddgdd}tj|| t tjtdddttdddg}tj|| t tttdddtdddg}t ddddgdd}tj|| t tjtttdddtdddg}tj|| t ttddgdd}tj|| tjt	dd t ttg W d Q R X tjt	dd t tjttg W d Q R X tjt	dd t ddg W d Q R X tjt	dd t tjddg W d Q R X d S )Nz2011-01r   )r   r   zfreq not specified)r4   )
r   r   r   r   r   r   r   r=   r>   r?   )r   rE   rF   r   r   r   test_constructor_pi_nat   s2    " z'TestPeriodIndex.test_constructor_pi_natc          $   C   s   d}t jt|d$ ttdddttdddg W d Q R X t jt|d* ttjtdddttdddg W d Q R X t jt|d$ tttdddtdddg W d Q R X t jt|d* ttjttdddtdddg W d Q R X d S )Nz5Input has different freq=D from PeriodIndex\(freq=M\))r4   z2011-01r   )r   r   )r=   r>   r   r   r   r   r   r   )r   rD   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 dddgdd}tj|| t dttdddg}t dddgdd}tj|| t tdddtdg}t dddgdd}tj|| d S )Nz2011-01r   )r   r   z
2011-01-01r   z
2012-01-01)r   r   r   r   r   )r   rE   rF   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 Q R X |j|jdd}tj|| d	}tjt|d" t|jj|j	d
|j
d W d Q R X t|jj|j|j
d}|j|dd}tj|| d S )Nz2007-01r*   r.   r   )namer%   r   z<class .*PeriodIndex'>)r4   )rk   z Should be numpy array of type i8i8)r   )r   r=   r>   r#   _simple_new_datar   r   r"   Zastyper   r<   )r   rE   r   rD   r   r   r   r   test_constructor_simple_newA  s    &z+TestPeriodIndex.test_constructor_simple_newc             C   sV   t g ddd}tjtdd |j|dd W d Q R X |j|jdd}tj|| d S )Nr   r*   )r   rk   z<class .*PeriodIndex'>)r4   )rk   )r   r=   r>   r#   rm   rn   r   r   )r   rE   r   r   r   r   !test_constructor_simple_new_emptyS  s
    z1TestPeriodIndex.test_constructor_simple_new_emptyfloatsg?g @c             C   sN   t jtdd tj| W d Q R X d}t jt|d t| W d Q R X d S )Nz<class )r4   z9PeriodIndex does not allow floating point in construction)r=   r>   r#   r   rm   rW   )r   rq   rD   r   r   r   test_constructor_floats\  s
    z'TestPeriodIndex.test_constructor_floatsc             C   sX   d}t jt|d tdddd W d Q R X t jt|d tdddd W d Q R X d S )Nzstart and end must not be NaT)r4   r   z
2011-01-01r   )r)   rK   r   )r=   r>   r?   r   )r   rD   r   r   r   test_constructor_nate  s
    z$TestPeriodIndex.test_constructor_natc             C   s^   t jdddg}|d }t||d}dd t||D }ttt|}t|}tj|| d S )Ni  i  i  i  )r2   r3   c             S   s&   g | ]}|d  dd|d dqS )r   dQr/   r   ).0tr   r   r   
<listcomp>p  s    zETestPeriodIndex.test_constructor_year_and_quarter.<locals>.<listcomp>)	r`   r   r   ziprQ   mapr   r   r   )r   r2   r3   rE   strsZlopsr*   r   r   r   !test_constructor_year_and_quarterl  s    z1TestPeriodIndex.test_constructor_year_and_quarterc             C   s  t dddd}tddddgdd}tj|| t d	d
dd}td	ddddgdd}tj|| t dddd}tdddgdd}tj|| d}tjt|d tdgdd W d Q R X d}tjt|d tdgdd W d Q R X d}tjt|d t dddd W d Q R X d S )Nz2014-01r6   r-   )r)   r   r%   z2014-03z2014-05z2014-07)r   z
2014-01-02z
2014-01-15rb   )r)   rK   r   z
2014-01-05z
2014-01-08z
2014-01-11z
2014-01-14z2014-01-01 17:00Z4Hr7   )rK   r   r%   z2014-01-01 09:00z2014-01-01 13:00z;Frequency must be positive, because it represents span: -1M)r4   z2011-01z-1Mz:Frequency must be positive, because it represents span: 0MZ0M)r%   r   )r   r   r   r   r=   r>   r?   )r   pidxr   rD   r   r   r   test_constructor_freq_multu  s*    z*TestPeriodIndex.test_constructor_freq_multr   rT   r   r   TSmultr/   r.   r7   r-   r0   c             C   s>   t || }td|dd}td|ddj|}tj|| d S )Nz
2014-04-01r(   )r)   r   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   sj   x,dD ]$}t ddg|d}t ddgdd}qW x6dD ].}tdd	|d
}t ddgdd}tj|| q4W d S )N1D1H1H1Dz
2016-01-01z
2016-01-02)r   z2016-01-01 00:00z2016-01-02 00:00Z25Hr.   )r)   r%   r   z2016-01-02 01:00)r   r   )r   r   )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tt dddd}t|d)ks<tt dddd}t|d*ksZtt d
ddd}t|d,ksxtt dddd}t|d-kstt dddd}t|d.kstt dddd}t|d/kstt dddd}t|d1k sttdd}t |dd}t|dkst|j|jks,t|d |ks>ttdd}t |dd}t|dksft|j|jksxt|d2 |ksttdd!}t |dd}t|t|kst||kj st|j|jksttd"d}t ||d#}t|td$dg}t|dkst|d |ks&tttj|td$dg}t|dksPt|d |ksbt|tdd%g}d&}t	j
t|d' t| W d Q R X tj|}t	j
t|d' t| W d Q R X t	j
td(d' tdd3 W d Q R X d S )4NrT   z1/1/2001z	12/1/2009)r   r)   rK   	   ru   r-   r      r   z
12/31/2009im  r.   r'   i  Hz12/31/2001 23:00   ZMinz1/1/2001 23:59<   r   z1/1/2001 23:59:59z02-Apr-2005r1   )r)   r%   r   z
2006-12-31Wr(   )rK   r%   r/   Z1wz
2005-05-01)r)   rK   z
2005-05-05wz9Input has different freq=W-SUN from PeriodIndex\(freq=B\))r4   zpass as a string instead$   l   i  i  i-	  i8"  i  i  iQ )r   r/   )r   rf   r#   r   r   allr   r   r   r=   r>   r   rW   )r   pir)   i1Zend_intvi2r\   rD   r   r   r   test_constructor  s`    




z TestPeriodIndex.test_constructorru   r'   LUrJ   r   c             C   s,   t d|dd}t|j|d}tj|| d S )Nz
2001/04/01r/   )r)   r   r%   )r   )r   r   r   r   r   )r   r   orgrE   r   r   r   test_recreate_from_data  s    z'TestPeriodIndex.test_recreate_from_datac             C   sf   dddg}t |dd}tdd |D }|jt}t|ts@ttdd	 |D sVttj|| d S )
Ni  i  i  rT   )r   c             S   s   g | ]}t |qS r   )r   )rv   numr   r   r   rx     s    zDTestPeriodIndex.test_map_with_string_constructor.<locals>.<listcomp>c             s   s   | ]}t |tV  qd S )N)re   r   )rv   Zresir   r   r   	<genexpr>  s    zCTestPeriodIndex.test_map_with_string_constructor.<locals>.<genexpr>)	r   r   rz   r   re   r#   r   r   r   )r   rawr+   r   rc   r   r   r    test_map_with_string_constructor  s    

z0TestPeriodIndex.test_map_with_string_constructor))__name__
__module____qualname__r   r    r=   markZparametrizerQ   r   r   r   r   Z_from_sequencer&   r,   rG   rI   rL   rO   rR   rS   rY   r]   ra   rd   rg   rh   ri   rj   ro   rp   rr   rs   r|   r~   r   r   r   r   r   r   r   r   r   r      s@   	*	(	(		"		D&r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestSeriesPeriodc             C   s   t tdddd| _d S )Nz
2000-01-01r(   r   )r%   r   )r   r   r_   )r   methodr   r   r   setup_method  s    zTestSeriesPeriod.setup_methodc             C   s8   d}t jt|d ttddddtd W d Q R X d S )Nz(Cannot cast PeriodArray to dtype float64)r4   z
2000-01-01r(   r   )r%   r   )r   )r=   r>   rW   r   r   float)r   rD   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j|| d S )Nz1/1/2000r(   )r%   r   )r   )r   r   r   r   Zassert_series_equal)r   srF   r   r   r   test_constructor_cast_object  s    z-TestSeriesPeriod.test_constructor_cast_objectN)r   r   r   r   r   r   r   r   r   r   r     s   r   )Znumpyr   r=   Zpandas._libs.tslibs.periodr   Zpandas.core.dtypes.dtypesr   Zpandasr`   r   r   r   r   r   r	   r
   r   Zpandas._testingZ_testingr   Zpandas.core.arraysr   r   r   r   r   r   r   <module>   s   (

   p