
    i                       d 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	 ddl
ZddlmZmZ ddlmc mZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z: erddl;m<Z<m=Z=m>Z>m?Z? ddl@mAZA d#dZBd ZC G d d      ZD G d d      ZE G d deE      ZFd$dZG G d dee         ZH G d d eH      ZI G d! d"eH      ZJy)%a  
Provide classes to perform the groupby aggregate operations.

These are not exposed to the user and provide implementations of the grouping
operations, primarily in cython. These classes (BaseGrouper and BinGrouper)
are contained *in* the SeriesGroupBy and DataFrameGroupBy objects.
    )annotationsN)TYPE_CHECKINGAnyGenericfinal)NaTlib)	ArrayLikeAxisIntNDFrameTShapenptAbstractMethodError)cache_readonly)maybe_downcast_to_dtype)ensure_float64ensure_int64ensure_platform_intensure_uint64is_1d_only_ea_dtype)isna
maybe_fill)Categorical)	DataFrame)grouper)CategoricalIndexIndex
MultiIndexensure_index)Series)compress_group_indexdecons_obs_group_idsget_group_indexget_group_index_sorterget_indexer_dict)Callable	GeneratorHashableIterator)NDFramec                b    t        | t        j                        r|t        k7  rt	        d      y y )NzMust produce aggregated value)
isinstancenpndarrayobject
ValueError)objdtypes     T/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pandas/core/groupby/ops.pycheck_result_arrayr5   Q   s1     #rzz"F? <==  #    c                z    t        | d      r.| j                  } | j                  dk(  rt        |       dk(  r| d   } | S )zb
    Extract the result object, it might be a 0-dim ndarray
    or a len-1 0-dim, or a scalar
    _values   r   )hasattrr8   ndimlen)ress    r4   extract_resultr>   \   s:    
 sIkk88q=SX]a&CJr6   c                  6   e Zd ZU dZ eg d      Zd>dZi d ej                  e	j                  d      d ej                  e	j                  d      ddd	d
d ej                  e	j                  d      d ej                  e	j                  d      ddddddddddd ej                  e	j                  d      d ej                  e	j                  d      ddddddd d!d"d#id$d%d&d'd(d)d*Zd+ed,<   d"d-iZed?d.       Zeej$                  	 	 	 	 	 	 	 	 d@d/              ZdAd0ZdBd1ZdCd2ZdCd3Zed4d4d5d6	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dDd7       Zed5d8	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dDd9       ZedEd:       Zed;d<	 	 	 	 	 	 	 	 	 	 	 dFd=       Zy4)GWrappedCythonOpaB  
    Dispatch logic for functions defined in _libs.groupby

    Parameters
    ----------
    kind: str
        Whether the operation is an aggregate or transform.
    how: str
        Operation name, e.g. "mean".
    has_dropped_na: bool
        True precisely when dropna=True and the grouper contains a null value.
    )anyallrankcountsizeidxminidxmaxc                .    || _         || _        || _        y Nkindhowhas_dropped_na)selfrK   rL   rM   s       r4   __init__zWrappedCythonOp.__init__~   s    	,r6   rA   )val_testrB   sum	group_sumprod
group_prodrF   )namerG   min	group_minmax	group_maxmean
group_meanmediangroup_median_float64var	group_varstdsemskew
group_skewkurt
group_kurtfirst	group_nthlast
group_lastohlc
group_ohlcgroup_cumprodgroup_cumsumgroup_cummingroup_cummax
group_rank)cumprodcumsumcummincummaxrC   )	aggregate	transformzdict[str, dict]_CYTHON_FUNCTIONS   c                (    || j                   d   v ryy)Nru   rv   )rw   )clsrL   s     r4   get_kind_from_howz!WrappedCythonOp.get_kind_from_how   s    #''44r6   c                \   |j                   }| j                  |   |   }t        |      r|}nt        t        |      }|r|S |t        j                  t              k(  rE|dv rt        d| d| d      |dv r|S |dv r	 |S d|j                  vrt        d| d| d      |S t        d|      )	N)r\   rq   z2function is not implemented for this dtype: [how->z,dtype->])r`   ra   rF   rG   rb   rd   r0   zPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/)
rU   rw   callablegetattr
libgroupbyr.   r3   r0   NotImplementedError__signatures__)rz   rK   rL   r3   
is_numeric	dtype_strftypefs           r4   _get_cython_functionz$WrappedCythonOp._get_cython_function   s    
 JJ	%%d+C0 E?A
