
    bMh25                         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          Zd Zd Zd	 Zd
 Zd Zd Zd Zd ZdS )    )product)ascii_lowercaseN)	DataFrameIndex
MultiIndexPeriodSeries	Timedelta	Timestamp
date_rangec                      e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zej                            dd  edd          D             d  edd          D             d  edd          D             d  edd          D             d  edd          D             g          d             Zd Zd Zd ZdS )TestCountingc                 4   t          dgdgdgdgdggdg          }|                    d          }|j        }t          g d          }t	          j        ||                                           t	          j        ||                                           d S )NabAcolumnsr         r      r   groupbyr   r	   tmassert_series_equalcumcountselfdfgsgexpecteds        b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pandas/tests/groupby/test_counting.pytest_cumcountzTestCounting.test_cumcount   s    usecUSE:SEJJJJJsOOS///**
x666
x77777    c                 X   t                                          d          }t          t                                        d          }t          d          }t	          j        ||                                           t	          j        ||                                           d S Nr   )leveldtypeint64)r   r   r	   objectr   r   r   r   gesees       r$   test_cumcount_emptyz TestCounting.test_cumcount_empty   s    [[  q ))&!!!)))22 !!!
q"++--000
q"++--00000r&   c                 J   t          dgdgdgdgdggdgdgdz            }|                    d          }|j        }t          g ddgdz            }t	          j        ||                                           t	          j        ||                                           d S )	Nr   r   r   r      r   indexr   r6   r   r   s        r$   test_cumcount_dupe_indexz%TestCounting.test_cumcount_dupe_index)   s    USEC53%#/#qcAg
 
 
 JJsOOS///!q999
x666
x77777r&   c                    t          j        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          }|j        }t          g d|	          }t          j        ||                                           t          j        ||                                           d S )
Nr   r   r   r   r   r   r5   r   r7   )	r   from_tuplesr   r   r   r	   r   r   r   r   mir    r!   r"   r#   s         r$   test_cumcount_mizTestCounting.test_cumcount_mi5   s    #aVaVaVaVaV$LMMusecUSE:SEQSTTTJJsOOS///444
x666
x77777r&   c                 N   t          dgdgdgdgdggdgdgdz            }|                    g d          }|j        }t          g ddgdz  	          }t	          j        ||                                           t	          j        ||                                           d S )
Nr   r   r   r   r4   r5   r   r   r   r   r   r   r7   r   r   s        r$   test_cumcount_groupby_not_colz*TestCounting.test_cumcount_groupby_not_col@   s    USEC53%#/#qcAg
 
 
 JJ''S///!q999
