
    Piϣ                    &   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          ZJdS )(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)NDFramereturnNonec                r    t          | t          j                  r|t          k    rt	          d          d S d S )NzMust produce aggregated value)
isinstancenpndarrayobject
ValueError)objdtypes     k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/core/groupby/ops.pycheck_result_arrayr7   Q   sA     #rz"" >F?? <===	> >?    c                    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   )hasattrr:   ndimlen)ress    r6   extract_resultr@   \   sD    
 sI k8q==SXX]]a&CJr8   c                     e Zd ZU dZ eg d          ZdWd
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/d0d1Zd2ed3<   d)d4iZedXd5            Zeej        dYd9                        ZdZd<Zd[d@Zd\dAZd\dBZedCdCdDdEd]dM            ZedDdNd]dO            Zed^dS            ZedTdUd_dV            ZdCS )`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idxmaxkindstrhowhas_dropped_naboolr,   r-   c                0    || _         || _        || _        d S NrJ   rL   rM   )selfrJ   rL   rM   s       r6   __init__zWrappedCythonOp.__init__~   s    	,r8   rC   )val_testrD   sum	group_sumprod
group_prodrH   namerI   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cummaxrE   )	aggregate	transformzdict[str, dict]_CYTHON_FUNCTIONS   c                (    || j         d         v rdS dS )Nrz   r{   )r|   )clsrL   s     r6   get_kind_from_howz!WrappedCythonOp.get_kind_from_how   s    #'444;{r8   r5   np.dtype
is_numericc                r   |j         }| j        |         |         }t          |          r|}nt          t          |          }|r|S |t          j        t                    k    rF|dv rt          d| d| d          |dv r|S |dv rnd|j	        vrt          d| d| d          |S t          d|          )	N)ra   rv   z2function is not implemented for this dtype: [how->z,dtype->])re   rf   rH   rI   rg   ri   r2   zPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/)
rZ   r|   callablegetattr
libgroupbyr0   r5   r2   NotImplementedError__signatures__)r   rJ   rL   r5   r   	dtype_strftypefs           r6   _get_cython_functionz$WrappedCythonOp._get_cython_function   s.   
 J	%d+C0 E?? 	+AA
E**A 	Hbhv&&&&+++)7 7 7*37 7 7   :::(((!111)7 7 7*37 7 7   H%0  r8   values
np.ndarrayc                
   | j         }|dv rt          |          }ng|j        j        dv rY|dv s| j        dk    r| j        rt          |          }n3|dv r/|j        j        dk    rt          |          }nt          |          }|S )z
        Cast numeric dtypes to float64 for functions that only support that.

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

        Returns
        -------
        values : np.ndarray
        )ra   re   rf   rg   ri   iu)rc   r_   r{   )rU   ro   rW   rw   rv   i)rL   r   r5   rJ   rM   r   r   )rR   r   rL   s      r6   _get_cython_valsz WrappedCythonOp._get_cython_vals   s     h::: $F++FF\$&&o%%	[((T-@( (//DDD<$++)&11FF*622Fr8   ngroupsintr   c                    | j         }| j        }| j                            |d          }|dk    r||f}n6|dk    rt	          d          |dk    r|j        }n|g|j        dd          R }|S )Nr;   ro   z<arity of more than 1 is not supported for the 'how' argumentr{   )rL   rJ   _cython_aritygetr   shape)rR   r   r   rL   rJ   arity	out_shapes          r6   _get_output_shapez!WrappedCythonOp._get_output_shape   s    hy"&&sA.. &== %(IIQYY%N   [  II 46<#344Ir8   c                    | j         }|dk    rd}n#|dv rd}n|j        dv r|j         |j         }nd}t          j        |          S )NrE   float64rH   rI   intpiufcbr2   )rL   rJ   itemsizer0   r5   )rR   r5   rL   	out_dtypes       r6   _get_out_dtypezWrappedCythonOp._get_out_dtype  sf    h&==!II((( IIZ7"" :7u~77II Ix	"""r8   c                    | j         }|dv r<|t          j        t                    k    rt          j        t          j                  S n6|dv r2|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.
        )rU   rw   rU   rW   rv   )r_   ra   rc   re   rf   fciub)rL   r0   r5   rN   int64rJ   r   )rR   r5   rL   s      r6   _get_result_dtypez!WrappedCythonOp._get_result_dtype  s     h===&&x))) ';;;zT!!u$$x
+++r8   Nr   )maskresult_maskinitial	min_countcomp_idsr   npt.NDArray[np.bool_] | Noner   r   r   c          
         |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 )Nr;   )r   r   r   r   r   r   r   )r=   _call_cython_opr   T)rR   r   r   r   r   r   r   r   kwargsvalues2dr?   s              r6   _cython_op_ndim_compatz&WrappedCythonOp._cython_op_ndim_compat7  s     ;!dAAAgHD!!!G}&)$'2&$&	#!'	 	 	 	C y|q  1v 5L#t#	
#	
 	
 	
 	
 		
r8   )r   c                  |}	|j         }
|
j        dv }|
j        dv }| j        dv r|t          |          }|r|                    d          }d}n |
j        dk    r|                    d          }|j         dk    r|                    t          j                  }| j        dv r}|
t          k    r7|d	         r/|-|	                                r|
                                }d||<   |                    t          d
                              t          j                  }d}|j        }||j        }||j        }|                     ||          }|                     | j        | j        |j         |          }|                     |          }|                     |j                   }t%          t          j        ||                    }| j        dk    rt          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| |                    t          d
          }nt| j        dv r5 |d#||||||d| |
t          k    r|                    t                    }n6t-          | j         d          | j        dk    r||d<    |d#||||||d| | j        dk    r| j        dvr|j         j        dv rz|sxt/          | j        dv rd nd!|          }||k     }|	                                rC|||                                         sJ n$|                    d"          }t          j        ||<   |j        }| j        | j        vr+|                     |	j                   }t9          ||          }n|}|S )$Nr   mM)rC   rD   r   Tbuint8float16skipnaFcopyr5   rz   )	rH   rI   r[   r]   r_   rm   rk   rU   ra   rU   r   )outcountsr   labelsr   r   r   is_datetimelike)rf   re   rc   ro   rW   )re   rf   r   )r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   z is not implementedrE   r   )r   r   r   r   r   r   r   r   )rU   rW   r   r;   r    )r5   rJ   rL   r   viewastyper0   float32r2   rC   r   rN   int8r   r   r   r   r   r   emptyzerosr   r   r]   rD   nancast_blocklistr   r   )rR   r   r   r   r   r   r   r   r   orig_valuesr5   r   r   r   funcr   resultr   cutoffempty_groups	res_dtype	op_results                         r6   r   zWrappedCythonOp._call_cython_opf  s    Z7*
*,8~%%|F|| 	*[[))FJJZ3[[))F<9$$]]2:..F8~%%(# ,'DHHJJ'!''+t]]4e]4499"'BBFJ6D&)m**7F;;	((DHflJWW&&v..''55	BHYi@@@AA9##XgRX666Fx 
 
 
 8u$$ )0F9% 
!!#' +$3
 
 
 
 
 
 BBB8~--0?F,-		
 ( +	 	 	 	 	 	 ^++ !# +      t%88--- !!# +     F??#]]622F *TX*J*J*JKKK x6!!(3}%D  /     9##8L(L(L | D((($(o"="=QQ1iPP%##%% 6".*<8<<>>>>>> "(y!9!9/1v|,84...
 ..{/@AAI/	BBIIIr8   axisr   r
   c                    |j         dk    rt          d          |j         dk    r|dk    s
J |            d S t          |j                  s|dk    sJ d S d S )N   z.number of dimensions is currently limited to 2r;   r   )r=   r   r   r5   )rR   r   r   s      r6   _validate_axiszWrappedCythonOp._validate_axis  sr    ;??%&VWWW;!1999d99999$V\22 	 19999	 	 9r8   )r   c          	         |                      ||           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/   r0   r1   _groupby_oprL   rM   r   )rR   r   r   r   r   r   r   s          r6   cython_operationz WrappedCythonOp.cython_operation  s     	D&)))&"*-- 		%6% H#2#     +t*

 
 
 
 	
r8   )rJ   rK   rL   rK   rM   rN   r,   r-   )rL   rK   r,   rK   )rJ   rK   rL   rK   r5   r   r   rN   )r   r   r,   r   )r   r   r   r   r,   r   )r5   r   r,   r   )r   r   r   r   r   r   r   r   r   r   r   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   rS   	functoolspartialr   group_any_allgroup_idxmin_idxmaxrd   r|   __annotations__r   classmethodr   cacher   r   r   r   r   r   r   r   r   r   r   r8   r6   rB   rB   j   sQ          YCCC N- - - -
$9$Z%=NNN
$9$Z%=NNN
 ;
 L	

 'i'
(FXVVV
 'i'
(FXVVV
 ;
 ;
 L
 ,
 ;
 $9$Z%9FFF
 $9$Z%9FFF
 L
 L
  [!
" L#
$ L%
 
* '$$$ 
 
+* *    < QKM   [ _' ' ' _ ['R# # # #J   &# # # #   4  .248,
 ,
 ,
 ,
 ,
 U,
\  c c c c c UcJ    U  !
 !
 !
 !
 !
 U!
 !
 !
r8   rB   c                     e Zd ZU dZded<   	 	 dRdSdZedTd            ZdUdZedVd            Z	dWdZ
edXd            ZedYd            ZeedZd                        Zed[d            Zed\d             Zed]d"            Zed^d$            Zed_d&            Zeed`d'                        Zeed`d(                        ZedZd)            ZeedVd*                        Zedad+            ZedZd,            Zedbd.            Zedcd/            Zedcd0            Zddd8Zded9Zedfd;            Z e	 dgdhdC            Z!e	 didjdI            Z"edkdK            Z#edldO            Z$eedZdP                        Z%dQS )m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   T	groupingslist[grouper.Grouping]sortrN   dropnar,   r-   c                |    t          |t                    s
J |            || _        || _        || _        || _        d S rP   )r/   r   r   
_groupings_sortr   )rR   r   r   r   r   s        r6   rS   zBaseGrouper.__init__N  sB     $&&,,,,&	#
r8   c                    | j         S rP   )r   rR   s    r6   r   zBaseGrouper.groupings\  s
    r8   Iterator[Hashable]c                *    t          | j                  S rP   )iterindicesr   s    r6   __iter__zBaseGrouper.__iter__`  s    DL!!!r8   r   c                *    t          | j                  S rP   )r>   r   r   s    r6   nkeyszBaseGrouper.nkeysc  s    4>"""r8   datar   #Iterator[tuple[Hashable, NDFrameT]]c              #  r   K   |                      |          }| j        }t          ||d          E d{V  dS )
        Groupby iterator

        Returns
        -------
        Generator yielding sequence of (name, subsetted object)
        for each group
        TstrictN)_get_splitterresult_indexzip)rR   r   splitterkeyss       r6   get_iteratorzBaseGrouper.get_iteratorg  sP       %%d++ tXd333333333333r8   r+   DataSplitterc                    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)rR   r   klasss      r6   r  zBaseGrouper._get_splitteru  sQ     dF## 	"(6EE "EuL'&	
 
 
 	
r8   $dict[Hashable, npt.NDArray[np.intp]]c                   t          | j                  dk    r-t          | j        t                    r| j        d         j        }n&d | j        D             }t          || j                  }| j        sgt          | j        t                    }|s+| j        j
        rd |                                D             }n |rd |                                D             }|S )z"dict {group name -> group indices}r;   r   c                    g | ]	}|j         
S r   codes.0pings     r6   
<listcomp>z'BaseGrouper.indices.<locals>.<listcomp>  s    @@@$*@@@r8   c                P    i | ]#\  }}t          |          rt          j        n||$S r   r   r0   r   r  keyvalues      r6   
<dictcomp>z'BaseGrouper.indices.<locals>.<dictcomp>  s<       <FCd3ii0BFFS%  r8   c                H    i | ]\  }}t          d  |D                       | S )c              3  P   K   | ]!}t          |          rt          j        n|V  "d S rP   r  )r  comps     r6   	<genexpr>z1BaseGrouper.indices.<locals>.<dictcomp>.<genexpr>  s3      IITDJJ8"&&DIIIIIIr8   )tupler  s      r6   r  z'BaseGrouper.indices.<locals>.<dictcomp>  sG        #U IISIIIII5  r8   )r>   r   r/   r  r   r   r&   levelsr   r   hasnansitems)rR   r   
codes_listhas_mis       r6   r   zBaseGrouper.indices  s     t~!##
43DFV(W(W#^A&.FF@@@@@J%j$+>>F{ 	 1:>>F 
d/7 
 JP,,..      '-llnn   r8   npt.NDArray[np.intp]c                   | j         }| j        r>t          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   r0   whererw   r%   r   take)rR   r   r   	null_gapsr   s        r6   r  zBaseGrouper.result_ilocs  s     h 	8C1H%%D	#),,T2Id)C'T\:: 	1bgi000Fr8   #list[npt.NDArray[np.signedinteger]]c                $    d | j         D             S )Nc                    g | ]	}|j         
S r   r  r  s     r6   r  z%BaseGrouper.codes.<locals>.<listcomp>  s    666t
666r8   r   r   s    r6   r  zBaseGrouper.codes  s    66t~6666r8   list[Index]c                t    t          | j                  dk    rt          | j        j                  S | j        gS Nr;   )r>   r   listr  r$  r   s    r6   r$  zBaseGrouper.levels  s7    t~"")0111%&&r8   list[Hashable]c                $    d | j         D             S )Nc                    g | ]	}|j         
S r   rY   r  s     r6   r  z%BaseGrouper.names.<locals>.<listcomp>  s    555d	555r8   r1  r   s    r6   nameszBaseGrouper.names  s    55dn5555r8   r!   c                    | j         }| j        }|r!t          j        ||dk             |          }ng }t	          || j        dd          S )z&
        Compute group sizes.
        r   )	minlengthr   F)indexr5   r   )r   r   r0   bincountr!   r  )rR   r   r   r   s       r6   rG   zBaseGrouper.size  sW    
 h, 	+c#)n@@@CCCc!2'NNNNr8   dict[Hashable, Index]c                F    t           j                  dk    r j        d         j        S  j        \  }}|j        t          j        |t          t          |                              } fd|                                	                                D             }|S )!dict {group name -> group labels}r;   r   )
categoriesc                X    i | ]&\  }}|         j                             |          'S r   )r   r,  )r  group
axis_ilocsrR   r   s      r6   r  z&BaseGrouper.groups.<locals>.<dictcomp>  sC     
 
 
 "z 5M49>>*55
 
 
r8   )
r>   r   groupsresult_index_and_idsr:   r   
from_codesrange_reverse_indexerr&  )rR   r  r   rA  r   r   s   `    @r6   rE  zBaseGrouper.groups  s     t~!##>!$++ 5c% +CE#lBSBS<T<TUUU

 
 
 
 
 &0%@%@%B%B%H%H%J%J
 
 

 r8   c                8    t          | j        d          j        S )NFr   )r   r   is_monotonic_increasingr   s    r6   is_monotoniczBaseGrouper.is_monotonic  s     TXE***BBr8   c                V    t          | j        dk                                               S )zE
        Whether grouper has null value(s) that are dropped.
        r   )rN   r   rC   r   s    r6   rM   zBaseGrouper.has_dropped_na  s$     TX\&&(()))r8   c                    | j         S rP   r   r   s    r6   