E*AHbhhv&&++) E)A7  ::(( H !1!11) E)A7  H%0 r6   c                2   | j                   }|dv rt        |      }|S |j                  j                  dv ra|dv s| j                  dk(  r| j                  rt        |      }|S |dv r1|j                  j                  dk(  rt        |      }|S t        |      }|S )z
        Cast numeric dtypes to float64 for functions that only support that.

        Parameters
        ----------
        values : np.ndarray

        Returns
        -------
        values : np.ndarray
        )r\   r`   ra   rb   rd   iu)r^   rZ   rv   )rQ   rj   rS   rr   rq   i)rL   r   r3   rK   rM   r   r   )rN   valuesrL   s      r4   _get_cython_valsz WrappedCythonOp._get_cython_vals   s     hh:: $F+F"  \\$&o%		[(T-@-@ (/  DD<<$$+)&1F  +62Fr6   c                    | j                   }| j                  }| j                  j                  |d      }|dk(  r||f}|S |dkD  rt	        d      |dk(  r|j
                  }|S |g|j
                  dd  }|S )Nr9   rj   z<arity of more than 1 is not supported for the 'how' argumentrv   )rL   rK   _cython_aritygetr   shape)rN   ngroupsr   rL   rK   arity	out_shapes          r4   _get_output_shapez!WrappedCythonOp._get_output_shape   s    hhyy""&&sA. &= %(I  QY%N  [ I  !46<<#34Ir6   c                    | j                   }|dk(  rd}n2|dv rd}n+|j                  dv r|j                   |j                   }nd}t        j                  |      S )NrC   float64rF   rG   intpiufcbr0   )rL   rK   itemsizer.   r3   )rN   r3   rL   	out_dtypes       r4   _get_out_dtypezWrappedCythonOp._get_out_dtype  s^    hh&=!I(( IZZ7" ::,u~~&67I Ixx	""r6   c                2   | j                   }|dv rA|t        j                  t              k(  r#t        j                  t        j                        S |S |dv rA|j
                  dv r|S |j
                  dv r#t        j                  t        j                        S |S )a  
        Get the desired dtype of a result based on the
        input dtype and how it was computed.

        Parameters
        ----------
        dtype : np.dtype

        Returns
        -------
        np.dtype
            The desired dtype of the result.
        )rQ   rr   rQ   rS   rq   )rZ   r\   r^   r`   ra   fciub)rL   r.   r3   boolint64rK   r   )rN   r3   rL   s      r4   _get_result_dtypez!WrappedCythonOp._get_result_dtype  s     hh==&xx))  ;;zzT!u$xx

++r6   Nr   )maskresult_maskinitialc          
        |j                   dk(  r\|d d d f   }	|	|d d d f   }|	|d d d f   } | j                  |	f||||||d|}
|
j                  d   dk(  r|
d   S |
j                  S  | j                  |f||||||d|S )Nr9   )	min_countr   comp_idsr   r   r   r   )r;   _call_cython_opr   T)rN   r   r   r   r   r   r   r   kwargsvalues2dr=   s              r4   _cython_op_ndim_compatz&WrappedCythonOp._cython_op_ndim_compat7  s     ;;!dAgHD!G}&)$'2&$&&	#!'	 	C yy|q 1v 55L#t##	
#	
 	
 		
r6   )r   c               *   |}	|j                   }
|
j                  dv }|
j                  dv }| j                  dv r|t        |      }|r|j	                  d      }d}n |
j                  dk(  r|j	                  d      }|j                   dk(  r|j                  t        j                        }| j                  dv rk|
t        k(  r,|d	   r'|%|j                         r|j                         }d||<   |j                  t        d
      j	                  t        j                        }d}|j                  }||j                  }||j                  }| j                  ||      }| j                  | j                  | j                  |j                   |      }| j!                  |      }| j#                  |j                         }t%        t        j&                  ||            }| j                  dk(  rt        j(                  |t        j*                        }| j                  dv r&| j                  dk(  r||d<    |d#||||||||d| n| j                  dv r$| j                  dv r||d<    |||||f|||d| n| j                  dv r& |d#|||||d| |j                  t        d
      }nw| j                  dv r. |d#||||||d| |
t        k(  rQ|j                  t              }n;t-        | j                   d      | j                  dk7  r||d<    |d#||||||d| | j                  dk(  r| j                  dvr|j                   j                  dv rn|slt/        | j                  dv rd nd!|      }|k  }|j                         r;|||   j1                         s&J |j                  d"      }t        j2                  ||<   |j                  }| j                  | j4                  vr)| j7                  |	j                         }t9        ||      }|S |}|S )$Nr   mM)rA   rB   r   Tbuint8float16skipnaFcopyr3   ru   )	rF   rG   rV   rX   rZ   rh   rf   rQ   r\   rQ   r   )outcountsr   labelsr   r   r   is_datetimelike)ra   r`   r^   rj   rS   )r`   ra   r   )r   r   r   )r   r   r   r   r   r~   )r   r   r   r   r   r   z is not implementedrC   r   )r   r   r   r   r   r   r   r   )rQ   rS   r   r9   r    )r3   rK   rL   r   viewastyper.   float32r0   rA   r   r   int8r   r   r   r   r   r   emptyzerosr   r   rX   rB   nancast_blocklistr   r   )rN   r   r   r   r   r   r   r   r   orig_valuesr3   r   r   r   funcr   resultr   cutoffempty_groups	res_dtype	op_results                         r4   r   zWrappedCythonOp._call_cython_opf  s7    ZZ7*
**,88~%|F|[[)FJZZ3[[)F<<9$]]2::.F88~%(#'DHHJ!''+t]]4e]499"''BFJ66D&)mm**7F;	((DHHfllJW&&v.''5	BHHYi@A99#XXgRXX6Fxx 
 
 88u$ )0F9% 
