a
    GGb!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   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!   Alib/python3.9/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   geZseer!   r!   r"   test_cumcount_empty   s
    
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   r0   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   r/   r   r1   )	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   r.   r/   r   r   r   r   r   r   r1   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   aaabar7   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   sd   t dtdidgd d}|d}|j}tg ddgd d}t||  t||  d S Nr   r9   r   r.   r1   r7   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   r9   r1   r7   )
r   r3   r   r;   r   r   r   r   r   r<   r4   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 rD   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   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g d}tg d	}t|| t|| d S )
Nr   xyr   r   Xr   r   r7   )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!   r1   ).0val)orderr!   r"   
<listcomp>       z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>c                    s"   g | ]\}} d |  |qS N)count)rP   irQ   )pr!   r"   rS      rT   )r   r>   r   r   sortedset	enumerater   r   r<   r   r   )r   r   r   ZngroupdZ
cumcountedr!   )rR   rX   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abcdefd   )FT)sortZgroup_idZgroup_index)nprandomZseedr   choicer;   r   r[   locr0   r   r   r   valuesr<   r   )	r   r   Z	sort_flagr   rW   _groupjZ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   rW   r!   r!   r"   rS      rT   zTestCounting.<listcomp>r   rN   c                 C   s   g | ]}t d |ddqS )rj   rk   	 20:09:25rl   rm   r!   r!   r"   rS      rT   c                 C   s"   g | ]}t d |ddddqS )rj   rk   rn   ZUTC)Ztzrl   rm   r!   r!   r"   rS      rT   c                 C   s   g | ]}t |d dqS )h)Zunit)r	   rP   rJ   r!   r!   r"   rS      rT   c                 C   s   g | ]}t d d|dqS )Z2Wi  )freqZyearZmonth)r   rp   r!   r!   r"   rS      rT   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   )rJ   rK   rJ   rK   r   r   r   r   r1   )r   r   rV   r0   namer   assert_frame_equal)r   ri   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   rw   )ZlevelsZcodesnamesrx   )r0   r'   rr   F)Zcheck_index_type)r   r`   nanr   rx   rV   r   r   r(   r   r   )r   r   resultr5   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   r.   rN   r   r   )r   rw   rw   )g        g      @g      @g      @rr   r   )r   r   r   r   )r0   data)r   r`   ZNaNr   rV   r   r   rs   )r   r   rt   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
   )startrq   periodsr1   c                 S   s   | d d dkS )Nr   r   r   r!   rJ   r!   r!   r"   <lambda>   rT   z@TestCounting.test_groupby_count_dateparseerror.<locals>.<lambda>c                 S   s   | d d dkS )Nr   r   r!   r   r!   r!   r"   r      rT   )r   r   r`   aranger   rV   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-   r2   r6   r8   r=   r?   r@   rA   rC   rE   rF   rG   rI   rM   r\   rh   pytestZmarkZparametrizer>   ru   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   r0   rr   )r   r;   r`   r   astyper   r   r   r   rV   r   r   )r   r    r{   r!   r!   r"   #test_groupby_timedelta_cython_count  s    r   c                  C   sH  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< 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   rq   r   r.   r   ir   )	1st2ndZ3rd4thZ5thZ6thZ7thZ8th9th)r   r   r   r   categoryr   r   )Zaxis)r   r   r`   ra   rb   r;   r   randintZrandnroundr   Zdroprz   rc   r   r   rV   Zapplyr   rs   )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   Zbarr   rv   r   r   F)Zas_indexr   r   rw   rx   r/   )r   r`   rz   r   rV   r0   rr   r   rs   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   rV   r   r   r   r   r`   rz   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 ]F}|d
 ||d
< |d ||d< |ddg }t	
|| qZd S )Nr   r.   )r]   r   r   )r   r   r   dr   r   r   )Zfloat32r)   r   r   )r`   Zhstackra   r   r   rz   r   rV   r   r   rs   )Zvalsr   r    tr{   r!   r!   r"   test_count_cross_typeI  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      )rN   r.   r      r   )r   r   r   grpr   r   )r   r   r   abr}   r1   )r   r`   ZarrayZint8Zuint32Zint16r;   r   rV   r   r   rs   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 rU   )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 rU   )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  rT   z5test_count_uses_size_on_exception.<locals>.<listcomp>rN   r   )r   r   r   r   r   r   r}   r1   )		Exceptionr   r>   r;   r   rV   r   r   rs   r   r!   )r   r   r"   !test_count_uses_size_on_exceptionj  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   (
 p	