codes_infozBaseGrouper.codes_info  s     xr8   c                *    t          | j                  S rP   )r>   r  r   s    r6   r   zBaseGrouper.ngroups  s     4$%%%r8   c                    | j         d         S )Nr   rF  r   s    r6   r  zBaseGrouper.result_index       (++r8   c                    | j         d         S r4  rS  r   s    r6   r   zBaseGrouper.ids  rT  r8   "tuple[Index, npt.NDArray[np.intp]]c           
     	   d | j         D             d | j         D             }d | j         D             t          t          | j         d                    D ],\  }\  }}|j        r|                    |j                  |<   -t          | j                   dk    r6d         }| j        d         |_        t          | j
        d                   }nt          |          r'|                     | j
        | j                  \  }}n{t          |          s&|                     | j
        | j                  \  }}nF| j        d | j         D             d	 t          |          D             }d
 t          |          D             }|                     fd|D             fd|D             fd|D             fd|D                       \  }	}
|                     fd|D             fd|D             fd|D                       \  }}t          j        t          j        |j
        t          |	                    t          j        |	j
        t          |          d          gd          }t          j        ||z   d          \  }}t)          t+          |j                  t+          |	j                  z   |t+          |j                  t+          |	j                  z                                 |          }t          |          |
z  |z   }t                    rt                    fdt          t1                    d          D             }t          |          dk    r(|                    |                                          }n|                                }|                    |          }t          j        |d          \  }}t          |          }|                    |          }nt9          |d          \  }}t          |          }t          j        |t          j        t          j        t          |                    |          g          }|                    |          }||fS )Nc                D    g | ]}t          j        |j        d           S )Fr   )r   _with_inferuniquesr  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>
  s7     
 
 
