U
    +¿if!2  ã                   @   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    úU/home/mars/bis/venv/lib/python3.8/site-packages/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ƒ}dD ] }|jdg|d}d|d< d|d	< t|ƒD ]>\}\}}||j|j	df< t|j	ƒD ]\}}||j|d	f< q|qVt
 t|d jƒ| ¡ ¡ t
 t|d	 jƒ| ¡ ¡ q*d S )
Nr   r   Zabcdeféd   )FT)ÚsortéÿÿÿÿZgroup_idZgroup_index)ÚnpÚrandomÚseedr   Úchoicer;   r   rZ   Úlocr1   r   r   r   Úvaluesr<   r   )	r   r   Z	sort_flagr   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|   )Z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serÚ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   iöÿÿÿr   )	Ú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_typeI  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_countZ  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!   ÚRaisingObjectExceptionk  s   r¹   c                       s*   e Zd Zd‡ fdd„	Z‡fdd„Z‡  ZS )z8test_count_uses_size_on_exception.<locals>.RaisingObjectúI will raise inside Cythonc                    s   t ƒ  ¡  || _d S rT   )ÚsuperÚ__init__Úmsg)r   r½   ©Ú	__class__r    r!   r¼   o  s    
zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__c                    s   ˆ | j ƒ‚d S rT   )r½   )r   Úother©r¹   r    r!   Ú__eq__s  s    z?test_count_uses_size_on_exception.<locals>.RaisingObject.__eq__)rº   )r‘   r’   r“   r¼   rÂ   Ú__classcell__r    rÁ   r¾   r!   ÚRaisingObjectn  s   rÄ   c                    s   g | ]
}ˆ ƒ ‘qS r    r    )rP   re   )rÄ   r    r!   rS   w  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_exceptionj  s    	$ rÆ   )Ú	itertoolsr   Ústringr   Ú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   (
 p	