!!#' +$3
 
 BB88~-0?F,-		
 ( +	 	 ^+ !# +   t%8-- !!# +  F?#]]62F *TXXJ6I*JKK xx6!(3}%  /  99#8L(L ||  D($((o"=Q1iP%##%".*<8<<>>> "(y!9/1vv|,884...
 ..{/@/@AI/	BI  Ir6   c                    |j                   dkD  rt        d      |j                   dk(  r|dk(  sJ |       y t        |j                        s|dk(  sJ y y )N   z.number of dimensions is currently limited to 2r9   r   )r;   r   r   r3   )rN   axisr   s      r4   _validate_axiszWrappedCythonOp._validate_axis  sU    ;;?%&VWW;;!19"d"9$V\\2 199 3r6   )r   c          	         | j                  ||       t        |t        j                        s, |j                  d| j
                  | j                  |||d|S  | j                  |f|||dd|S )zW
        Call our cython function, with appropriate pre- and post- processing.
        )rL   rM   r   r   idsN)r   r   r   r   r   )r   r-   r.   r/   _groupby_oprL   rM   r   )rN   r   r   r   r   r   r   s          r4   cython_operationz WrappedCythonOp.cython_operation  s     	D&)&"**-%6%% HH#22#   +t**

 
 	
r6   )rK   strrL   r   rM   r   returnNone)rL   r   r   r   )rK   r   rL   r   r3   np.dtyper   r   )r   
np.ndarrayr   r   )r   intr   r   r   r   )r3   r   r   r   )r   r   r   r   r   r   r   r   r   npt.NDArray[np.bool_] | Noner   r   r   r   r   r   )r   r   r   r
   r   r   )r   r
   r   r   r   r   r   r   r   r   r   r
   )__name__
