o
    EÜh25  ã                   @   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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!   úBlib/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   ÚgeZ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   ©r2   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   r1   r   r3   )	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   r0   r1   ©r   r   r   r   r   r   r3   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   Úaaabar9   ©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abcder0   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   r0   )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   r/   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   r0   r3   r9   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;   r3   r9   )
r   r5   r   r=   r   r   r   r   r   r>   r6   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 rF   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   rJ   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   r9   )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!   r3   )Ú.0Úval)Úorderr!   r"   Ú
<listcomp>¹   s    z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>c                    s"   g | ]\}}ˆ d |…   |¡‘qS ©N)Úcount)rR   ÚirS   )Úpr!   r"   rU   º   ó   " )r   r@   r   r   ÚsortedÚsetÚ	enumerater   r   r>   r   r   )r   r   r   ZngroupdZ
cumcountedr!   )rT   rY   r"   Útest_ngroup_cumcount_pair²   s   ÷z&TestCounting.test_ngroup_cumcount_pairc           	      C   s¾   t dtj d¡ tdƒd¡iƒ}|jd|d}d|d< d|d< t|ƒD ]\}\}}||j|j	df< t|j	ƒD ]\}}||j|df< q6q#t
 t|d jƒ| ¡ ¡ t
 t|d jƒ| ¡ ¡ d S )	Nr   r   Zabcdeféd   )ÚsortéÿÿÿÿZgroup_idZgroup_index)r   ÚnpÚrandomÚdefault_rngÚchoicer=   r   r]   Úlocr2   r   r   r   Úvaluesr>   r   )	r   r`   r   r   rX   Ú_ÚgroupÚjZ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
   ©rR   rX   r!   r!   r"   rU   Ð   ó    zTestCounting.<listcomp>r   rP   c                 C   rm   )rn   ro   ú	 20:09:25rp   rq   r!   r!   r"   rU   Ñ   rr   c                 C   s"   g | ]}t d |d›ddd‘qS )rn   ro   rs   ZUTC)Ztzrp   rq   r!   r!   r"   rU   Ò   rZ   c                 C   s   g | ]}t |d d‘qS )Úh)Zunit)r	   ©rR   rL   r!   r!   r"   rU   Ó   s    c                 C   s   g | ]	}t d d|d‘qS )Z2Wiá  )ÚfreqZyearZmonth)r   ru   r!   r!   r"   rU   Ô   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   )rL   rM   rL   rM   r   r   r   r   r3   )r   r   rW   r2   Únamer   Úassert_frame_equal)r   rl   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levelsZcodesÚnamesr}   )r2   r(   rw   F)Zcheck_index_type)r   rb   Únanr   r}   rW   r   r   r)   r   r   )r   r   Úresultr7   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   r0   rP   r   r   )r   r|   r|   )g        g      @g      @g      @©rw   r   )r   r   r   r   )r2   Údata)r   rb   r   r   rW   r   r   rx   )r   r   ry   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é
   )Ústartrv   Úperiodsr3   c                 S   s   | d d dkS )Nr   r   r   r!   ©rL   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   rb   Úaranger   rW   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.   r4   r8   r:   r?   rA   rB   rC   rE   rG   rH   rI   rK   rO   r^   rk   ÚpytestZmarkZparametrizer@   rz   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ÚababrP   ztimedelta64[ns])r   Údeltar   r   r   r   r‚   r“   ©r2   rw   )r   r=   rb   r‹   Úastyper   r   r   r“   rW   r   r   )r   r    r€   r!   r!   r"   Ú#test_groupby_timedelta_cython_count  s   ÿr–   c                  C   sº  d} t d| d dd}ttj d¡ ttƒ| ¡tj d¡ dd| ¡tj d¡ 	| ¡ 
d	¡tj d¡ d
d| ¡tj d¡ || ¡tj d¡ 	| ¡ 
d	¡tj d¡ 	| ¡ 
d	¡tj d¡ || ¡tj d¡ |d¡ tj d¡ ttƒ| ¡dœ	ƒ}|j g d¢¡D ]}tj|jtj d¡ | | d ¡|f< q|d  d¡|d< ddddgfD ]6}| |¡ ¡ }d}tjt|d | |¡ tj¡j|dd}W d   ƒ n1 sÏw   Y  t ||¡ q¤d S )Ni €  z
2015-08-30r…   Úmin)r‡   rv   r   r   r0   r   iöÿÿÿr   )	Ú1stÚ2ndZ3rdÚ4thZ5thZ6thZ7thZ8thÚ9th)r˜   r™   rš   r›   Úcategoryr˜   r™   z7DataFrameGroupBy.apply operated on the grouping columns)Úmatch)Zaxis)r   r   rb   rc   rd   re   r=   r   ÚintegersZstandard_normalÚroundr   Zdropr   rf   r•   r   rW   r   Zassert_produces_warningÚFutureWarningZapplyrx   )ÚnrŒ   r   ÚcolÚkeyÚleftÚmsgÚrightr!   r!   r"   Ú
test_count
  s6   ÿöÿ&ÿû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   Zbarr   r{   r   r   F)Zas_indexr   r   r|   r}   r1   )r   rb   r   r   rW   r2   rw   r   rx   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   rW   r   r   r   r   rb   r   ©r   r€   r    r!   r!   r"   Útest_count_object>  s   .2r«   c                  C   s¸   t  t j d¡ ddd¡t j d¡ ddd¡f¡ d¡} t| g d¢d}t j||dk< | dd	g¡ 	¡ }d
D ]#}|d  |¡|d< |d  |¡|d< | dd	g¡ 	¡ }t
 ||¡ q6d S )Nr   r   r0   )r_   r   Zfloat64)r   r   r©   Údr   r©   r¬   )Zfloat32r*   r   r   )rb   Zhstackrc   rd   rž   r•   r   r   r   rW   r   rx   )Zvalsr   r    Útr€   r!   r!   r"   Útest_count_cross_typeJ  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   é   )rP   r0   r¯   é   r’   )r   r   r©   Úgrpr±   r   )r   r   r©   Úabr‚   r3   )r   rb   ZarrayZint8Zuint32Zint16r=   r   rW   r   r   rx   rª   r!   r!   r"   Útest_lower_int_prec_count_  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"   ÚRaisingObjectExceptionp  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 rV   )ÚsuperÚ__init__r¥   )r   r¥   ©Ú	__class__r!   r"   r¸   t  s   

zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__c                    s
   ˆ | j ƒ‚rV   )r¥   )r   Úother©r´   r!   r"   Ú__eq__x  s   
z?test_count_uses_size_on_exception.<locals>.RaisingObject.__eq__)rµ   )r¶   N)rŽ   r   r   r¸   r½   Ú__classcell__r!   r¼   r¹   r"   ÚRaisingObjects  s    r¿   c                    s   g | ]}ˆ ƒ ‘qS r!   r!   )rR   rh   )r¿   r!   r"   rU   |  s    z5test_count_uses_size_on_exception.<locals>.<listcomp>rP   r’   )r   r±   r±   r   r   r²   r‚   r3   )	Ú	Exceptionr   r@   r=   r   rW   r   r   rx   rª   r!   )r¿   r´   r"   Ú!test_count_uses_size_on_exceptiono  s   $	 rÁ   c                 C   sb   t  d¡ tg d¢tg d¢| ddœƒ}| d¡ ¡ }tdditg d¢dd	d
}t ||¡ d S )NZpyarrow)r   r   r   )r   r   r   r'   )r   r   r   r   r   r‚   r3   )	r‘   Zimportorskipr   r   r   rW   r   r   rx   )Zany_string_dtyper   r€   r    r!   r!   r"   Útest_count_arrow_string_array‚  s   
ÿrÂ   )Ú	itertoolsr   Ústringr   Znumpyrb   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!   r"   Ú<module>   s     (
 n	 