<@Edl777
 
 
r8   c                ,    g | ]}|j         p|j         S r   )	_observed_passed_categoricalr  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>  s2     
 
 
?CDN:$"::
 
 
r8   c                    g | ]	}|j         
S r   )r   r  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>  s    777777r8   Tr   r;   r   c                    g | ]	}|j         
S r   r  r  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>#  s    ;;;DTZ;;;r8   c                    g | ]	\  }}||
S r   r   r  idxobs      r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>$  s!    BBB'#rrB#BBBr8   c                    g | ]	\  }}||
S r   r   ra  s      r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>%  s!    HHHGCRHCHHHr8   c                     g | ]
}|         S r   r   r  rb  r$  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>'  s    :::s:::r8   c                     g | ]
}|         S r   r   r  rb  r  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>(      888cuSz888r8   c                     g | ]
}|         S r   r   r  rb  r9  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>)  ri  r8   c                     g | ]
}|         S r   r   )r  rb  sortss     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>*  ri  r8   )r$  r  r9  rm  c                     g | ]
}|         S r   r   rf  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>-  s    <<<s<<<r8   c                     g | ]
}|         S r   r   rh  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>.      :::cuSz:::r8   c                     g | ]
}|         S r   r   rk  s     r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>/  rp  r8   )r$  r  r9  r   return_indexc                $    g | ]\  }}||z
  S r   r   r  rb  r   n_levelss      r6   r  z4BaseGrouper.result_index_and_ids.<locals>.<listcomp>D  s7       !TsN  r8   Fr   )r   	enumerater  r]  set_categories