__module____qualname____doc__	frozensetr   rO   	functoolspartialr   group_any_allgroup_idxmin_idxmaxr_   rw   __annotations__r   classmethodr{   cacher   r   r   r   r   r   r   r   r   r   r   r6   r4   r@   r@   j   sZ    CN-
$9$$Z%=%=N
$9$$Z%=%=N
 ;
 L	

 'i''
(F(FXV
 'i''
(F(FXV
 ;
 ;
 L
 ,
 ;
 $9$$Z%9%9F
 $9$$Z%9%9F
 L
 L
  [!
" L#
$ L%
* '$$$ 
+* < QKM  __'' ')1'?C'  'R#J&#4  .248,
,
 	,

 ,
 ,
 +,
 2,
 ,
 
,
 ,
\  cc 	c
 c c +c 2c c 
c cJ    !
 !
 	!

 !
 !
 !
 
!
 !
r6   r@   c                     e Zd ZU dZded<   	 	 d#	 	 	 	 	 	 	 	 	 d$dZed%d       Zd&dZed'd       Z	d(dZ
ed)d	       Zed*d
       Zeed+d              Zed,d       Zed-d       Zed.d       Zed/d       Zed0d       Zeed1d              Zeed1d              Zed+d       Zeed'd              Zed2d       Zed+d       Zed3d       Zed4d       Zed4d       Z	 	 	 	 	 	 	 	 	 	 d5dZ	 	 	 	 	 	 	 	 d6dZed7d       Z e	 d8	 	 	 	 	 	 	 	 	 d9d       Z!e	 d:	 	 	 	 	 	 	 d;d       Z"e	 	 	 	 	 	 d<d       Z#e	 	 	 	 	 	 d=d        Z$eed+d!              Z%y")>BaseGroupera  
    This is an internal Grouper class, which actually holds
    the generated groups

    Parameters
    ----------
    axis : Index
    groupings : Sequence[Grouping]
        all the grouping instances to handle in this grouper
        for example for grouper list to groupby, need to pass the list
    sort : bool, default True
        whether this grouper will give sorted result or not

    r   r   c                j    t        |t              sJ |       || _        || _        || _        || _        y rI   )r-   r   r   
_groupings_sortdropna)rN   r   	groupingssortr   s        r4   rO   zBaseGrouper.__init__N  s5     $&,,&	#
r6   c                    | j                   S rI   )r   rN   s    r4   r   zBaseGrouper.groupings\  s    r6   c                ,    t        | j                        S rI   )iterindicesr   s    r4   __iter__zBaseGrouper.__iter__`  s    DLL!!r6   c                ,    t        | j                        S rI   )r<   r   r   s    r4   nkeyszBaseGrouper.nkeysc  s    4>>""r6   c              #  v   K   | j                  |      }| j                  }t        ||d      E d{    y7 w)
        Groupby iterator

        Returns
        -------
        Generator yielding sequence of (name, subsetted object)
        for each group
        TstrictN)_get_splitterresult_indexzip)rN   datasplitterkeyss       r4   get_iteratorzBaseGrouper.get_iteratorg  s5      %%d+  tXd333s   /979c                    t        |t              rt        }nt        } ||| j                  | j
                  | j                        S )zV
        Returns
        -------
        Generator yielding subsetted objects
        )
sorted_idssort_idx)r-   r!   SeriesSplitterFrameSplitterr   _sorted_idsresult_ilocs)rN   r   klasss      r4   r   zBaseGrouper._get_splitteru  sC     dF#(6E "ELL''&&	
 	
r6   c                   t        | j                        dk(  r4t        | j                  t              r| j                  d   j
                  }n9| j                  D cg c]  }|j                   }}t        || j                        }| j                  st        | j                  t              }|sV| j                  j                  r@|j                         D ci c]#  \  }}t        |      rt        j                  n||% }}}|S |r3|j                         D ci c]  \  }}t!        d |D              | }}}|S c c}w c c}}w c c}}w )z"dict {group name -> group indices}r9   r   c              3  V   K   | ]!  }t        |      rt        j                  n| # y wrI   )r   r.   r   ).0comps     r4   	<genexpr>z&BaseGrouper.indices.<locals>.<genexpr>  s     ITDJ"&&D8Is   '))r<   r   r-   r   r   r   codesr&   levelsr   r   hasnansitemsr   r.   r   tuple)rN   r   ping
codes_listhas_mikeyvalues          r4   r   zBaseGrouper.indices  s!    t~~!#
43D3DFV(W^^A&..F15@$**@J@%j$++>F{{ 1 1:>Fd//77JP,,.<FCd3iBFFS%7   
 '-lln #U ISII5P  ! A

s   D;(E Ec                   | j                   }| j                  r8t        j                  |dk\        }t        j                  |dk(        |   }||   }t        || j                        }| j                  r|t        j                  |      z  }|S )zR
        Get the original integer locations of result_index in the input.
        r   r   )r   rM   r.   whererr   r%   r   take)rN   r   r   	null_gapsr   s        r4   r	  zBaseGrouper.result_ilocs  s{     hh88C1H%D		#),T2Id)C'T\\:bggi00Fr6   c                T    | j                   D cg c]  }|j                   c}S c c}w rI   )r   r  rN   r  s     r4   r  zBaseGrouper.codes  s    '+~~6t

666   %c                    t        | j                        dkD  rt        | j                  j                        S | j                  gS Nr9   )r<   r   listr   r  r   s    r4   r  zBaseGrouper.levels  s9    t~~"))0011%%&&r6   c                T    | j                   D cg c]  }|j                   c}S c c}w rI   )r   rU   r  s     r4   nameszBaseGrouper.names  s    &*nn5d		555r   c                    | j                   }| j                  }|rt        j                  ||dk7     |      }ng }t	        || j
                  dd      S )z&
        Compute group sizes.
        r   )	minlengthr   F)indexr3   r   )r   r   r.   bincountr!   r   )rN   r   r   r   s       r4   rE   zBaseGrouper.size  sM    
 hh,,++c#)n@CCc!2!2'NNr6   c                   t        | j                        dk(  r| j                  d   j                  S | j                  \  }}|j                  }t        j                  |t        t        |                  }|j                         j                         D ci c]$  \  }}||   | j                  j                  |      & }}}|S c c}}w )!dict {group name -> group labels}r9   r   )
categories)r<   r   groupsresult_index_and_idsr8   r   
from_codesrange_reverse_indexerr  r   r  )rN   r   r   r   r,  group
axis_ilocsr   s           r4   r-  zBaseGrouper.groups  s     t~~!#>>!$+++ 55c%% ++CE#lBS<TU
 &0%@%@%B%H%H%J
 "z 5M499>>*55
 

 
s   )Cc                D    t        | j                  d      j                  S )NFr   )r   r   is_monotonic_increasingr   s    r4   is_monotoniczBaseGrouper.is_monotonic  s     TXXE*BBBr6   c                N    t        | j                  dk  j                               S )zE
        Whether grouper has null value(s) that are dropped.
        r   )r   r   rA   r   s    r4   rM   zBaseGrouper.has_dropped_na  s      TXX\&&())r6   c                    | j                   S rI   r   r   s    r4   
codes_infozBaseGrouper.codes_info  s     xxr6   c                ,    t        | j                        S rI   )r<   r   r   s    r4   r   zBaseGrouper.ngroups  s     4$$%%r6   c                     | j                   d   S )Nr   r.  r   s    r4   r   zBaseGrouper.result_index       ((++r6   c                     | j                   d   S r"  r=  r   s    r4   r   zBaseGrouper.ids  r>  r6   c           
     
   | j                   D cg c]#  }t        j                  |j                  d      % }}| j                   D cg c]  }|j                  xs |j
                    }}| j                   D cg c]  }|j                   }}t        t        | j                   |d            D ]3  \  }\  }}|j
                  s|j                  |j                        ||<   5 t        | j                         dk(  r5|d   }| j                  d   |_        t        | j                  d         }||fS t!        |      r/| j#                  || j                  | j                  |      \  }}||fS t%        |      s.| j'                  || j                  | j                        \  }}||fS | j                  }	| j                   D cg c]  }|j                   }
}t        |      D cg c]
  \  }}|s	| }}}t        |      D cg c]
  \  }}|r	| }}}| j#                  |D cg c]  }||   	 c}|D cg c]  }|
|   	 c}|D cg c]  }|	|   	 c}|D cg c]  }||   	 c}      \  }}| j'                  |D cg c]  }||   	 c}|D cg c]  }|
|   	 c}|D cg c]  }|	|   	 c}      \  }}t)        j*                  t)        j,                  |j                  t        |            t)        j.                  |j                  t        |      d	      gd	      }t)        j0                  ||z   d
      \  }}t3        t5        |j6                        t5        |j6                        z   |t5        |j                        t5        |j                        z         j9                  |      }t        |      |z  |z   }t%        |      rt        |      }t        t;        |      d      D cg c]  \  }}|s||z
   }}}t        |      dkD  r |j=                  |      j?                         }n|j?                         }|jA                  |      }t)        j0                  |d
      \  }}t        |      }|jA                  |      }nstC        |d      \  }}t        |      }t)        j*                  |t)        jD                  t)        jF                  t        |            |      g      }|jA                  |      }| jH                  r t)        jJ                  |dk  |dk  z  d|      }||fS c c}w c c}w c c}w c c}w c c}}w c c}}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}}w )NFr   Tr   r9   r   )r  r  r%  sorts)r  r  r%  r   return_indexr   r   )&r   r   _with_inferuniques	_observed_passed_categoricalr   	enumerater   set_categories
_orig_catsr<   r%  rU   r   r  rB   _ob_index_and_idsrA   _unob_index_and_idsr.   concatenatetilerepeatuniquer   r#  r  reorder_levelsreversed_drop_level_numbersargsortr  r"   deletearanger   r  )rN   r  r  obsrA  klevelr   r   r%  r  idxob
ob_indicesunob_indicesob_indexob_ids
unob_indexunob_idsresult_index_codes_r(  n_levelsr   drop_levelssorterrG  takers                               r4   r.  z BaseGrouper.result_index_and_ids  s    EINN
<@Edll7
 
 HL~~
?CDNN:$":":::
 
 )-777 )#dnnfT*R S 	BA}e''!00Aq		B t~~!#!!9L $

1L%djjm4CP S  O X $ 6 6

DJJ!L#L S  G S $ 8 8TZZ XL#D S   JJE,0NN;DTZZ;E;-6s^B'#rr#BJB/8~HGCRCHLH#55/9:s:-78cuSz8-78cuSz8-78cuSz8	  6  Hf $(#;#;/;<s<-9:cuSz:-9:cuSz: $< $ J "$GGJ,,c(m<IIhnnc*oAF " yy
!:NHAu%J--.hoo1FF(:++,tHNN/CC nU#	  j/F*X5C5zu: &/x%B!T sN 
 {#a')==kJRRTF)113F+00899V$?5)#.jjo  4CeDW)#.bii		#l2C(DgNO  ,007{{hh
x!|<b#FS  o

 8& <BH:888 =::0s_   (T+"T09T5?T:"
T?-T?
UU#U5U
UU?UU$
#U).U.c                V    t        d | j                  D              r| S | j                  S )Nc              3  4   K   | ]  }|j                     y wrI   )rH  )r  r  s     r4   r  z/BaseGrouper.observed_grouper.<locals>.<genexpr>e  s     9$t~~9s   )rB   r   _observed_grouperr   s    r4   observed_grouperzBaseGrouper.observed_grouperc  s%    9$..99K%%%r6   c                    | j                   D cg c]  }|j                   }}t        | j                  || j                  | j
                        }|S c c}w )N)r   r   )r   observed_groupingr   r   r   r   )rN   r  r   r   s       r4   rl  zBaseGrouper._observed_grouperj  sF    8<GT++G	GdiiDKKX Hs   Ac                   t        fddd  D              }|rd   nd}t        d |D              }t        ||dd      }t        ||      \  }	}
t	        |	      }	t        |	|
||d	      }t        |||d
      }|st        |      dkD  rt              }t        t              d      D cg c]  \  }}|s||z
   }}}t        |      dkD  r |j                  |      j                         }n|j                         }|j                  |      }t        j                  |d      \  }}t        j                  |	dk(  d|j                  |	            }	t	        |	      }	||	fS c c}}w )Nc              3  .   K   | ]  }d    |k(    yw)r   Nr   )r  r   rA  s     r4   r  z0BaseGrouper._ob_index_and_ids.<locals>.<genexpr>w  s      HdqT!1 Hs   r9   r   Fc              3  2   K   | ]  }t        |        y wrI   r<   r  r[  s     r4   r  z0BaseGrouper._ob_index_and_ids.<locals>.<genexpr>y       5Uc%j5   Tr   xnullrE  )rx  )r  r  r%  verify_integrityrC  r   )rB   r  r$   r"   r   r#   r   r<   rJ  rT  rU  rV  r  r.   rR  r  )rN   r  r  r%  rA  consistent_sortingsort_in_compressr   group_indexra  obs_group_idsob_index_codesr`  rf  r\  r   rg  rh  re  r(  s       `               r4   rM  zBaseGrouper._ob_index_and_idsp  sr    ! HeABi HH'958u5f55%eUTJ 4[GW X$V,-M5%t
  "	
 "c(ma&75zH "+8E?A!>C 3K 
 ;!#!55kBJJL!))+}}V,Hyyd;HAuXXflB

60BCF$V,s   0E%c                    t        d |D              }t        ||dd      }t        j                  ||      }t	        |      }||fS )Nc              3  2   K   | ]  }t        |        y wrI   rs  rt  s     r4   r  z2BaseGrouper._unob_index_and_ids.<locals>.<genexpr>  ru  rv  Trw  )r%  )r  r$   r   from_productr   )rN   r  r  r%  r   rc  rb  s          r4   rN  zBaseGrouper._unob_index_and_ids  sK     5f55"5%d$G,,V5A
&x08##r6   c              #     K   | j                   }t        | j                        dk(  r| y t        |j                  dz
  dd      D ]  }|j                  |        y w)Nr9   r   )r   r<   r   r0  nlevelsget_level_values)rN   r   r[  s      r4   get_group_levelszBaseGrouper.get_group_levels  s^      ((t~~!#|33a7R@ ;"33E::;s   AAc           	         |dv sJ t        ||| j                        } |j                  d|||| j                  | j                  d|S )z;
        Returns the values of a cython operation.
        )rv   ru   rJ   )r   r   r   r   r   r   )r@   rM   r   r   r   )rN   rK   r   rL   r   r   r   cy_ops           r4   _cython_operationzBaseGrouper._cython_operation  sb     1111Ts4CVCVW%u%% 
XXLL
 
 	
r6   c                \    | j                  ||      }|j                  j                  |      S )a1  
        Parameters
        ----------
        obj : Series
        func : function taking a Series and returning a scalar-like
        preserve_dtype : bool
            Whether the aggregation is known to be dtype-preserving.

        Returns
        -------
        np.ndarray or ExtensionArray
        )_aggregate_series_pure_pythonarray_cast_pointwise_result)rN   r2   r   preserve_dtyper   s        r4   
agg_serieszBaseGrouper.agg_series  s+      33C>yy//77r6   c                    t        j                  | j                  d      }d}| j                  |      }t	        |      D ]7  \  }} ||      }t        |      }|st        ||j                         d}|||<   9 |S )NOr   FT)r.   r   r   r   rJ  r>   r5   r3   )	rN   r2   r   r   initializedr   r   r2  r=   s	            r4   r  z)BaseGrouper._aggregate_series_pure_python  s~     $,,c2%%c*!(+ 		HAuu+C %C"34"F1I		 r6   c                x   d}| j                  |      }| j                  }g }t        ||d      }|D ]Q  \  }}	t        j	                  |	d|       |	j
                  }
 ||	      }|st        ||
      sd}|j                  |       S t        |      dk(  r$t        |dd       dv r ||j                  d d        ||fS )NFTr   rU   r   r   )rb   rd   rQ   rS   )r   r   r   r0   __setattr__axes_is_indexed_likeappendr<   r   iloc)rN   r   r   mutatedr   
group_keysresult_valueszippedr  r2  
group_axesr=   s               r4   apply_groupwisezBaseGrouper.apply_groupwise  s     %%d+&&
 Z$7  	&JC ufc2 JE(C#3C#D  %	& z?aGAz4$@ E
 %
 diimg%%r6   c                z    | j                   j                  | j                        }t        | dd      r||dk\     }|S )Nr   Tr   )r   r  r	  r   )rN   r   s     r4   r  zBaseGrouper._sorted_ids   s;     t00144(FaK(Fr6   N)TT)
r   r   r   list[grouper.Grouping]r   r   r   r   r   r   r   r  )r   zIterator[Hashable]r   r   )r   r   r   z#Iterator[tuple[Hashable, NDFrameT]])r   r+   r   DataSplitter)r   z$dict[Hashable, npt.NDArray[np.intp]]r   npt.NDArray[np.intp])r   z#list[npt.NDArray[np.signedinteger]]r   list[Index]r   list[Hashable])r   r!   )r   zdict[Hashable, Index]r   r   )r   r   )r   z"tuple[Index, npt.NDArray[np.intp]])r   r   )
r  r  r  list[npt.NDArray[np.intp]]r%  r  rA  z
list[bool]r   'tuple[MultiIndex, npt.NDArray[np.intp]])r  r  r  r  r%  r  r   r  )r   zGenerator[Index])r   )
rK   r   rL   r   r   r   r   r   r   r
   )F)r2   r!   r   r'   r  r   r   r
   )r2   r!   r   r'   r   znpt.NDArray[np.object_])r   r'   r   zDataFrame | Seriesr   ztuple[list, bool])&r   r   r   r   r   rO   propertyr   r   r   r  r   r   r   r   r	  r  r  r%  rE   r-  r6  rM   r:  r   r   r   r.  rm  rl  rM  rN  r  r  r  r  r  r  r   r6   r4   r   r   <  sb    K  * 	
  
  " # #4 
 
&  0   . 7 7 ' ' 6 6 O O   C  C *  *   &  & , , , , X! X!t & &  
& &  *&  	& 
 &  
1& P
$
$ *
$ 	
$
 
1
$ ; ;  

 	

 
 
 

 
2 BG88!)8;?8	8 8$ !)	  * %&%&!3%&	%& %&T   r6   r   c                      e Zd ZU dZded<   ded<   	 d	 ddZed        Zedd	       Z	edd
       Z
ddZed        Zedd       Zed        Zedd       Zedd       Zedd       Zedd       Zy)
BinGroupera  
    This is an internal Grouper class

    Parameters
    ----------
    bins : the split index of binlabels to group the item of axis
    binlabels : the label list
    indexer : np.ndarray[np.intp], optional
        the indexer created by Grouper
        some groupers (TimeGrouper) will sort its axis and its
        group_info is also sorted, so need the indexer to reorder

    Examples
    --------
    bins: [2, 4, 6, 8, 10]
    binlabels: DatetimeIndex(['2005-01-01', '2005-01-03',
        '2005-01-05', '2005-01-07', '2005-01-09'],
        dtype='datetime64[ns]', freq='2D')

    the group_info, which contains the label of each item in grouped
    axis, the index of label in label list, group number, is

    (array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4]), array([0, 1, 2, 3, 4]), 5)

    means that, the grouped axis has 10 items, can be grouped into 5
    labels, the first and second items belong to the first label, the
    third and forth items belong to the second label, and so on

    znpt.NDArray[np.int64]binsr   	binlabelsNc                    t        |      | _        t        |      | _        || _        t        | j                        t        | j                        k(  sJ y rI   )r   r  r    r  indexerr<   )rN   r  r  r  s       r4   rO   zBinGrouper.__init__L  sD     !&	%i0 4>>"c$))n444r6   c                    t        | j                  | j                  d      D ci c]  \  }}|t        ur|| }}}|S c c}}w )r+  Tr   )r   r  r  r   )rN   r  r  r   s       r4   r-  zBinGrouper.groupsZ  sM     "$..$))DI
U#~ J
 

 
s   A c                     yr"  r   r   s    r4   r   zBinGrouper.nkeysf  s     r6   c                    | j                   }| j                  &t        j                  || j                  f      }||   }|S rI   )r   r  r.   lexsort)rN   r   rh  s      r4   r:  zBinGrouper.codes_infok  s<     hh<<#ZZdll 34Ff+C
r6   c              #     K   fd}d}t        | j                  | j                  d      D ]  \  }}|t        ur| |||      f |} |t	              k  r| j                  d    ||d      f yyw)r   c                "    j                   | | S rI   )r  )startedger   s     r4   <lambda>z)BinGrouper.get_iterator.<locals>.<lambda>}  s    TYYuT%: r6   r   Tr   r   N)r   r  r  r   r<   )rN   r   slicerr  r  labels    `    r4   r  zBinGrouper.get_iteratort  s      ; !tyy$..F 	KD%CVE4000E	
 3t9..$fUD&999 s   A3A6c                    t        j                  t              }d}t        | j                  | j
                  d      D ]-  \  }}||k  s|t        urt        t        ||            ||<   |}/ |S Nr   Tr   )collectionsdefaultdictr#  r   r  r  r   r0  )rN   r   r   r  bins        r4   r   zBinGrouper.indices  sj    ))$/dnndiiE 	JE33w#%)%3-%8GEN		
 r6   c                    | j                   gS rI   r9  r   s    r4   r  zBinGrouper.codes  s    zr6   c                .   | j                   }t        | j                         dk7  rt        | j                   d         r|dd  }t        |      }t        j                  t        j
                  d| j                  f         }t        |      }|t        | j                        k(  r*t        j                  t        j                  |      |      }n<t        j                  t        j
                  dt        j                  |      f   |      }t        |      }||fS )Nr   r9   r   )
