o
    ҷh4                     @   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!   U/var/www/html/venv/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   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 rH   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   ngroupsr?   )r   r   r   rL   
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   g_ngroup
g_cumcountexpected_ngroup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)rZ   ir[   )pr!   r"   r]         " )r   rB   r   r   sortedset	enumerater   r   r?   r   r   )r   r   r   ngroupd
cumcountedr!   )r\   ra   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   abcdefd   )sortgroup_idgroup_index)r   nprandomdefault_rngchoicer>   r   re   locr3   r   r   r   valuesr?   r   )	r   rk   r   r   r`   _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
   rZ   r`   r!   r!   r"   r]          zTestCounting.<listcomp>r   rX   c                 C   r{   )r|   r}   	 20:09:25r~   r   r!   r!   r"   r]      r   c                 C   s"   g | ]}t d |ddddqS )r|   r}   r   UTC)tzr~   r   r!   r!   r"   r]      rb   c                 C   s   g | ]}t |d dqS )h)unit)r	   rZ   rP   r!   r!   r"   r]      s    c                 C   s   g | ]	}t d d|dqS )2Wi  )freqyearmonth)r   r   r!   r!   r"   r]      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   )rP   rQ   rP   rQ   r   r   r   r   r4   )r   r   r_   r3   namer   assert_frame_equal)r   rz   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   )levelscodesnamesr   )r3   r(   r   F)check_index_type)r   ro   nanr   r   r_   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   rX   r   r   )r   r   r   )g        g      @g      @g      @r   r   )r   r   r   r   )r3   data)r   ro   r   r   r_   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/20125min
   )startr   periodsr4   c                 S   s   | d d dkS )Nr   r   r   r!   rP   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   ro   aranger   r_   r   r   )r   dr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@   rC   rD   rE   rG   rI   rJ   rK   rO   rW   rh   ry   pytestmarkparametrizerB   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ababrX   ztimedelta64[ns])r   deltar   r   r   r   r   r   r3   r   )r   r>   ro   r   astyper   r   r   r   r_   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 ]}|| }||tjj|dd}t|| qd S )Ni   z
2015-08-30r   T)r   r   r   r   r1   r   ir   )	1st2nd3rd4th5th6th7th8th9th)r   r   r   r   categoryr   r   )axis)r   r   ro   rp   rq   rr   r>   r   integersstandard_normalroundr   dropr   rs   r   r   r_   applyr   r   )nr   r   colkeyleftrightr!   r!   r"   
test_count
  s0   &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   foor   barr   r   r   r   F)as_indexr   r   r   r   r2   )r   ro   r   r   r_   r3   r   r   r   reset_indexr   )r   count_ascount_not_asr    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   r_   r   r   r   r   ro   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   )rj   r   float64)r   r   r   dr   r   r   )float32r*   r   r   )ro   hstackrp   rq   r   r   r   r   r   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   rj   )r   r   r      )rX   r1   r      r   )r   r   r   grpr   r   )r   r   r   abr   r4   )r   ro   arrayint8uint32int16r>   r   r_   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"   RaisingObjectExceptionn  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 r^   )super__init__msg)r   r   	__class__r!   r"   r   r  s   

zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__c                    s
    | j r^   )r   )r   otherr   r!   r"   __eq__v  s   
z?test_count_uses_size_on_exception.<locals>.RaisingObject.__eq__)r   )r   N)r   r   r   r   r   __classcell__r!   r   r   r"   RaisingObjectq  s    r   c                    s   g | ]}  qS r!   r!   )rZ   ru   )r   r!   r"   r]   z  s    z5test_count_uses_size_on_exception.<locals>.<listcomp>rX   r   )r   r   r   r   r   r   r   r4   )		Exceptionr   rB   r>   r   r_   r   r   r   r   r!   )r   r   r"   !test_count_uses_size_on_exceptionm  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 )Npyarrow)r   r   r   )r   r   r   r'   )r   r   r   r   r   r   r4   )	r   importorskipr   r   r   r_   r   r   r   )any_string_dtyper   r   r    r!   r!   r"   test_count_arrow_string_array  s   
r   )	itertoolsr   stringr   numpyro   r   pandasr   r   r   r   r   r	   r
   r   pandas._testing_testingr   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s     (
 n	