_orig_catsr>   r9  rZ   r   r  rD   _ob_index_and_idsrC   _unob_index_and_idsr0   concatenatetilerepeatuniquer   r5  r$  reorder_levelsreversed_drop_level_numbersargsortr,  r"   deletearange)rR   obskr  levelr  r   
ob_indicesunob_indicesob_indexob_ids
unob_indexunob_idsresult_index_codes_r<  drop_levelssorterrZ  takerr  r$  rw  r9  rm  s                       @@@@@r6   rF  z BaseGrouper.result_index_and_ids  s   
 
DHN
 
 

 
GK~
 
 
 87777 )#dnfT*R*R*R S S 	B 	BA}e' B!00AAq	t~!##!!9L $
1L%djm44CCXX ?	8 $ 6 6
DJ! !L## S ;	8 $ 8 8TZ X XL## JE;;DN;;;EBBYs^^BBBJHHy~~HHHL#55::::z:::8888Z8888888Z8888888Z888	  6    Hf $(#;#;<<<<|<<<::::\:::::::\::: $< $ $ J "$GJ,c(mm<<Ihnc*ooAFFF " " " y
!:NNNHAu%J-..ho1F1FF(:+,,tHN/C/CC   nU##	 
 j//F*X5C5zz 8u::   %.x%B%B  
 {##a'')==kJJRRTTFF)1133F+00889V$???5)#..jjoo  4CeDDDW)#..bi	#l2C2C(D(DgNNO   ,0077S  r8   c                P    t          d | j        D                       r| S | j        S )Nc              3  $   K   | ]}|j         V  d S rP   )r\  r  s     r6   r"  z/BaseGrouper.observed_grouper.<locals>.<genexpr>_  s$      99$t~999999r8   )rD   r   _observed_grouperr   s    r6   observed_grouperzBaseGrouper.observed_grouper]  s1    99$.99999 	K%%r8   c                l    d | j         D             }t          | j        || j        | j                  }|S )Nc                    g | ]	}|j         
S r   )observed_groupingr  s     r6   r  z1BaseGrouper._observed_grouper.<locals>.<listcomp>f  s    GGGT+GGGr8   )r   r   )r   r   r   r   r   )rR   r   r   s      r6   r  zBaseGrouper._observed_grouperd  s8    GGGGG	diDKXXXr8   r$  r  list[npt.NDArray[np.intp]]r9  rm  
list[bool]'tuple[MultiIndex, npt.NDArray[np.intp]]c                d   t          fddd          D                       }|rd         nd}t          d |D                       }t          ||dd          }t          ||          \  }	}
t	          |	          }	t          |	|
||d	          }t          |||d
          }|st          |          dk    rt                    fdt          t                    d          D             }t          |          dk    r(|
                    |                                          }n|                                }|                    |          }t          j        |d          \  }}t          j        |	dk    d|                    |	                    }	t	          |	          }	||	fS )Nc              3  0   K   | ]}d          |k    V  dS )r   Nr   )r  r   rm  s     r6   r"  z0BaseGrouper._ob_index_and_ids.<locals>.<genexpr>q  s,       H HdqT!1 H H H H H Hr8   r;   r   Fc              3  4   K   | ]}t          |          V  d S rP   r>   r  r  s     r6   r"  z0BaseGrouper._ob_index_and_ids.<locals>.<genexpr>s  (      55Uc%jj555555r8   Tr   xnullrx  )r  )r$  r  r9  verify_integrityc                $    g | ]\  }}||z
  S r   r   rv  s      r6   r  z1BaseGrouper._ob_index_and_ids.<locals>.<listcomp>  s7       C3  r8   rs  r   )rD   r#  r$   r"   r   r#   r   r>   ry  r  r  r  r,  r0   r  r+  )rR   r$  r  r9  rm  consistent_sortingsort_in_compressr   group_indexr  obs_group_idsob_index_codesr  r  r  r  r<  rw  s       `            @r6   r|  zBaseGrouper._ob_index_and_idsj  s    ! H H H HeABBi H H HHH'9D588u55f55555%eUTJJJ 4[GW X X X$V,,-M5%t
 
 
  "	
 
 
 " 	Dc(mma&7&75zzH   !*8E??A!>!>  K
 ;!##!55kBBJJLL!))++}}V,,Hyd;;;HAuXflB