x666
x77777r&   c                 8   t          dt          d          i          }|                    d          }|j        }t	          g d          }t          j        ||                                           t          j        ||                                           d S )Nr   aaabar?   r   listr   r   r	   r   r   ngroupr   s        r$   test_ngroupzTestCounting.test_ngroupL   s}    T']]+,,JJsOOS///**
x444
x55555r&   c                 R   t          dt          d          i          }|                    d          }|j        }t	          t          d          d          }t          j        ||                                           t          j        ||                                           d S )Nr   abcder4   r,   r*   )	r   rD   r   r   r	   ranger   r   rE   r   s        r$   test_ngroup_distinctz!TestCounting.test_ngroup_distinctV   s    T']]+,,JJsOOS%(('222
x444
x55555r&   c                 *   t          ddgdz  i          }|                    d          }|j        }t          dgdz            }t	          j        ||                                           t	          j        ||                                           d S )Nr   r   r4   )r   r   r   r	   r   r   rE   r   s        r$   test_ngroup_one_groupz"TestCounting.test_ngroup_one_group`   s|    aS1W~&&JJsOOS1#'??
x444
x55555r&   c                 X   t                                          d          }t          t                                        d          }t          d          }t	          j        ||                                           t	          j        ||                                           d S r(   )r   r   r	   r-   r   r   rE   r.   s       r$   test_ngroup_emptyzTestCounting.test_ngroup_emptyj   s    [[  q ))&!!!)))22 !!!
q"))++...
q"))++.....r&   c                 8   t          dt          d          i          }t          t          d                    }t          j        |                    |                                          |                    |                                                     d S )Nr   rB   )r   rD   r	   r   r   r   rE   )r   r    ss      r$    test_ngroup_series_matches_framez-TestCounting.test_ngroup_series_matches_framet   sr    T']]+,,4==!!
rzz!}}3355qyy||7J7J7L7LMMMMMr&   c                 P   t          dt          d          idgdz            }|                    d          }|j        }t	          g ddgdz            }t          j        ||                                           t          j        ||                                           d S Nr   rB   r   r4   r7   r?   rC   r   s        r$   test_ngroup_dupe_indexz#TestCounting.test_ngroup_dupe_indexz   s    T']]+A37;;;JJsOOS///!q999
x444
x55555r&   c                    t          j        ddgddgddgddgddgg          }t          dt          d          i|          }|                    d          }|j        }t          g d|          }t          j        ||	                                           t          j        ||	                                           d S )Nr   r   r   r   rB   r7   r?   )
r   r:   r   rD   r   r   r	   r   r   rE   r;   s         r$   test_ngroup_mizTestCounting.test_ngroup_mi   s    #aVaVaVaVaV$LMMT']]+2666JJsOOS///444
x444
x55555r&   c                 T   t          dt          d          idgdz            }|                    g d          }|j        }t	          g ddgdz            }t          j        ||                                           t          j        ||                                           d S rS   rC   r   s        r$   test_ngroup_groupby_not_colz(TestCounting.test_ngroup_groupby_not_col   s    T']]+A37;;;JJ''S///!q999
x444
x55555r&   c                    t          g ddg          }|                    dg          }t          g d          }t          g d          }t          j        ||j        dz
  |z
             t          j        ||                    d                     t          j        ||                    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   ngroupsrE   )r   r    r!   rZ   
descendings        r$   test_ngroup_descendingz#TestCounting.test_ngroup_descending   s    0003%@@@JJu???++	OOO,,

