U
    f/e:                     @   s   d dl Zd dl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mZ d dlmZ d dlmZ G dd deZdd Zd	d
 Zejdddgdd ZdS )    N)IncompatibleFrequency)	DatetimeIndexIndexNaTPeriodPeriodIndexSeries
date_rangeoffsetsperiod_range)DatetimeLikec                       s>  e Zd ZeZejedddZeje	
deddddddd	 gd
dgddd Zejjdd fddZe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d&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"d:d; Z#d<d= Z$d>d? Z%d@dA Z&  Z'S )BTestPeriodIndex)returnc                 C   s   t ddddS )N20130101   Dperiodsfreqr   self r   K/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/indexes/period/test_period.pysimple_index   s    zTestPeriodIndex.simple_index
   r   r   r   NZ	index_incZ	index_dec)paramsidsc                 C   s   |j S N)param)r   requestr   r   r   index   s    zTestPeriodIndex.indexz"Goes through a generate_range path)reasonc                    s   t    d S r   )supertest_pickle_compat_constructionr   	__class__r   r   r%   &   s    z/TestPeriodIndex.test_pickle_compat_constructionr   MAc                 C   s0   t ddttjg|d}t|}t|| d S )Nz
2016-05-16r   r   )r   r   npNaNtmround_trip_pickleassert_index_equal)r   r   idxresultr   r   r   test_pickle_round_trip*   s    
z&TestPeriodIndex.test_pickle_round_tripc                 C   s   d S r   r   r   r   r   r   
test_where0   s    zTestPeriodIndex.test_wherec              	   C   sP   d}t jt|d tj W 5 Q R X d}t jt|d tg j W 5 Q R X d S )Nz:type object 'DatetimeIndex' has no attribute 'millisecond'matchz5'DatetimeIndex' object has no attribute 'millisecond')pytestraisesAttributeErrorr   Zmillisecond)r   msgr   r   r   test_no_millisecond_field4   s    z)TestPeriodIndex.test_no_millisecond_fieldc                 C   s,   t dddd}td|d}t|ts(td S )Nr)   1/1/2001	12/1/2009r   startend   r"   )r   r   
isinstanceAssertionError)r   r"   Zseriesr   r   r   test_make_time_series=   s    z%TestPeriodIndex.test_make_time_seriesc                 C   s   t g dd}tjg tjd}t|d| t|j| t dtgdd}tjddgtjd}t|d| t|j| tjddgtjd}t d	tgd
d}t|d| t|j| d S )Nr(   r*   dtypei82011-01           :  
2011-01-01r   )	r   r+   arrayint64r-   assert_numpy_array_equalviewasi8r   r   r0   expr   r   r   test_view_asi8B   s    zTestPeriodIndex.test_view_asi8c                 C   s.  t g dd}tjg td}t|j| t| | tjg tjd}t|j	| t dt
gdd}tjtdddt
gtd}t|j| t| | tjddgtjd}t|j	| t dt
gdd}tjtdddt
gtd}t|j| t| | tjd	dgtjd}t|j	| d S )
Nr(   r*   rE   rH   rI   rJ   rL   r   rK   )r   r+   rM   objectr-   rO   valuesZto_numpyrN   rQ   r   r   rR   r   r   r   test_valuesT   s$    zTestPeriodIndex.test_valuesc              	   C   st  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}t |dd}t|dkst|j|jkst|d |ksttdd}t |dd}t|dkst|j|jkst|d |ksttdd}t |dd}t|t|kst||k s$t|j|jks6td}tjt|d t ||d W 5 Q R X tdd}t ||d}d}tjt|d t |d W 5 Q R X t	|tddg}t|dkst|d |kstt	t
|tddg}t|dkst|d |kst|tddg}d}tjt|d t	| W 5 Q R X t
|}tjt|d t	| W 5 Q R X d S )Nr)   r;   r<   r=   	   Q$   r(   l   z02-Apr-2005B   )r>   r   r   
2006-12-31Wr   r?   r   r   Z1wz!start and end must have same freqr4   )r>   r?   z
2005-05-01zOOf the three parameters: start, end, and periods, exactly two must be specified)r>   z
2005-05-05   wz9Input has different freq=W-SUN from PeriodIndex\(freq=B\))r   lenrC   r   r   allr6   r7   
ValueErrorr   r+   rM   r   )r   pir>   i1end_intvi2r9   valsr   r   r   test_period_index_lengthn   sV    