60B0BCCF$V,,r8   c                    t          d |D                       }t          ||dd          }t          j        ||          }t	          |          }||fS )Nc              3  4   K   | ]}t          |          V  d S rP   r  r  s     r6   r"  z2BaseGrouper._unob_index_and_ids.<locals>.<genexpr>  r  r8   Tr  )r9  )r#  r$   r   from_productr   )rR   r$  r  r9  r   r  r  s          r6   r}  zBaseGrouper._unob_index_and_ids  sc     55f55555"5%d$GGG,V5AAA
&x008##r8   Generator[Index]c              #     K   | j         }t          | j                  dk    r|V  d S t          |j        dz
  dd          D ]}|                    |          V  d S )Nr;   r   )r  r>   r   rH  nlevelsget_level_values)rR   r  r  s      r6   get_group_levelszBaseGrouper.get_group_levels  s       (t~!##|3a7R@@ ; ;"33E::::::; ;r8   r   rJ   rK   rL   r   r   r
   c           	     v    |dv sJ t          ||| j                  } |j        d|||| j        | j        d|S )z;
        Returns the values of a cython operation.
        )r{   rz   rQ   )r   r   r   r   r   r   )rB   rM   r   r   r   )rR   rJ   r   rL   r   r   r   cy_ops           r6   _cython_operationzBaseGrouper._cython_operation  sk     11111Ts4CVWWW%u% 