r  r<   r   r.   diffr_r  r   rQ  rX  )rN   r   r   repr   s        r4   r.  zBinGrouper.result_index_and_ids  s    ~~t~~!#T^^A->(?'+Ll#ggbeeAtyyL)*!#&c$))n$))BIIg.4C))BEE"bii&8"893?C!#&S  r6   c                    | j                   gS rI   )r  r   s    r4   r  zBinGrouper.levels  s    r6   c                0    | j                   j                  gS rI   )r  rU   r   s    r4   r%  zBinGrouper.names  s    ##$$r6   c                    | j                   }| j                  }|j                  |      }t        j                  ||dd |j
                        }|gS )NF)in_axisr[  rG  )r  r   r  r   Groupingr8   )rN   levr  r   r  s        r4   r   zBinGrouper.groupings  sI    nn%FEs{{
 vr6   c                    | S rI   r   r   s    r4   rm  zBinGrouper.observed_grouper  s    r6   rI   r   r   r  r  )r   r+   )r   r  r  r  r  )r   r  )r   r   r   r   r   rO   r   r-  r  r   r:  r  r   r  r.  r  r%  r   rm  r   r6   r4   r  r  *  s    <   	5
 
5 	 	    :( 	 	   ! !"     % %    r6   r  c                    t        | t              r-t        |      dkD  ry| j                  j	                  |d         S t        | t
              r| j                  j	                  |d         S y)Nr9   Fr   )r-   r!   r<   r(  equalsr   )r2   r  s     r4   r  r    sY    #vt9q=yyQ((	C	#yyQ((r6   c                  J    e Zd Z	 	 	 	 	 	 	 	 	 	 ddZddZedd       Zd	dZy)
