o
    ·¶ßdÜ1  ã                   @   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   sb   t dgdgdgdgdggdgd}| d¡}|j}tg d¢ƒ}t || ¡ ¡ t || ¡ ¡ d S )NÚaÚbÚA©Úcolumns©r   é   é   r   é   ©r   Úgroupbyr   r   ÚtmÚassert_series_equalÚcumcount©ÚselfÚdfÚgÚsgÚexpected© r!   úV/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/groupby/test_counting.pyÚtest_cumcount   s   "
zTestCounting.test_cumcountc                 C   óN   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   ó
   
z TestCounting.test_cumcount_emptyc                 C   st   t dgdgdgdgdggdgdgd d}| d¡}|j}tg d¢dgd d}t || ¡ ¡ t || ¡ ¡ d S )	Nr   r   r   r   é   ©r   Úindexr   ©r3   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g d¢|d	}t || ¡ ¡ t || ¡ ¡ d S )
Nr   r   r   r   r   r   r2   r   r4   )	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   sx   t dgdgdgdgdggdgdgd d}| g d¢¡}|j}tg d¢dgd d	}t || ¡ ¡ t || ¡ ¡ d S )
Nr   r   r   r   r1   r2   ©r   r   r   r   r   r   r4   r   r   r!   r!   r"   Útest_cumcount_groupby_not_col@   s   "ÿz*TestCounting.test_cumcount_groupby_not_colc                 C   sP   t dtdƒiƒ}| d¡}|j}tg d¢ƒ}t || ¡ ¡ t || ¡ ¡ d S )Nr   Úaaabar:   ©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abcder1   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   r1   )r   r   r   r   r   r   r?   r   r!   r!   r"   Útest_ngroup_one_group`   s   
z"TestCounting.test_ngroup_one_groupc                 C   r$   r%   )r   r   r   r*   r   r   r?   r+   r!   r!   r"   Útest_ngroup_emptyj   r0   zTestCounting.test_ngroup_emptyc                 C   s@   t dtdƒiƒ}ttdƒƒ}t | |¡ ¡ | |¡ ¡ ¡ d S )Nr   r<   )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   sd   t dtdƒidgd d}| d¡}|j}tg d¢dgd d}t || ¡ ¡ t || ¡ ¡ d S ©Nr   r<   r   r1   r4   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g d¢|d}t || 	¡ ¡ t || 	¡ ¡ d S )Nr   r   r   r   r<   r4   r:   )
r   r6   r   r>   r   r   r   r   r   r?   r7   r!   r!   r"   Útest_ngroup_mi„   s   (
zTestCounting.test_ngroup_mic                 C   sh   t dtdƒidgd d}| g d¢¡}|j}tg d¢dgd d}t || ¡ ¡ t || ¡ ¡ d S rG   r=   r   r!   r!   r"   Útest_ngroup_groupby_not_colŽ   s   z(TestCounting.test_ngroup_groupby_not_colc                 C   sx   t g d¢dgd}| dg¡}tg d¢ƒ}tg d¢ƒ}t ||jd | ¡ t ||jdd¡ t ||jd	d¡ d S )
N)r   r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   r   T)Ú	ascendingF)r   r   r   r   r   Zngroupsr?   )r   r   r   rK   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g d¢ƒ}tg d	¢ƒ}t ||¡ t ||¡ d S )
Nr   ÚxÚyr   r   ÚXr   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 ]<‰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!   r4   )Ú.0Úval)Úorderr!   r"   Ú
<listcomp>¹   s    z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>c                    s"   g | ]\}}ˆ d |…   |¡‘qS ©N)Úcount)rS   ÚirT   )Úpr!   r"   rV   º   ó   " )r   rA   r   r   ÚsortedÚsetÚ	enumerater   r   r?   r   r   )r   r   r   ZngroupdZ
cumcountedr!   )rU   rZ   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< q9q&t
 t|d jƒ| ¡ ¡ t
 t|d jƒ| ¡ ¡ d S )	Nr   r   Zabcdeféd   )ÚsortéÿÿÿÿZgroup_idZgroup_index)ÚnpÚrandomÚseedr   Úchoicer>   r   r^   Úlocr3   r   r   r   Úvaluesr?   r   )	r   ra   r   r   rY   Ú_ÚgroupÚjÚindr!   r!   r"   Ú"test_ngroup_respects_groupby_order¿   s   ÿz/TestCounting.test_ngroup_respects_groupby_orderÚdatetimelikec                 C   ó   g | ]}t d |d›dƒ‘qS )ú2016-05-Ú02dz 20:09:25+00:00©r
   ©rS   rY   r!   r!   r"   rV   Ñ   ó    zTestCounting.<listcomp>r   rQ   c                 C   ro   )rp   rq   ú	 20:09:25rr   rs   r!   r!   r"   rV   Ò   rt   c                 C   s"   g | ]}t d |d›ddd‘qS )rp   rq   ru   ÚUTC)Útzrr   rs   r!   r!   r"   rV   Ó   r[   c                 C   s   g | ]}t |d d‘qS )Úh)Úunit)r	   ©rS   rM   r!   r!   r"   rV   Ô   s    c                 C   s   g | ]	}t d d|d‘qS )Z2Wiá  )ÚfreqÚyearÚmonth)r   rz   r!   r!   r"   rV   Õ   s    c                 C   sP   t g d¢|dœƒ}| d¡ ¡ }t dddgiddgd	}d|j_t ||¡ d S )