XL
 
 
 
 	
r8   Fr4   r   r'   preserve_dtypec                b    |                      ||          }|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)rR   r4   r   r  r   s        r6   
agg_serieszBaseGrouper.agg_series  s/      33C>>y//777r8   npt.NDArray[np.object_]c                   t          j        | j        d          }d}|                     |          }t	          |          D ]=\  }} ||          }t          |          }|st          ||j                   d}|||<   >|S )NOr   FT)r0   r   r   r  ry  r@   r7   r5   )	rR   r4   r   r   initializedr  r   rC  r?   s	            r6   r  z)BaseGrouper._aggregate_series_pure_python  s     $,c222%%c**!(++ 		 		HAu$u++C %%C #"3444"F1IIr8   r   DataFrame | Seriestuple[list, bool]c                   d}|                      |          }| j        }g }t          ||d          }|D ]\\  }}	t                              |	d|           |	j        }
 ||	          }|st          ||
          sd}|                    |           ]t          |          dk    r+t          |dd           dv r ||j
        d d                    ||fS )NFTr   rZ   r   r   )rg   ri   rU   rW   )r  r  r  r2   __setattr__axes_is_indexed_likeappendr>   r   iloc)rR   r   r   mutatedr  
group_keysresult_valueszippedr  rC  
group_axesr?   s               r6   apply_groupwisezBaseGrouper.apply_groupwise  s    %%d++&
 Z$777  	& 	&JC ufc222 J!E((C #3C#D#D   %%%%z??aGAz4$@$@ E
 %
 %
 Adimg%%r8   c                ~    | j                             | j                  }t          | dd          r||dk             }|S )Nr   Tr   )r   r,  r  r   rR   r   s     r6   r  zBaseGrouper._sorted_ids  sA     t01144(( 	)FaK(Fr8   N)TT)
