o
    թZh75                     @   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!   Y/var/www/html/lang_env/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 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abcdefd   )sortZgroup_idZgroup_index)r   nprandomdefault_rng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   r   r   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   deltar   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   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 sw   Y  t|| qd S )Ni   z
2015-08-30r   min)r   r{   r   r   r1   r   ir   )	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   rc   rd   re   rf   r>   r   integersZstandard_normalroundr   Zdropr   rg   r   r   rX   r   Zassert_produces_warningDeprecationWarningapplyr   )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   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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   r1   )r`   r   Zfloat64)r   r   r   dr   r   r   )Zfloat32r*   r   r   )rc   Zhstackrd   re   r   r   r   r   r   rX   r   r   )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      )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_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 rW   )super__init__r   )r   r   	__class__r!   r"   r   t  s   

zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__c                    s
    | j rW   )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!   )rS   ri   )r   r!   r"   rV   |  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_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   r4   )	r   Zimportorskipr   r   r   rX   r   r   r   )Zany_string_dtyper   r   r    r!   r!   r"   test_count_arrow_string_array  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!   r"   <module>   s     (
 n	 