3
Êb_&  ã               @   sX   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 d dljZG dd„ dƒZdS )é    )ÚproductN)Ú	DataFrameÚ
MultiIndexÚPeriodÚSeriesÚ	TimedeltaÚ	Timestampc            	   @   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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 ƒgƒd+d,„ ƒZd-d.„ Zd/S )0ÚTestCountingc             C   sh   t dgdgdgdgdggdgd�}|jdƒ}|j}tdddddgƒ}tj||jƒ ƒ tj||jƒ ƒ d S )	NÚaÚbÚA)Úcolumnsr   é   é   é   )r   Úgroupbyr   r   ÚtmÚassert_series_equalÚcumcount)ÚselfÚdfÚgÚsgÚexpected© r   úD/tmp/pip-build-jkwsxyiy/pandas/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j||jƒ ƒ tj||jƒ ƒ 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�}|jdƒ}|j}tddddd	gdgd d
�}tj||jƒ ƒ tj||jƒ ƒ d S )Nr
   r   r   r   é   )r   Úindexr   r   r   )r&   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   Útest_cumcount_dupe_index   s    (
z%TestCounting.test_cumcount_dupe_indexc             C   s–   t j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�}|jdƒ}|j}tdddddg|d	�}tj||jƒ ƒ tj||jƒ ƒ d S )
Nr   r   r   r
   r   r   )r   r&   r   )r&   )	r   Úfrom_tuplesr   r   r   r   r   r   r   )r   Úmir   r   r   r   r   r   r   Útest_cumcount_mi+   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�}|jdddddgƒ}|j}tddddd	gdgd d
�}tj||jƒ ƒ tj||jƒ ƒ d S )Nr
   r   r   r   r%   )r   r&   r   r   r   )r&   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   Útest_cumcount_groupby_not_col6   s    (z*TestCounting.test_cumcount_groupby_not_colc             C   sV   t dtdƒiƒ}|jdƒ}|j}tdddddgƒ}tj||jƒ ƒ tj||jƒ ƒ d S )Nr   Úaaabar   r   )r   Úlistr   r   r   r   r   Úngroup)r   r   r   r   r   r   r   r   Útest_ngroupB   s    
zTestCounting.test_ngroupc             C   sT   t dtdƒiƒ}|jdƒ}|j}ttdƒdd�}tj||jƒ ƒ tj||jƒ ƒ d S )Nr   Zabcder%   r   )r   )	r   r-   r   r   r   Úranger   r   r.   )r   r   r   r   r   r   r   r   Útest_ngroup_distinctL   s    
z!TestCounting.test_ngroup_distinctc             C   sT   t ddgd iƒ}|jdƒ}|j}tdgd ƒ}tj||jƒ ƒ tj||jƒ ƒ d S )Nr   r   r%   )r   r   r   r   r   r   r.   )r   r   r   r   r   r   r   r   Útest_ngroup_one_groupV   s    
z"TestCounting.test_ngroup_one_groupc             C   sN   t ƒ jdd�}ttd�jdd�}tdd�}tj||jƒ ƒ tj||jƒ ƒ d S )Nr   )r   )r   r   )r   r   r   r    r   r   r.   )r   r!   r"   r#   r   r   r   Útest_ngroup_empty`   s
    
zTestCounting.test_ngroup_emptyc             C   s@   t dtdƒiƒ}ttdƒƒ}tj|j|ƒjƒ |j|ƒjƒ ƒ d S )Nr   r,   )r   r-   r   r   r   r   r.   )r   r   Úsr   r   r   Ú test_ngroup_series_matches_framej   s    z-TestCounting.test_ngroup_series_matches_framec             C   sj   t dtdƒidgd d�}|jdƒ}|j}tdddddgdgd d�}tj||jƒ ƒ tj||jƒ ƒ d S )Nr   r,   r   r%   )r&   r   )r   r-   r   r   r   r   r   r.   )r   r   r   r   r   r   r   r   Útest_ngroup_dupe_indexp   s    
z#TestCounting.test_ngroup_dupe_indexc             C   s†   t jddgddgddgddgddggƒ}tdtdƒi|d�}|jdƒ}|j}tdddddg|d�}tj||j	ƒ ƒ tj||j	ƒ ƒ d S )Nr   r   r   r   r,   )r&   )
r   r(   r   r-   r   r   r   r   r   r.   )r   r)   r   r   r   r   r   r   r   Útest_ngroup_miz   s    (
zTestCounting.test_ngroup_mic             C   st   t dtdƒidgd d�}|jdddddgƒ}|j}tdddddgdgd d�}tj||jƒ ƒ tj||jƒ ƒ d S )Nr   r,   r   r%   )r&   r   )r   r-   r   r   r   r   r   r.   )r   r   r   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�}|jdgƒ}tdddddgƒ}tdddddgƒ}tj||jd | ƒ tj||jdd�ƒ tj||jd	d�ƒ d S )
Nr
   r   r   )r   r   r   T)Ú	ascendingF)r   r   r   r   r   Zngroupsr.   )r   r   r   r9   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�}|jddgƒ}|jƒ }|jƒ }tdd	d