r   r   r   r   r   rN   r   rN   r,   r-   r,   r   )r,   r   r,   r   )r   r   r,   r   )r   r+   r,   r  )r,   r  r,   r)  )r,   r.  r,   r2  r,   r6  )r,   r!   )r,   r>  r,   rN   )r,   r   )r,   rV  )r,   r   )
r$  r2  r  r  r9  r6  rm  r  r,   r  )r$  r2  r  r  r9  r6  r,   r  )r,   r  )r   )
rJ   rK   rL   rK   r   r   r   r   r,   r
   )F)r4   r!   r   r'   r  rN   r,   r
   )r4   r!   r   r'   r,   r  )r   r'   r   r  r,   r  )&r   r   r   r   r   rS   propertyr   r   r   r  r   r  r   r   r  r  r$  r9  rG   rE  rL  rM   rP  r   r  r   rF  r  r  r|  r}  r  r  r  r  r  r  r   r8   r6   r   r   <  s          KKK         X" " " " # # # X#4 4 4 4 
 
 
 U
&    ^0    ^ U. 7 7 7 X7 ' ' ' X' 6 6 6 X6 O O O UO    ^ C C C ^ UC * * * ^ U*    ^ & & & ^ U& , , , X, , , , X, R! R! R! ^R!h & & & X&    ^
&  &  &  & P
$ 
$ 
$ 
$ ; ; ; U;  
 
 
 
 U
2 BG8 8 8 8 U8$    U* %& %& %& U%&T    ^ U  r8   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dS )'
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   	binlabelsNr,   r-   c                    t          |          | _        t          |          | _        || _        t          | j                  t          | j                  k    sJ d S rP   )r   r  r    r  indexerr>   )rR   r  r  r  s       r6   rS   zBinGrouper.__init__F  sT     !&&	%i00 4>""c$)nn444444r8   c                R    d t          | j        | j        d          D             }|S )r@  c                ,    i | ]\  }}|t           u||S r   )r   r  s      r6   r  z%BinGrouper.groups.<locals>.<dictcomp>Y  s/     
 
 
U#~~ ~~r8   Tr   )r  r  r  r  s     r6   rE  zBinGrouper.groupsT  s:    

 
!$.$)DIII
 
 

 r8   r   c                    dS r4  r   r   s    r6   r   zBinGrouper.nkeys`  s	     qr8   r)  c                h    | j         }| j        #t          j        || j        f          }||         }|S rP   )r   r  r0   lexsort)rR   r   r  s      r6   rP  zBinGrouper.codes_infoe  s7     h<#Zdl 344Ff+C
r8   r   r+   c              #     K   fd}d}t          | j        | j        d          D ] \  }}|t          ur| |||          fV  |}!|t	                    k     r| j        d          ||d          fV  dS dS )r   c                "    j         | |         S rP   )r  )startedger   s     r6   <lambda>z)BinGrouper.get_iterator.<locals>.<lambda>w  s    TYuTz%: r8   r   Tr   r   N)r  r  r  r   r>   )rR   r   slicerr  r  labels    `    r6   r  zBinGrouper.get_iteratorn  s       ;::: !ty$.FFF 	 	KD%CVVE4000000EE3t99.$ffUD&9&9999999 r8   c                    t          j        t                    }d}t          | j        | j        d          D ]6\  }}||k     r+|t          ur t          t          ||                    ||<   |}7|S Nr   Tr   )collectionsdefaultdictr5  r  r  r  r   rH  )rR   r   r   r  bins        r6   r   zBinGrouper.indices  su    )$//dndiEEE 	 	JE33ww##%)%3--%8%8GENr8   r  c                    | j         gS rP   rO  r   s    r6   r  zBinGrouper.codes  s    zr8   c                2   | j         }t          | j                   dk    r$t          | j         d                   r
|dd          }t          |          }t          j        t          j        d| j        f                   }t          |          }|t          | j                  k    r(t          j        t          j	        |          |          }n9t          j        t          j        dt          j	        |          f         |          }t          |          }||fS )Nr   r;   r   )
r  r>   r   r0   diffr_r  r   r  r  )rR   r  r   repr   s        r6   rF  zBinGrouper.result_index_and_ids  s    ~t~!##T^A->(?(?#'+Ll##gbeAtyL)**!#&&c$)nn$$)BIg..44CC)BE"bi&8&8"893??C!#&&S  r8   r2  c                    | j         gS rP   )r  r   s    r6   r$  zBinGrouper.levels  s    r8   r6  c                    | j         j        gS rP   )r  rZ   r   s    r6   r9  zBinGrouper.names  s    #$$r8   r   c                    | j         }| j        }|                    |          }t          j        ||dd |j                  }|gS )NF)in_axisr  rZ  )r  r   r,  r   Groupingr:   )rR   levr  r   r  s        r6   r   zBinGrouper.groupings  sL    n%FEs{
 
 
 vr8   c                    | S rP   r   r   s    r6   r  zBinGrouper.observed_grouper  s    r8   rP   r,   r-   r  r  )r   r+   )r,   r  r  r  r  )r,   r  )r   r   r   r   r   rS   r   rE  r  r   rP  r  r   r  rF  r$  r9  r   r  r   r8   r6   r  r  $  s         <   	5 5 5 5 5 	 	 ^	    X    ^: : : :( 	 	 ^	    ^ ! ! ^!"       X  % % % X%    X    X  r8   r  rN   c                   t          | t                    r5t          |          dk    rdS | j                            |d                   S t          | t
                    r | j                            |d                   S dS )Nr;   Fr   )r/   r!   r>   r<  equalsr   )r4   r  s     r6   r  r    ss    #v )t99q==5yQ(((	C	#	# )yQ(((5r8   c                  >    e Zd Zdd