zAIMY+FGGG
y!((T(*B*BCCC
z188e8+D+DEEEEEr&   c                 `   t          ddgddgddgddgddggddg          }|                    ddg          }|                                }|                                }t	          g d          }t	          g d	          }t          j        ||           t          j        ||           d S )
Nr   xyr   r   Xr   r   r?   )r   r   rE   r   r	   r   r   )r   r    r!   g_ngroup
g_cumcountexpected_ngroupexpected_cumcounts          r$   test_ngroup_matches_cumcountz)TestCounting.test_ngroup_matches_cumcount   s    3Z#sc3Z#sc3ZH#J
 
 
 JJSz""88::ZZ\\
 11"???33
x999
z+<=====r&   c                    t          t          d          d          D ]֊t          di          }|                    dg          }t	          t                              fdD             }fdt                    D             }t          j        |	                                t          |                     t          j        |                                t          |                     d S )Nr      )repeatr   c                 :    g | ]}                     |          S  r7   ).0valorders     r$   
<listcomp>z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>   s%    555Cu{{3''555r&   c                 P    g | ]"\  }}d |                              |          #S N)count)rl   irm   ps      r$   ro   z:TestCounting.test_ngroup_cumcount_pair.<locals>.<listcomp>   s1    FFFvq#!BQB%++c**FFFr&   )r   rI   r   r   sortedset	enumerater   r   rE   r	   r   )r   r    r!   ngroupd
cumcountedrn   rt   s        @@r$   test_ngroup_cumcount_pairz&TestCounting.test_ngroup_cumcount_pair   s    q!,,, 		E 		EAC8$$B

C5!!A3q66NNE55551555GFFFF1FFFJ"188::vg???"1::<<
1C1CDDDD		E 		Er&   c                    t          dt          j                            d                              t          d          d          i          }|                    d|          }d|d<   d|d<   t          |          D ]?\  }\  }}||j        |j	        df<   t          |j	                  D ]\  }}||j        |df<   @t          j        t          |d         j                  |                                           t          j        t          |d         j                  |                                           d S )	Nr   r   abcdefd   )sortgroup_idgroup_index)r   nprandomdefault_rngchoicerD   r   rw   locr6   r   r   r	   valuesrE   r   )	r   r~   r    r!   rs   _groupjinds	            r$   "test_ngroup_respects_groupby_orderz/TestCounting.test_ngroup_respects_groupby_order   s%   RY22155<<T(^^SQQRSSJJsJ&&:=&q\\ 	/ 	/MAz5./BF5;
*+#EK00 / /3-.sM)**/ 	vbn&;<<ahhjjIII
vb&7&>??NNNNNr&   datetimelikec                 6    g | ]}t          d |dd          S )2016-05-02dz 20:09:25+00:00r   rl   rs   s     r$   ro   zTestCounting.<listcomp>   s/    OOOaY8!888899OOOr&   r   rh   c                 6    g | ]}t          d |dd          S )r   r   	 20:09:25r   r   s     r$   ro   zTestCounting.<listcomp>   s/    IIIY2!222233IIIr&   c                 :    g | ]}t          d |ddd          S )r   r   r   UTC)tzr   r   s     r$   ro   zTestCounting.<listcomp>   s4    SSS!Y2!2222u===SSSr&   c                 0    g | ]}t          |d           S )h)unit)r
   rl   r_   s     r$   ro   zTestCounting.<listcomp>   s%    999Yqs###999r&   c                 2    g | ]}t          d d|          S )2Wi  )freqyearmonth)r   r   s     r$   ro   zTestCounting.<listcomp>   s'    HHHqVD222HHHr&   c                     t          g d|d          }|                    d                                          }t          dddgiddg	          }d|j        _        t          j        ||           d S )
N)r   r   r   )r_   r`   r_   r`   r   r   r   r   r7   )r   r   rr   r6   namer   assert_frame_equal)r   r   r    resr#   s        r$   test_count_with_datetimelikez)TestCounting.test_count_with_datetimelike   sy     ___<@@AAjjoo##%%cAq6]3*===!
h,,,,,r&   c                 X   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          }t          g |t          j        d	
          }t          j
        ||d           d S )Nr   r   r   r   r   BCr   r   )levelscodesnamesr   )r6   r+   r   F)check_index_type)r   r   nanr   r   rr   r   r	   r,   r   r   )r   r    resultr<   r#   s        r$   (test_count_with_only_nans_in_first_groupz5TestCounting.test_count_with_only_nans_in_first_group   s    bfbf-S#JaVLLMMS#J'')//11S#J/BxSzRRR"BbhSAAA
vx%HHHHHHr&   c                    t          g dddt          j        ddgd          }|                    dg                                          }t          t          g dd	          d
g di          }t          j        ||           d S )N)r   r   r   r   r   r4   rh   r   r   )r   r   r   )g        g      @g      @g      @r   r   )r   r   r   r   )r6   data)r   r   r   r   rr   r   r   r   )r   r    r   r#   s       r$   4test_count_groupby_column_with_nan_in_groupby_columnzATestCounting.test_count_groupby_column_with_nan_in_groupby_column   s    ___Aq"&!Q3GHHIIjj#%%'',,,3777sLLL>Q
 
 
 	h,,,,,r&   c                    t          ddd          }t          t          j        d          |t          j        d          g          }|                    d           }|                                }t          t          j        d          t          j        d          |g          }|                    d           }|                                }t          j        ||           d S )Nz1/1/20125min
   )startr   periodsr7   c                      | d         dz  dk    S )Nr   r   r   rk   r_   s    r$   <lambda>z@TestCounting.test_groupby_count_dateparseerror.<locals>.<lambda>       !qA r&   c                      | d         dz  dk    S )Nr   r   rk   r   s    r$   r   z@TestCounting.test_groupby_count_dateparseerror.<locals>.<lambda>   r   r&   )r   r	   r   aranger   rr   r   r   )r   drsergroupedr   r#   s         r$   !test_groupby_count_dateparseerrorz.TestCounting.test_groupby_count_dateparseerror   s    jvrBBB RYr]]2ry}}*=>>>++5566RYr]]29R=="*=>>>++5566==??
vx00000r&   N)__name__
__module____qualname__r%   r2   r8   r=   r@   rF   rJ   rL   rN   rQ   rT   rV   rX   r]   rf   rz   r   pytestmarkparametrizerI   r   r   r   r   rk   r&   r$   r   r      s       8 8 81 1 1
8 
8 
8	8 	8 	8
8 
8 
86 6 66 6 66 6 6/ / /N N N6 6 66 6 66 6 6	F 	F 	F> > >E E EO O O [OO55A;;OOOIIUU1a[[IIISSuuQPQ{{SSS99UU1a[[999HHEE!QKKHHH	
	 	- -	 	-I I I- - -1 1 1 1 1r&   r   c                  Z   t          t          d          t          j        d                              d          d          } t          ddgt          ddgd	          d
          }|                     d          j        	                                }t          j        ||           d S )Nababrh   ztimedelta64[ns])r!   deltar   r   r   r!   r   r   r6   r   )r   rD   r   r   astyper	   r   r   r   rr   r   r   )r    r#   r   s      r$   #test_groupby_timedelta_cython_countr     s    	8nnry||':':;L'M'MNN
 
B q!fE3*3$?$?$?gNNNHZZ__"((**F8V,,,,,r&   c                     d} t          d| dz  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          z
  t          j                            d                              t          t                    |           d	          }|j                            g d          D ]L}t          j        |j        t          j                            d                              | | dz            |f<   M|d                             d          |d<   ddddgfD ]}|                    |                                          }d}t%          j        t(          |          5  |                    |                              t          j                                      |d          }d d d            n# 1 swxY w Y   t%          j        ||           d S )Ni   z
2015-08-30r   min)r   r   r   r   r4   r   ir   )	1st2nd3rd4th5th6th7th8th9th)r   r   r   r   categoryr   r   z7DataFrameGroupBy.apply operated on the grouping columns)match)axis)r   r   r   r   r   r   rD   r   integersstandard_normalroundr   dropr   r   r   r   rr   r   assert_produces_warningFutureWarningapplyr   )nr   r    colkeyleftmsgrights           r$   
test_countr   
  sX   A	L!r'	>	>	>B	9((++2243H3H!LL9((++44Q1==9((++;;A>>DDQGG9((++44S"a@@9((++222q999((++;;A>>DDQGG9((++;;A>>DDQGG9((++222q99i##A&&--b!4459((++2243H3H!LL	
 	

 
B z44455 J JCE6ry$$Q''..q!r'::C?@@5	  ,,BuIuuen- + +zz#$$&&G'SAAA 	M 	MJJsOO)))/::??!?LLE	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M
dE****+ +s   AN""N&	)N&	c                  v   t          g ddt          j        dgdt          j        t          j        ggg d          } |                     d                                          }|                     dd	                                          }t          dd
gddggddgddg          }d|j        _        t          j        ||	                                           t          j        ||           |                     d          d                                         }t          j
        ||d                    d S )N)r   r   foor   barr   r   r   r   F)as_indexr   r   r   r   r5   )r   r   r   r   rr   r6   r   r   r   reset_indexr   )r    count_ascount_not_asr#   count_Bs        r$   test_count_non_nullsr   *  s!    
	BFE*Q,?@
 
 
B
 zz#$$&&H::cE:2288::L1a&1a&)C:aVLLLHHN,(<(<(>(>???(H---jjooc"((**G7HSM22222r&   c                  d   t          dgdz  dgdz  z   dgdz  dgdz  z   d          } |                     d          j                                        }t	          ddgt          ddgd          d          }t          j        ||           t          dt          j	        t          j	        gdgdz  z   dgdz  dgdz  z   d          } |                     d          j                                        }t	          d	dgt          ddgd          d          }t          j        ||           d S )
Nr   r   r   r   )r   cr   r   r   r   )
r   r   r   rr   r	   r   r   r   r   r   r    r   r#   s      r$   test_count_objectr   >  s0   	#cUQY.aS1WsQw5FGG	H	HBZZ__$$&&Fq!fE1a&s$;$;$;#FFFH68,,,	#rvrv.#:!qA3QR7ARSS	T	TBZZ__$$&&Fq!fE1a&s$;$;$;#FFFH68,,,,,r&   c                     t          j        t           j                            d                              ddd          t           j                            d                              ddd          f                              d          } t          | g d          }t           j        ||dk    <   |                    dd	g          	                                }d
D ]|}|d                             |          |d<   |d                             |          |d<   |                    dd	g          	                                }t          j        ||           }d S )Nr   r   r4   )r}   r   float64)r   r   r   dr   r   r   )float32r-   r   r   )r   hstackr   r   r   r   r   r   r   rr   r   r   )valsr    r#   tr   s        r$   test_count_cross_typer   J  sC    9I!!!$$--aH==I!!!$$--aH==	
 
 fY 	 
4!5!5!5	6	6	6B&BrQwKzz3*%%++--H" 0 0S'..##3S'..##3S#J''--//
fh////	0 0r&   c                     t          t          j        g dt          j                  t          j        g dt          j                  t          j        g dt          j                  t          d          d          } |                     d                                          }t          ddgddgddgdt          t          d	          d
                    }t          j        ||           d S )N)r   r   r   r}   )r   r   r      )rh   r4   r     r   )r   r   r   grpr  r   )r   r   r   abr   r7   )r   r   arrayint8uint32int16rD   r   rr   r   r   r   r   s      r$   test_lower_int_prec_countr
  _  s    	..."'22,,,	22,,,11>>		
 	

 
B ZZ$$&&F!fAq6A//uT$ZZe7T7T7T  H &(+++++r&   c                      G d dt                      G fdd          t          fdt          d          D             t          d          d          } |                     d	                                          }t          d
ddgit          t          d          d	                    }t          j        ||           d S )Nc                       e Zd ZdS )Atest_count_uses_size_on_exception.<locals>.RaisingObjectExceptionN)r   r   r   rk   r&   r$   RaisingObjectExceptionr  p  s        r&   r  c                   .     e Zd Zdd fdZfdZ xZS )8test_count_uses_size_on_exception.<locals>.RaisingObjectI will raise inside CythonreturnNc                 V    t                                                       || _        d S rq   )super__init__r   )r   r   	__class__s     r$   r  zAtest_count_uses_size_on_exception.<locals>.RaisingObject.__init__t  s$    GGDHHHr&   c                 $     | j                   rq   )r   )r   otherr  s     r$   __eq__z?test_count_uses_size_on_exception.<locals>.RaisingObject.__eq__x  s    ((222r&   )r  )r  N)r   r   r   r  r  __classcell__)r  r  s   @r$   RaisingObjectr  s  s\        	 	 	 	 	 	 		3 	3 	3 	3 	3 	3 	3 	3 	3r&   r  c                 $    g | ]}             S rk   rk   )rl   r   r  s     r$   ro   z5test_count_uses_size_on_exception.<locals>.<listcomp>|  s    <<<a--//<<<r&   rh   r   )r   r  r  r   r   r  r   r7   )		Exceptionr   rI   rD   r   rr   r   r   r   )r    r   r#   r  r  s      @@r$   !test_count_uses_size_on_exceptionr  o  s           3 3 3 3 3 3 3 3 3 3 
<<<<588<<<T(^^TT	U	UBZZ$$&&F#1veDJJU.K.K.KLLLH&(+++++r&   c                 8   t          j        d           t          g dt          g d|           d          }|                    d                                          }t          ddit          g dd	          
          }t          j        ||           d S )Npyarrow)r   r   r   )r   r   r   r*   )r   r   r   r   r   r   r7   )	r   importorskipr   r	   r   rr   r   r   r   )any_string_dtyper    r   r#   s       r$   test_count_arrow_string_arrayr#    s    
	"""	iif___<LMMMNN
 
B ZZ__""$$F#qyyys)C)C)CDDDH&(+++++r&   )	itertoolsr   stringr   numpyr   r   pandasr   r   r   r   r	   r
   r   r   pandas._testing_testingr   r   r   r   r   r   r   r
  r  r#  rk   r&   r$   <module>r*     sj         " " " " " "     	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	      j1 j1 j1 j1 j1 j1 j1 j1Z- - -+ + +@3 3 3(	- 	- 	-0 0 0*, , , , , ,&, , , , ,r&   