U
    sVc1                     @   s   d dl mZ d dlmZ d dl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mZ 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dS )    )product)ascii_lowercaseN)	DataFrameIndex
MultiIndexPeriodSeries	Timedelta	Timestamp
date_rangec                   @   s  e Z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dd Zdd Zdd Zdd  Zd!d" Zejd#d$d% ed&d'D d(d% ed&d'D d)d% ed&d'D d*d% ed&d'D d+d% ed&d'D gd,d- Zd.d/ Zd0d1 Zd2d3 Zd4S )5TestCountingc                 C   sh   t dgdgdgdgdggdgd}|d}|j}tdddddg}t||  t||  d S )	NabAcolumnsr            r   groupbyr   r   tmassert_series_equalcumcountselfdfgsgexpected r    F/tmp/pip-unpacked-wheel-xj8nt62q/pandas/tests/groupby/test_counting.pytest_cumcount   s    "
zTestCounting.test_cumcountc                 C   sN   t  jdd}ttdjdd}tdd}t||  t||  d S Nr   )leveldtypeint64)r   r   r   objectr   r   r   r   geseer    r    r!   test_cumcount_empty   s
    
z TestCounting.test_cumcount_emptyc                 C   sz   t dgdgdgdgdggdgdgd d}|d}|j}tddddd	gdgd d
}t||  t||  d S Nr   r   r   r      r   indexr   r   r   r1   r   r   r    r    r!   test_cumcount_dupe_index)   s      
z%TestCounting.test_cumcount_dupe_indexc                 C   s   t ddgddgddgddgddgg}tdgdgdgdgdggdg|d}|d}|j}tdddddg|d	}t||  t||  d S )
Nr   r   r   r   r   r   r0   r   r2   )	r   from_tuplesr   r   r   r   r   r   r   r   mir   r   r   r   r    r    r!   test_cumcount_mi5   s    ($
zTestCounting.test_cumcount_mic                 C   s   t dgdgdgdgdggdgdgd d}|dddddg}|j}tddddd	gdgd d
}t||  t||  d S r.   r   r   r    r    r!   test_cumcount_groupby_not_col@   s      z*TestCounting.test_cumcount_groupby_not_colc                 C   sV   t dtdi}|d}|j}tdddddg}t||  t||  d S )Nr   aaabar   r   r   listr   r   r   r   r   ngroupr   r    r    r!   test_ngroupL   s    
zTestCounting.test_ngroupc                 C   sT   t dtdi}|d}|j}ttddd}t||  t||  d S )Nr   Zabcder/   r'   r%   )	r   r;   r   r   r   ranger   r   r<   r   r    r    r!   test_ngroup_distinctV   s    
z!TestCounting.test_ngroup_distinctc                 C   sT   t ddgd i}|d}|j}tdgd }t||  t||  d S )Nr   r   r/   )r   r   r   r   r   r   r<   r   r    r    r!   test_ngroup_one_group`   s    
z"TestCounting.test_ngroup_one_groupc                 C   sN   t  jdd}ttdjdd}tdd}t||  t||  d S r#   )r   r   r   r(   r   r   r<   r)   r    r    r!   test_ngroup_emptyj   s
    
zTestCounting.test_ngroup_emptyc                 C   s@   t dtdi}ttd}t|| ||  d S )Nr   r9   )r   r;   r   r   r   r   r<   )r   r   sr    r    r!    test_ngroup_series_matches_framet   s    z-TestCounting.test_ngroup_series_matches_framec                 C   sj   t dtdidgd d}|d}|j}tdddddgdgd d}t||  t||  d S Nr   r9   r   r/   r2   r   r:   r   r    r    r!   test_ngroup_dupe_indexz   s    
z#TestCounting.test_ngroup_dupe_indexc                 C   s   t ddgddgddgddgddgg}tdtdi|d}|d}|j}tdddddg|d}t||	  t||	  d S )Nr   r   r   r   r9   r2   )
r   r4   r   r;   r   r   r   r   r   r<   r5   r    r    r!   test_ngroup_mi   s    (
zTestCounting.test_ngroup_mic                 C   st   t dtdidgd d}|dddddg}|j}tdddddgdgd d}t||  t||  d S rD   r:   r   r    r    r!   test_ngroup_groupby_not_col   s    z(TestCounting.test_ngroup_groupby_not_colc                 C   s   t dddddgdgd}|dg}tdddddg}tdddddg}t||jd |  t||jdd t||jd	d d S )
Nr   r   r   r   r   r   T)	ascendingF)r   r   r   r   r   Zngroupsr<   )r   r   r   rH   Z
descendingr    r    r!   test_ngroup_descending   s    z#TestCounting.test_ngroup_descendingc                 C   s   t ddgddgddgddgddggddgd}|ddg}| }| }tdd	d
ddg}tdddd	dg}t|| t|| d S )Nr   xyr   r   Xr   r   r   r   r   )r   r   r<   r   r   r   r   )r   r   r   Zg_ngroupZ
g_cumcountZexpected_ngroupZexpected_cumcountr    r    r!   test_ngroup_matches_cumcount   s     z)TestCounting.test_ngroup_matches_cumcountc                    s   t tdddD ]xtdi}|dg}tt  fddD }fddtD }t|	 t
| t| t
| qd S )Nr      )repeatr   c                    s   g | ]}  |qS r    r2   ).0val)orderr    r!   
<listcomp>   s     z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>c                    s"   g | ]\}} d |  |qS N)count)rP   irQ   )pr    r!   rS      s     )r   r>   r   r   sortedset	enumerater   r   r<   r   r   )r   r   r   ZngroupdZ
cumcountedr    )rR   rW   r!   test_ngroup_cumcount_pair   s    z&TestCounting.test_ngroup_cumcount_pairc           	      C   s   t jd tdt jtddi}|jd|d}d|d< d|d< t|D ]>\}\}}||j|j	df< t|j	D ]\}}||j|df< qrqLt
t|d j|  t
t|d j|  d S )	Nr   r   Zabcdefd   )sortZgroup_idZgroup_index)nprandomseedr   choicer;   r   rZ   locr1   r   r   r   valuesr<   r   )	r   r]   r   r   rV   _groupjindr    r    r!   "test_ngroup_respects_groupby_order   s    z/TestCounting.test_ngroup_respects_groupby_orderdatetimelikec                 C   s   g | ]}t d |ddqS )2016-05-02dz 20:09:25+00:00r
   rP   rV   r    r    r!   rS      s     zTestCounting.<listcomp>r   rN   c                 C   s   g | ]}t d |ddqS )rk   rl   	 20:09:25rm   rn   r    r    r!   rS      s     c                 C   s"   g | ]}t d |ddddqS )rk   rl   ro   UTC)tzrm   rn   r    r    r!   rS      s     c                 C   s   g | ]}t |d dqS )h)unit)r	   rP   rJ   r    r    r!   rS      s     c                 C   s   g | ]}t d d|dqS )Z2Wi  )freqyearmonth)r   rt   r    r    r!   rS      s     c                 C   sR   t dddg|d}|d }t dddgiddgd}d|j_t|| d S )	Nr   r   )rJ   rK   rJ   rK   r   r   r2   )r   r   rU   r1   namer   assert_frame_equal)r   rj   r   resr   r    r    r!   test_count_with_datetimelike   s
    z)TestCounting.test_count_with_datetimelikec                 C   sx   t tjtjgddgddgd}|ddgj }tg ddggg g gddgd}tg |tjd	d
}t	j
||dd d S )Nr   r   r   r   )r   BCr   r|   )levelscodesnamesr}   )r1   r&   rx   F)Zcheck_index_type)r   r_   nanr   r}   rU   r   r   r'   r   r   )r   r   resultr6   r   r    r    r!   (test_count_with_only_nans_in_first_group   s
     z5TestCounting.test_count_with_only_nans_in_first_groupc                 C   sl   t dddddgddtjddgd}|dg }t tdd	d
dgdddddddgid}t|| d S )Nr   r/   rN   r   r   )r   r|   r|   g        g      @g      @g      @rx   r   )r1   data)r   r_   NaNr   rU   r   r   ry   )r   r   rz   r   r    r    r!   4test_count_groupby_column_with_nan_in_groupby_column   s    $ zATestCounting.test_count_groupby_column_with_nan_in_groupby_columnc                 C   s   t dddd}ttd|tdgd}|dd }| }ttdtd|gd}|dd }| }t|| d S )	Nz1/1/2012Z5min
   )startru   periodsr2   c                 S   s   | d d dkS )Nr   r   r   r    rJ   r    r    r!   <lambda>       z@TestCounting.test_groupby_count_dateparseerror.<locals>.<lambda>c                 S   s   | d d dkS )Nr   r   r    r   r    r    r!   r      r   )r   r   r_   aranger   rU   r   r   )r   drZserZgroupedr   r   r    r    r!   !test_groupby_count_dateparseerror   s    z.TestCounting.test_groupby_count_dateparseerrorN)__name__
__module____qualname__r"   r-   r3   r7   r8   r=   r?   r@   rA   rC   rE   rF   rG   rI   rM   r[   ri   pytestmarkZparametrizer>   r{   r   r   r   r    r    r    r!   r      s<   











	r   c                  C   s\   t tdtddd} tddgtddgdd	d
d}| dj	 }t
|| d S )NababrN   ztimedelta64[ns])r   deltr   r   r   r   r   r   r1   rx   )r   r;   r_   r   astyper   r   r   r   rU   r   r   )r   r   r   r    r    r!   #test_groupby_timedelta_cython_count  s    r   c                  C   sJ  d} t d| d dd}ttjtt| tjdd| tj| 	dtjd	d| tj|| tj| 	dtj| 	dtj|| tj|d
 tjtt| d	}|j
dddgD ]"}tj|jtj| | d |f< q|d d|d< ddddgfD ]<}|| }||tjj|d
d}t|| qd S )Ni   z
2015-08-30r   T)r   ru   r   r/   r   ir   )	1st2ndZ3rd4thZ5thZ6thZ7thZ8th9thr   r   r   r   category)Zaxis)r   r   r_   r`   rb   r;   r   randintZrandnroundr   Zdropr   rc   r   r   rU   applyr   ry   )nr   r   colkeyleftrightr    r    r!   
test_count  s*     r   c                  C   s   t dddgdtjdgdtjtjggdddgd	} | d }| jdd
d }t ddgddggddgddgd}d|j_t||	  t|| | dd  }t
||d  d S )Nr   r   Zfoobarr   r   r|   r}   r   F)Zas_indexr   r0   )r   r_   r   r   rU   r1   rx   r   ry   Zreset_indexr   )r   Zcount_asZcount_not_asr   Zcount_Br    r    r!   test_count_non_nulls(  s     "r   c                  C   s   t dgd dgd  dgd dgd  d} | dj }tddgtddgdddd}t|| t dtj	tj	gdgd  dgd dgd  d} | dj }td	dgtddgdddd}t|| d S )
Nr   r   r   r   )r   cr   r   r   r   )
r   r   r   rU   r   r   r   r   r_   r   r   r   r   r    r    r!   test_count_object<  s    .2r   c                  C   s   t t jdddt jdddf} t| ddddgd	}t j||dk< |ddg }d
D ]F}|d ||d< |d ||d< |ddg }t	
|| q^d S )Nr   r/   )r\   r   r   r   r   r   dr   )Zfloat32r(   )r_   Zhstackr`   r   r   r   r   rU   r   r   ry   )valsr   r   tr   r    r    r!   test_count_cross_typeH  s    r   c               	   C   s   t tddddgtjtddddgtjtdddd	gtjtd
d} | d }t ddgddgddgdt	tdddd}t
|| d S )Nr   r   r   r\   r      rN   r/      r   )r   r   r   grpr   )r   r   r   abr   r2   )r   r_   arrayZint8Zuint32Zint16r;   r   rU   r   r   ry   r   r    r    r!   test_lower_int_prec_countY  s     r   c                     s   G dd dt G fddd t fddtdD tdd	} | d
 }tdddgittdd
dd}t|| d S )Nc                   @   s   e Zd ZdS )zAtest_count_uses_size_on_exception.<locals>.RaisingObjectExceptionN)r   r   r   r    r    r    r!   RaisingObjectExceptionj  s   r   c                       s0   e Zd Zddd fddZfddZ  ZS )	z8test_count_uses_size_on_exception.<locals>.RaisingObjectI will raise inside CythonN)returnc                    s   t    || _d S rT   )super__init__msg)r   r   	__class__r    r!   r   n  s    
zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__c                    s    | j d S rT   )r   )r   otherr   r    r!   __eq__r  s    z?test_count_uses_size_on_exception.<locals>.RaisingObject.__eq__)r   )r   r   r   r   r   __classcell__r    r   r   r!   RaisingObjectm  s   r   c                    s   g | ]
}  qS r    r    )rP   re   )r   r    r!   rS   v  s     z5test_count_uses_size_on_exception.<locals>.<listcomp>rN   r   )r   r   r   r   r   r   r   r2   )		Exceptionr   r>   r;   r   rU   r   r   ry   r   r    )r   r   r!   !test_count_uses_size_on_exceptioni  s    	$ r   )	itertoolsr   stringr   Znumpyr_   r   Zpandasr   r   r   r   r   r	   r
   r   Zpandas._testingZ_testingr   r   r   r   r   r   r   r   r   r    r    r    r!   <module>   s   (
 o	