N)r   r   r   )rM   rN   rM   rN   r   r   r   r   r4   )r   r   rX   r3   Únamer   Úassert_frame_equal)r   rn   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„   )r3   r(   r~   F)Zcheck_index_type)r   rc   Únanr   r„   rX   r   r   r)   r   r   )r   r   Úresultr8   r    r!   r!   r"   Ú(test_count_with_only_nans_in_first_groupâ   s
    z5TestCounting.test_count_with_only_nans_in_first_groupc                 C   s^   t g d¢ddtjddgdœƒ}| dg¡ ¡ }t tg d¢dd	d
g d¢id}t ||¡ d S )N)r   r   r   r   r   r1   rQ   r   r   )r   rƒ   rƒ   )g        g      @g      @g      @©r~   r   )r   r   r   r   )r3   Údata)r   rc   ÚNaNr   rX   r   r   r   )r   r   r€   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é
   )Ústartr{   Úperiodsr4   c                 S   s   | d d dkS )Nr   r   r   r!   ©rM   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   rc   Úaranger   rX   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/   r5   r9   r;   r@   rB   rC   rD   rF   rH   rI   rJ   rL   rP   r_   rm   ÚpytestÚmarkZparametrizerA   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ÚababrQ   ztimedelta64[ns])r   Údeltr   r   r   r   rŠ   rž   ©r3   r~   )r   r>   rc   r”   Úastyper   r   r   rž   rX   r   r   )r   r    rˆ   r!   r!   r"   Ú#test_groupby_timedelta_cython_count  s   ÿr¡   c                  C   sF  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
 g d¢¡D ]}tj|jtj | | d ¡|f< qa|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   r{   r   r1   r   iöÿÿÿr   )	Ú1stÚ2ndZ3rdÚ4thZ5thZ6thZ7thZ8thÚ9th)r£   r¤   r¥   r¦   Úcategoryr£   r¤   )Zaxis)r   r   rc   rd   rf   r>   r   ÚrandintZrandnÚroundr   Zdropr‡   rg   r    r   rX   Úapplyr   r   )Únr•   r   ÚcolÚkeyÚleftÚrightr!   r!   r"   Ú
test_count  s,   ÷ÿ ýr°   c                  C   s¸   t g d¢dtjdgdtjtjggg d¢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 )N)r   r   Zfoor   Úbarr   r‚   r   r   F)Zas_indexr   r   rƒ   r„   r2   )r   rc   r‡   r   rX   r3   r~   r   r   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   rX   r   r   r   r   rc   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| g d¢d}t j||dk< | ddg¡ ¡ }d	D ]#}|d
  |¡|d
< |d  |¡|d< | ddg¡ ¡ }t	 
||¡ q-d S )Nr   r1   )r`   r   r   )r   r   r³   Údr   r³   r¶   )Zfloat32r*   r   r   )rc   Zhstackrd   r¨   r   r‡   r   rX   r    r   r   )Úvalsr   r    Útrˆ   r!   r!   r"   Útest_count_cross_typeH  s   ÿür¹   c                  C   sŠ   t t g d¢tj¡t g d¢tj¡t g d¢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 )N)r   r   r   r`   )r   r   r   é   )rQ   r1   rº   é   r   )r   r   r³   Úgrpr¼   r   )r   r   r³   ÚabrŠ   r4   )r   rc   ÚarrayZint8Zuint32Zint16r>   r   rX   r   r   r   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                       s,   e Zd Zdd	‡ fdd„Z‡fdd„Z‡  ZS )
z8test_count_uses_size_on_exception.<locals>.RaisingObjectúI will raise inside CythonÚreturnNc                    s   t ƒ  ¡  || _d S rW   )ÚsuperÚ__init__Úmsg)r   rÅ   ©Ú	__class__r!   r"   rÄ   n  s   

zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__c                    s
   ˆ | j ƒ‚rW   )rÅ   )r   Úother©rÀ   r!   r"   Ú__eq__r  s   
z?test_count_uses_size_on_exception.<locals>.RaisingObject.__eq__)rÁ   )rÂ   N)r˜   r™   rš   rÄ   rÊ   Ú__classcell__r!   rÉ   rÆ   r"   ÚRaisingObjectm  s    rÌ   c                    s   g | ]}ˆ ƒ ‘qS r!   r!   )rS   ri   )rÌ   r!   r"   rV   v  s    z5test_count_uses_size_on_exception.<locals>.<listcomp>rQ   r   )r   r¼   r¼   r   r   r½   rŠ   r4   )	Ú	Exceptionr   rA   r>   r   rX   r   r   r   r´   r!   )rÌ   rÀ   r"   Ú!test_count_uses_size_on_exceptioni  s   $	 rÎ   )Ú	itertoolsr   Ústringr   Únumpyrc   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	