z(TestPeriodIndex.test_period_index_lengthc                 C   s   t dddd}| | t dddd}| | t dddd}| | t d	d
dd}| | t dd
dd}| | t dddd}| | t dddd}| | t dddd}| | tdd}t |dd}| | d S )Nr)   r;   z	12/1/2005r=   rY   z	12/1/2002r(   z1/1/2002r   z	12/1/2001z6/1/2001r\   Hz
12/31/2001z1/1/2002 23:00ZMinz1/1/2002 00:20Sz12/31/2001 00:00:00z12/31/2001 00:05:00r^   r_   r   r`   )r   _check_all_fieldsr   )r   rf   rh   rg   r   r   r   test_fields   s.    






  

zTestPeriodIndex.test_fieldsc           
      C   s   ddddddddd	d
dddddg}t |}t|}|D ]}t||}t|t|ksXtt||D ]\}}t|||ksbtqbt|dkrq6t|j|}	t|t|	kstt||	D ]\}}t|||kstqq6d S )NyearmonthdayhourminutesecondZ
weekofyearweekZ	dayofweekZday_of_weekZ	dayofyearZday_of_yearZquarterZqyearZdays_in_monthr   )listr   getattrrc   rC   zipdt)
r   Zperiodindexfieldsr   sfieldZ	field_idxxvalZfield_sr   r   r   rn      s:    
z!TestPeriodIndex._check_all_fieldsc                 C   s   dd }| }| |st| | r,t| | s>t| |     s`t|  |srt| }d|_| |st| |d d  rt| |drt| |drt| |d rt| |d rtd S )Nc                   S   s   t ddddS )Nr)   r;   r<   r=   r   r   r   r   r   <lambda>       z*TestPeriodIndex.test_is_.<locals>.<lambda>ZAppler(   r)   ra   r   )is_rC   rP   nameZasfreq)r   Zcreate_indexr"   Zind2r   r   r   test_is_   s    "zTestPeriodIndex.test_is_c                 C   s   t dddddgdd}ttjt||d}|d }|dd	 }t|| d|d d < |dd	 dk srt	t dddddgdd}ttjt||d}|d }||dk }t|| d S )
N      A-JUNr*   rA   Z2007r@      )
r   r   r+   randomZrandnrc   r-   Zassert_series_equalrd   rC   )r   r0   tsr1   expectedr   r   r   test_index_duplicate_periods  s    z,TestPeriodIndex.test_index_duplicate_periodsc                 C   sL   t dddddgdd}t dddgdd}t| | | dksHtd S )Nr   r   r   r   r*   r   )r   r-   r/   uniqueZnuniquerC   )r   r0   r   r   r   r   test_index_unique  s    z!TestPeriodIndex.test_index_uniquec                 C   s   d S r   r   r   r   r   r   
test_shift  s    zTestPeriodIndex.test_shiftc                 C   sR   t ddd t ddd tdddgdd}ttdddgdd}t|| d S )Nir)   )ordinalr   r   r   r@   )r   r   r+   rM   r-   r/   )r   Zidx1Zidx2r   r   r   test_negative_ordinals  s
    z&TestPeriodIndex.test_negative_ordinalsc                 C   sl   t dddddgddd}td	d	d
ddgtjdd}t|j| tddd
ddgtjdd}t|j| d S )NrH   z2011-02r   z2012-03z2012-04r   r   )r   r   i  r   i  )rF   r   r@   ra   r      )r   r   r+   rN   r-   r/   rp   rq   rR   r   r   r   test_pindex_fieldaccessor_nat'  s      z-TestPeriodIndex.test_pindex_fieldaccessor_natc                 C   sJ   t dddddgdd}ttjt||d }|d	 |d
 ksFtd S )NZ2Q05Z3Q05Z4Q05Z1Q06Z2Q06rY   r*   rA   Z05Q4ra   )r   r   r+   r   Zrandrc   ZcumsumrC   )r   rf   r|   r   r   r   test_pindex_qaccess1  s    z#TestPeriodIndex.test_pindex_qaccessc                 C   s   t ddddddgdd}td	d
dd}t|| |jtdksFt|jdksTttd	ddd}t|| |jtdkst|jdkstd S )NrH   z2011-03z2011-05z2011-07z2011-09z2011-11Z2Mr*   z1/1/11z12/31/11)r>   r?   r   ra      r>   r   r   )	r   r   r-   r/   r   r
   MonthEndrC   freqstr)r   r   rf   r   r   r   test_pindex_multiples7  s    z%TestPeriodIndex.test_pindex_multiplesc                 C   s@   t dddd}t|}t|d ts(t|d j|jks<td S )Nz1/1/10r   r\   r   r   )r   rw   rB   r   rC   r   )r   r"   r1   r   r   r   test_iterationG  s    zTestPeriodIndex.test_iterationc                 C   sf   t dddd}|jdd|jg}tddd	d