ZddZedd            ZddZdS )r  r   r   r   r   r
  r)  r	  r,   r-   c               >    || _         || _        || _        || _        d S rP   )r   r   _slabels	_sort_idx)rR   r   r   r
  r	  s        r6   rS   zDataSplitter.__init__  s$     	"!r8   r*   c              #     K   | j         dk    rd S t          j        | j        | j                   \  }}| j        }t          ||d          D ]+\  }}|                     |t          ||                    V  ,d S r  )r   r	   generate_slicesr  _sorted_datar  _chopslice)rR   startsendssdatar  ends         r6   r   zDataSplitter.__iter__  s      <1 F*4=$,GG!fd4888 	7 	7JE3**UE%$5$5666666	7 	7r8   c                D    | j                             | j        d          S )Nr   rr  )r   r,  r  r   s    r6   r  zDataSplitter._sorted_data  s    y~~dn1~555r8   	slice_objr	  r+   c                     t          |           rP   r   )rR   r  r  s      r6   r  zDataSplitter._chop  s    !$'''r8   N)
r   r   r   r   r
  r)  r	  r)  r,   r-   )r,   r*   )r,   r   )r  r	  r,   r+   )r   r   r   rS   r   r   r  r  r   r8   r6   r  r    sp        " " " "	7 	7 	7 	7 6 6 6 ^6( ( ( ( ( (r8   r  c                      e Zd ZddZdS )	r  r  r!   r  r	  r,   c                    |j                             |          }|                    ||j                  }|j        |_        |                    |d          S )Nr  groupbymethod)_mgr	get_slice_constructor_from_mgrr  rZ   _name__finalize__)rR   r  r  mgrsers        r6   r  zSeriesSplitter._chop  sS    j""9--))#CH)==J	i888r8   N)r  r!   r  r	  r,   r!   r   r   r   r  r   r8   r6   r  r    s(        9 9 9 9 9 9r8   r  c                      e Zd ZddZdS )	r  r  r   r  r	  r,   c                    |j                             |d          }|                    ||j                  }|                    |d          S )Nr;   rr  r  r  r  )r  r  r  r  r  )rR   r  r  r  dfs        r6   r  zFrameSplitter._chop  sN     j""91"55((38(<<uY777r8   N)r  r   r  r	  r,   r   r  r   r8   r6   r  r    s(        8 8 8 8 8 8r8   r  r  r  )Kr   
__future__r   r  r   typingr   r   r   r   numpyr0   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+   r7   r@   rB   r   r  r  r  r  r  r   r8   r6   <module>r6     sD    # " " " " "                               * ) ) ) ) ) ) ) )              . - - - - - 2 2 2 2 2 2                         
 + * * * * * ' ' ' ' ' ' ' ' ' ' ' '            & % % % % %               ,            ,+++++> > > >  O
 O
 O
 O
 O
 O
 O
 O
de e e e e e e ePT T T T T T T Tn   ( ( ( ( (78$ ( ( (D9 9 9 9 9\ 9 9 98 8 8 8 8L 8 8 8 8 8r8   