r  c               <    || _         || _        || _        || _        y rI   )r   r   _slabels	_sort_idx)rN   r   r   r  r  s        r4   rO   zDataSplitter.__init__  s      	"!r6   c              #    K   | j                   dk(  ry t        j                  | j                  | j                         \  }}| j                  }t        ||d      D ]#  \  }}| j                  |t        ||             % y wr  )r   r	   generate_slicesr  _sorted_datar   _chopslice)rN   startsendssdatar  ends         r4   r   zDataSplitter.__iter__  st     <<1 **4==$,,G!!fd48 	7JE3**UE%$566	7s   A?Bc                P    | j                   j                  | j                  d      S )Nr   rB  )r   r  r  r   s    r4   r  zDataSplitter._sorted_data  s    yy~~dnn1~55r6   c                    t        |       rI   r   )rN   r  	slice_objs      r4   r  zDataSplitter._chop  s    !$''r6   N)
r   r   r   r   r  r  r  r  r   r   )r   r*   )r   r   )r  r  r   r+   )r   r   r   rO   r   r   r  r  r   r6   r4   r  r    sU    "" "
 '" )" 
"	7 6 6(r6   r  c                      e Zd ZddZy)r  c                    |j                   j                  |      }|j                  ||j                        }|j                  |_        |j                  |d      S )Nr  groupbymethod)_mgr	get_slice_constructor_from_mgrr  rU   _name__finalize__)rN   r  r  mgrsers        r4   r  zSeriesSplitter._chop  sP    jj""9-))#CHH)=JJ	i88r6   N)r  r!   r  r  r   r!   r   r   r   r  r   r6   r4   r  r    s    9r6   r  c                      e Zd ZddZy)r  c                    |j                   j                  |d      }|j                  ||j                        }|j	                  |d      S )Nr9   rB  r  r  r  )r  r  r  r  r  )rN   r  r  r  dfs        r4   r  zFrameSplitter._chop  sI     jj""91"5((388(<uY77r6   N)r  r   r  r  r   r   r  r   r6   r4   r  r    s    8r6   r  r  r  )Kr   
__future__r   r  r   typingr   r   r   r   numpyr.   pandas._libsr   r	   pandas._libs.groupby_libsr  r   pandas._typingr
   r   r   r   r   pandas.errorsr   pandas.util._decoratorsr   pandas.core.dtypes.castr   pandas.core.dtypes.commonr   r   r   r   r   pandas.core.dtypes.missingr   r   pandas.core.arraysr   pandas.core.framer   pandas.core.groupbyr   pandas.core.indexes.apir   r   r   r    pandas.core.seriesr!   pandas.core.sortingr"   r#   r$   r%   r&   collections.abcr'   r(   r)   r*   pandas.core.genericr+   r5   r>   r@   r   r  r  r  r  r  r   r6   r4   <module>r     s    #     * )  . 2 
 + ' '  &    ,>O
 O
dk k\T Tn(78$ (D9\ 98L 8r6   