g|}t|jjd tsHtt|jj	d d t
sbtd S )N1/1/2012r   Z12Hr   r   r*   r   r@   ra   r   )r	   	to_periodrs   r   rB   r"   levelsr   rC   rV   r   )r   r"   Zindex_as_arraysr|   r   r   r   test_with_multi_indexN  s
    z%TestPeriodIndex.test_with_multi_indexc                 C   s<   t dddd}t|}|jt ks*t|jdks8td S )Nz1/1/2011r   r(   r*   )r   r-   r.   r   r
   r   rC   r   )r   ZprngZnew_prngr   r   r   test_pickle_freqY  s    
z TestPeriodIndex.test_pickle_freqc                 C   sB   t dddgdd}|dd }tdd	 |D }t|| d S )
Ni  r   r   r)   r*   c                 S   s   | j S r   r   )r~   r   r   r   r   d  r   z*TestPeriodIndex.test_map.<locals>.<lambda>c                 S   s   g | ]
}|j qS r   r   ).0r~   r   r   r   
<listcomp>e  s     z,TestPeriodIndex.test_map.<locals>.<listcomp>)r   mapr   r-   r/   )r   r"   r1   rS   r   r   r   test_map`  s    zTestPeriodIndex.test_mapc                 C   s8   | j g dd}| g kst|jdddgks4td S )Nr)   r*   T)r    )
_index_clsformatrC   )r   Z	empty_idxr   r   r   test_format_emptyh  s    z!TestPeriodIndex.test_format_empty)(__name__
__module____qualname__r   r   r6   Zfixturer   r   r-   ZmakePeriodIndexr   r"   markZxfailr%   parametrizer2   r3   r:   rD   rT   rW   rk   ro   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r&   r   r      sF   

	<"$
r   c               	   C   sr   t ddgdd} td}| |dks,t| ddks>tt }d}tjt|d | | W 5 Q R X d S )N2000Z2001r   r*   ra   z5Input has different freq=B from PeriodIndex\(freq=D\)r4   )	r   r
   ZDayZ_maybe_convert_timedeltarC   ZBusinessDayr6   r7   re   )rf   offsetr9   r   r   r   test_maybe_convert_timedeltao  s    
r   c            
      C   sf  t ddd} | d}t| d}||j| | j||jfD ]6}t|trR|jsRt|js\t|j	rft|j
s:tq:| dt}|d}t|d}||j||j||jfD ]6}t|tr|jrt|jrt|j	rt|j
stq| dt}|d}t|d}	||j||j|	|	jfD ]B}t|tr:|jr:t|jrFt|j	rRt|j
stqd S )Nz
2016-01-01r   )r   r   ztimedelta64[ns]r   rl   )r	   r   r   rP   Z_enginerB   Zis_monotonicrC   Zis_monotonic_increasingZis_monotonic_decreasingZ	is_uniqueinsertr   )
Zdtirf   ZtdiobjZdti1Zpi1Ztdi1Zdti2Zpi2Ztdi2r   r   r   test_is_monotonic_with_nat{  s6    










r   rM   TFc              
   C   s  t ddgdd}| r|j}tj|d |d gtd}t|}t|| t|}t|| |j}dd	tj	fD ]8}tj||d}t|| tj||d}t|| qrd
D ]^}d}t
jt|d tj||d W 5 Q R X t
jt|d tj|tt|d W 5 Q R X qd S )Nz
2000-01-01z
2001-01-01r   r*   r   r@   rE   rG   rN   )Zfloat64Zint32Zuint64zargument must ber4   )r   _datar+   rM   rU   r-   rO   ZasarrayrQ   rN   r6   r7   	TypeErrorrx   )rM   r   r   r1   rF   r9   r   r   r   test_dunder_array  s(    

r   )Znumpyr+   r6   Zpandas._libs.tslibs.periodr   Zpandasr   r   r   r   r   r   r	   r
   r   Zpandas._testingZ_testingr-   Z!pandas.tests.indexes.datetimeliker   r   r   r   r   r   r   r   r   r   r   <module>   s   ,  \)