ddgƒ}tdddd	dgƒ}tj||ƒ tj||ƒ d S )Nr
   ÚxÚyr   r   ÚX)r   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’   xŒt tdƒdd�D ]x‰tdˆiƒ}|jdgƒ}ttˆƒƒ‰ ‡ fdd„ˆD ƒ}‡fdd„tˆƒD ƒ}tj|j	ƒ t
|ƒƒ tj|jƒ t
|ƒƒ qW d S )Nr   é   )Úrepeatr
   c                s   g | ]}ˆ j |ƒ‘qS r   )r&   )Ú.0Úval)Úorderr   r   ú
<listcomp>¯   s    z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>c                s"   g | ]\}}ˆ d |… j |ƒ‘qS )N)Úcount)rA   ÚirB   )Úpr   r   rD   °   s    )r   r0   r   r   ÚsortedÚsetÚ	enumerater   r   r.   r   r   )r   r   r   ZngroupdZ
cumcountedr   )rC   rG   r   Útest_ngroup_cumcount_pair¨   s    z&TestCounting.test_ngroup_cumcount_pairc       	      C   sÜ   t jjdƒ tdt jjtdƒdƒiƒ}x°dD ]¨}|jdg|d�}d|d	< d|d
< xNt|ƒD ]B\}\}}||j|j	d	f< x$t|j	ƒD ]\}}||j|d
f< q‚W qZW t
jt|d	 jƒ|jƒ ƒ t
jt|d
 jƒ|jƒ ƒ q,W d S )Nr   r
   Zabcdeféd   FT)Úsortr   Zgroup_idZgroup_index)FTéÿÿÿÿrN   )ÚnpÚrandomÚseedr   Úchoicer-   r   rJ   Úlocr&   r   r   r   Úvaluesr.   r   )	r   r   Z	sort_flagr   rF   Ú_Ú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 )z2016-05-Ú02dz 20:09:25+00:00)r   )rA   rF   r   r   r   rD   È   s    zTestCounting.<listcomp>r   r?   c             C   s   g | ]}t d |d›d�ƒ‘qS )z2016-05-r[   z	 20:09:25)r   )rA   rF   r   r   r   rD   É   s    c             C   s   g | ]}t |d d�‘qS )Úh)Úunit)r   )rA   r;   r   r   r   rD   Ê   s    c             C   s   g | ]}t d d|d�‘qS )Z2Wiá  )ÚfreqÚyearÚmonth)r   )rA   r;   r   r   r   rD   Ë   s    c             C   sR   t dddg|dœƒ}|jdƒjƒ }t dddgiddgd�}d|j_tj||ƒ d S )	Nr
   r   )r;   r<   r;   r<   r   r   )r&   )r   r   rE   r&   Únamer   Zassert_frame_equal)r   rZ   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œƒ}|jddgƒj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   rd   )ZlevelsÚcodesÚnamesre   )r&   r   ra   F)Zcheck_index_type)r   rO   Únanr   re   rE   r   r   r   r   r   )r   r   Úresultr)   r   r   r   r   Ú(test_count_with_only_nans_in_first_groupØ   s
     z5TestCounting.test_count_with_only_nans_in_first_groupN)Ú__name__Ú
__module__Ú__qualname__r   r$   r'   r*   r+   r/   r1   r2   r3   r5   r6   r7   r8   r:   r>   rK   rY   ÚpytestÚmarkZparametrizer0   rc   rj   r   r   r   r   r	   
   s0   








 r	   )Ú	itertoolsr   ZnumpyrO   rn   Zpandasr   r   r   r   r   r   Zpandas._testingZ_testingr   r	   r   r   r   r   Ú<module>   s
    
