o
    ŀgZ                     @  s  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  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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.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: erddl;m<Z<m=Z=m>Z> ddl?m@Z@ d5ddZAdd ZBG dd dZCG dd dZDG dd deDZEd6d"d#ZFG d$d% d%ee ZGG d&d' d'eGZHG d(d) d)eGZIdd*d7d3d4ZJdS )8a  
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CallableGenericfinal)NaTlib)	ArrayLikeAxisIntNDFrameTShapenptAbstractMethodError)cache_readonly)maybe_cast_pointwise_resultmaybe_downcast_to_dtype)ensure_float64ensure_int64ensure_platform_intensure_uint64is_1d_only_ea_dtype)isna
maybe_fill)	DataFrame)grouper)CategoricalIndexIndex
MultiIndexensure_index)Series)compress_group_indexdecons_obs_group_idsget_flattened_listget_group_indexget_group_index_sorterget_indexer_dict)HashableIteratorSequence)NDFramereturnNonec                 C  s$   t | tjr|tkrtdd S d S )NzMust produce aggregated value)
isinstancenpndarrayobject
ValueError)objdtype r4   K/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/groupby/ops.pycheck_result_arrayP   s
   r6   c                 C  s2   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   )hasattrr7   ndimlen)resr4   r4   r5   extract_result[   s
   
r=   c                   @  s  e Zd ZU dZeg dZd\d
dZi deje	j
dddeje	j
dddddddeje	jdddeje	jddddddddddddd eje	jd dd!eje	jd!dd"d#d$d%d&d'd(d)d*d+d,d-d.d/d0Zd1ed2< d(d3iZed]d4d5Zeejd^d9d:Zd_d=d>Zd`dBdCZdadDdEZdadFdGZedHdHdIdbdOdPZedbdQdRZedcdVdWZedXdYdddZd[ZdHS )e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                 C  s   || _ || _|| _d S NrF   rH   rI   )selfrF   rH   rI   r4   r4   r5   __init__}   s   
zWrappedCythonOp.__init__r?   )val_testr@   sum	group_sumprod
group_prodrD   namerE   min	group_minmax	group_maxmean
group_meanmediangroup_median_float64var	group_varstdsemskew
group_skewfirst	group_nthlast
group_lastohlc
group_ohlcgroup_cumprodgroup_cumsumgroup_cummingroup_cummax
group_rank)cumprodcumsumcummincummaxrA   )	aggregate	transformzdict[str, dict]_CYTHON_FUNCTIONS   c                 C  s   || j d v r	dS dS )Nrs   rt   )ru   )clsrH   r4   r4   r5   get_kind_from_how   s   z!WrappedCythonOp.get_kind_from_howr3   np.dtype
is_numericc                 C  s   |j }| j| | }t|r|}ntt|}|r|S |ttkrO|dv r0td| d| d|dv r6|S |dkr=	 |S d|j	vrMtd| d| d|S td|)	N)r\   ro   z2function is not implemented for this dtype: [how->z,dtype->])r`   ra   rD   rE   rb   r0   zPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/)
rU   ru   callablegetattr
libgroupbyr.   r3   r0   NotImplementedError__signatures__)rw   rF   rH   r3   rz   	dtype_strftypefr4   r4   r5   _get_cython_function   sD   

z$WrappedCythonOp._get_cython_functionvalues
np.ndarrayc                 C  sv   | j }|dv rt|}|S |jjdv r9|dv s| jdkr%| jr%t|}|S |dv r9|jjdkr5t|}|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   iu)r^   rZ   rt   )rP   rh   rR   rp   ro   i)rH   r   r3   rF   rI   r   r   )rM   r   rH   r4   r4   r5   _get_cython_vals   s   	z WrappedCythonOp._get_cython_valsngroupsintr   c                 C  sh   | j }| j}| j|d}|dkr||f}|S |dkrtd|dkr(|j}|S |f|jdd   }|S )Nr8   rh   z<arity of more than 1 is not supported for the 'how' argumentrt   )rH   rF   _cython_aritygetr   shape)rM   r   r   rH   rF   arity	out_shaper4   r4   r5   _get_output_shape   s   	z!WrappedCythonOp._get_output_shapec                 C  sL   | j }|dkr
d}n|dv rd}n|jdv r|j |j }nd}t|S )NrA   float64rD   rE   intpiufcbr0   )rH   rF   itemsizer.   r3   )rM   r3   rH   	out_dtyper4   r4   r5   _get_out_dtype  s   

zWrappedCythonOp._get_out_dtypec                 C  s\   | j }|dv r|ttkrttjS |S |dv r,|jdv r!|S |jdv r,t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.
        )rP   rp   rP   rR   ro   )rZ   r\   r^   r`   ra   fciub)rH   r.   r3   rJ   int64rF   r   )rM   r3   rH   r4   r4   r5   _get_result_dtype  s   

z!WrappedCythonOp._get_result_dtypeN)maskresult_mask	min_countcomp_idsr   npt.NDArray[np.bool_] | Noner   c          
      K  s   |j dkrB|d d d f }|d ur|d d d f }|d ur%|d d d f }| j|f|||||d|}	|	jd dkr?|	d S |	jS | j|f|||||d|S )Nr8   )r   r   r   r   r   r   )r:   _call_cython_opr   T)
rM   r   r   r   r   r   r   kwargsvalues2dr<   r4   r4   r5   _cython_op_ndim_compat6  s>   
	z&WrappedCythonOp._cython_op_ndim_compatc                K  s  |}|j }	|	jdv }
|	jdv }|r|d}d}
n
|	jdkr#|d}|j dkr.|tj}| jdv r\|d u r;t|}|	tkrO|d	 rO|	 rO|
 }d||< |jtd
dtj}d}
|j}|d urm|j}|d urm|j}| ||}| | j| j|j |
}| |}| |j }ttj||d}| jdkrtj|tjd}| jdv r|d!||||||||d| nw| jdv r| jdv r||d< |||||f|||d| nY| jdv r|d!|||||d| |jtd
d}n?| jdv r|d!||||||d| |	tkr|t}n t| j d| jdkr||d< |d!||||||d| | jdkrr| jdvrr|j jdv rr|srt| jdv rLdnd|}||k }|	 rr|d urh||  sgJ n
|d }tj||< |j}| j| jvr| |j }t||}|S |}|S )"Nr   mMr   Tbuint8float16)r?   r@   skipnaF)copyr3   rs   )rD   rE   rV   rX   rZ   rf   rd   rP   )outcountsr   labelsr   r   r   is_datetimelike)ra   r`   r^   rh   rR   r\   )r`   ra   r   )r   r   r   )r   r   r   r   r   )rb   )r   r   r   r   r   r   z is not implementedrA   r   )r   r   r   r   r   r   r   r   )rP   rR   r   r8   r   r4   )r3   rF   viewastyper.   float32rH   r   r0   r?   r   rJ   int8r   r   r   r   r   r   emptyzerosr   r   rX   r@   nancast_blocklistr   r   )rM   r   r   r   r   r   r   r   orig_valuesr3   rz   r   r   funcr   resultr   cutoffempty_groups	res_dtype	op_resultr4   r4   r5   r   b  s   









	




	






zWrappedCythonOp._call_cython_opaxisr
   r	   c                 C  sN   |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 2r8   r   )r:   r   r   r3   )rM   r   r   r4   r4   r5   _validate_axis   s   


zWrappedCythonOp._validate_axis)r   c                K  sT   |  || 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.
        )rH   rI   r   r   idsN)r   r   r   r   r4   )r   r-   r.   r/   _groupby_oprH   rI   r   )rM   r   r   r   r   r   r   r4   r4   r5   cython_operation  s*   	z WrappedCythonOp.cython_operation)rF   rG   rH   rG   rI   rJ   r+   r,   )rH   rG   r+   rG   )rF   rG   rH   rG   r3   ry   rz   rJ   )r   r   r+   r   )r   r   r   r   r+   r   )r3   ry   r+   ry   )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   rN   	functoolspartialr~   group_any_allgroup_idxmin_idxmaxr_   ru   __annotations__r   classmethodrx   cacher   r   r   r   r   r   r   r   r   r   r4   r4   r4   r5   r>   i   s   
 
	

)
%

+ 
r>   c                   @  s  e Zd ZU dZded< 		dndoddZedpddZedqddZdrddZ	edsddZ
	dtdudd Zedtdvd#d$Zeed%d& Zedwd(d)Zedxd+d,Zeedyd.d/Zedzd1d2Zed{d4d5Zed|d7d8Zed}d:d;Zeed~d<d=Zeed~d>d?ZeddAdBZedxdCdDZeddFdGZeedsdHdIZeddKdLZeddMdNZeddPdQZe	RdddXdYZ e	Zddd_d`Z!eddbdcZ"e	dtddgdhZ#eedxdidjZ$eedxdkdlZ%dmS )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Sequence[grouper.Grouping]sortrJ   dropnar+   r,   c                 C  s2   t |ts	J ||| _t|| _|| _|| _d S rK   )r-   r   r   list
_groupings_sortr   )rM   r   r   r   r   r4   r4   r5   rN   B  s
   

zBaseGrouper.__init__list[grouper.Grouping]c                 C  s   | j S rK   )r   rM   r4   r4   r5   r   P  s   zBaseGrouper.groupingsr   c                 C  s   t dd | jD S )Nc                 s  s    | ]}|j V  qd S rK   )r   .0pingr4   r4   r5   	<genexpr>V  s    z$BaseGrouper.shape.<locals>.<genexpr>)tupler   r   r4   r4   r5   r   T     zBaseGrouper.shapeIterator[Hashable]c                 C  
   t | jS rK   )iterindicesr   r4   r4   r5   __iter__X  s   
zBaseGrouper.__iter__r   c                 C  r   rK   )r;   r   r   r4   r4   r5   nkeys[     
zBaseGrouper.nkeysr   datar   r
   #Iterator[tuple[Hashable, NDFrameT]]c                 c  s*    | j ||d}| j}t||E dH  dS )
        Groupby iterator

        Returns
        -------
        Generator yielding sequence of (name, subsetted object)
        for each group
        r   N)_get_splittergroup_keys_seqzip)rM   r   r   splitterkeysr4   r4   r5   get_iterator_  s   zBaseGrouper.get_iteratorr*   DataSplitterc                 C  s$   | j \}}}t|||| j| j|dS )zV
        Returns
        -------
        Generator yielding subsetted objects
        )
sorted_idssort_idxr   )
group_infor   _sorted_ids	_sort_idx)rM   r   r   r   _r   r4   r4   r5   r   n  s   zBaseGrouper._get_splitterc                 C  s6   t | jdkr| jd S | j\}}}t||| j| jS Nr8   r   )r;   r   levelsr   r#   codesrM   r   r   r   r4   r4   r5   r     s   
zBaseGrouper.group_keys_seq$dict[Hashable, npt.NDArray[np.intp]]c                 C  sP   t | jdkrt| jtr| jd jS dd | jD }dd | jD }t||S )z"dict {group name -> group indices}r8   r   c                 S     g | ]}|j qS r4   r   r   r4   r4   r5   
<listcomp>      z'BaseGrouper.indices.<locals>.<listcomp>c                 S  r  r4   _group_indexr   r4   r4   r5   r    r  )r;   r   r-   result_indexr   r   r&   )rM   
codes_listr   r4   r4   r5   r     s
   
zBaseGrouper.indicesnpt.NDArray[np.intp]c                 C  s|   t | j| j| jdd}t|| jd\}}| jr+t|dk}t|dk| }|| }t	|| j
}| jr<|t||7 }|S )zR
        Get the original integer locations of result_index in the input.
        Tr   xnullr   r   r   )r$   r   r   r   r!   rI   r.   whererp   r%   r   take)rM   group_indexr   r   	null_gapsr   r4   r4   r5   result_ilocs  s   zBaseGrouper.result_ilocs#list[npt.NDArray[np.signedinteger]]c                 C     dd | j D S )Nc                 S  r  r4   r  r   r4   r4   r5   r    r  z%BaseGrouper.codes.<locals>.<listcomp>r   r   r4   r4   r5   r     s   zBaseGrouper.codeslist[Index]c                 C  r  )Nc                 S  r  r4   r  r   r4   r4   r5   r    r  z&BaseGrouper.levels.<locals>.<listcomp>r  r   r4   r4   r5   r        zBaseGrouper.levelslist[Hashable]c                 C  r  )Nc                 S  r  r4   rT   r   r4   r4   r5   r    r  z%BaseGrouper.names.<locals>.<listcomp>r  r   r4   r4   r5   names  r  zBaseGrouper.namesr    c                 C  s>   | j \}}}|rtj||dk |d}ng }t|| jdddS )z&
        Compute group sizes.
        r   )	minlengthr   F)indexr3   r   )r   r.   bincountr    r  )rM   r   r   r   r   r4   r4   r5   rC     s
   zBaseGrouper.sizedict[Hashable, np.ndarray]c                 C  sn   t | jdkr| jd jS g }| jD ]}|j}t|ts"|| q||jd j qt|}| j	
|S )!dict {group name -> group labels}r8   r   )r;   r   groupsgrouping_vectorr-   r   appendr   from_arraysr   groupby)rM   
to_groupbyr   gvr  r4   r4   r5   r    s   


zBaseGrouper.groupsc                 C  s   t | jd jS Nr   )r   r   is_monotonic_increasingr   r4   r4   r5   is_monotonic  s   zBaseGrouper.is_monotonicc                 C  s   t | jd dk  S )zE
        Whether grouper has null value(s) that are dropped.
        r   )rJ   r   r?   r   r4   r4   r5   rI     s   zBaseGrouper.has_dropped_na6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]c                 C  s&   |   \}}t|}t|}|||fS rK   )_get_compressed_codesr;   r   )rM   r   obs_group_idsr   r4   r4   r5   r     s   
zBaseGrouper.group_infoc                 C  s   | j \}}}|S rK   )r   rM   r   r   r4   r4   r5   
codes_info  s   zBaseGrouper.codes_info:tuple[npt.NDArray[np.signedinteger], npt.NDArray[np.intp]]c                 C  sV   t | jdkrt| j| jddd}t|| jdS | jd }|jtjt |j	tj
dfS )Nr8   Tr
  r  r   r   )r;   r   r$   r   r   r!   r   r.   aranger  r   )rM   r  r   r4   r4   r5   r)    s
   
z!BaseGrouper._get_compressed_codesc                 C  r   rK   )r;   r  r   r4   r4   r5   r     s   
zBaseGrouper.ngroupslist[npt.NDArray[np.intp]]c                 C  s&   | j }| j\}}}t||| j|ddS )NT)r  )r   r   r"   r   )rM   r   r   obs_idsr   r4   r4   r5   reconstructed_codes  s   zBaseGrouper.reconstructed_codesc                 C  sN   t | jdkr| jd j| jd S | j}dd | jD }t||d| jdS )Nr8   r   c                 S  r  r4   )_result_indexr   r4   r4   r5   r    r  z,BaseGrouper.result_index.<locals>.<listcomp>F)r   r   verify_integrityr  )r;   r   r2  renamer  r1  r   )rM   r   r   r4   r4   r5   r    s   
zBaseGrouper.result_indexlist[ArrayLike]c                 C  sZ   t | jdkr| jd jgS g }t| j| jD ]\}}t|}|j|}|| q|S r   )r;   r   _group_arrayliker   r1  r   r  r   )rM   	name_listr   r   r   r4   r4   r5   get_group_levels  s   zBaseGrouper.get_group_levelsr   rF   rG   rH   r   r	   c                 K  sJ   |dv sJ t ||| jd}| j\}}	}	| j}
|jd|||||
d|S )z;
        Returns the values of a cython operation.
        )rt   rs   rL   )r   r   r   r   r   Nr4   )r>   rI   r   r   r   )rM   rF   r   rH   r   r   r   cy_opr   r   r   r4   r4   r5   _cython_operation,  s   zBaseGrouper._cython_operationFr2   r   r   preserve_dtypec                 C  sL   t |jtjs	d}| ||}tj|dd}|r"t||jdd}|S |}|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
        TF)	try_float)numeric_only)	r-   r7   r.   r/   _aggregate_series_pure_pythonr   maybe_convert_objectsr   r3   )rM   r2   r   r;  r   npvaluesr   r4   r4   r5   
agg_seriesH  s   zBaseGrouper.agg_seriesnpt.NDArray[np.object_]c                 C  sn   | j \}}}tj|dd}d}| j|dd}t|D ]\}}	||	}
t|
}
|s0t|
|	j d}|
||< q|S )NOr   Fr   r   T)r   r.   r   r   	enumerater=   r6   r3   )rM   r2   r   r   r   r   initializedr   r   groupr<   r4   r4   r5   r>  i  s   
z)BaseGrouper._aggregate_series_pure_pythonr   DataFrame | Seriestuple[list, bool]c                 C  s   d}| j ||d}| j}g }t||}|D ]!\}	}
t|
d|	 |
j}||
}|s1t|||s1d}|| qt|dkrNt	|dd dv rN||j
d d  ||fS )NFr   rU   Tr   r   )rb   rP   rR   )r   r   r   r0   __setattr__axes_is_indexed_liker   r;   r}   iloc)rM   r   r   r   mutatedr   
group_keysresult_valueszippedkeyrF  
group_axesr<   r4   r4   r5   apply_groupwise  s   
zBaseGrouper.apply_groupwisec                 C  s   | j \}}}t||S rK   )r   r%   r   r4   r4   r5   r     s   
zBaseGrouper._sort_idxc                 C  s   | j \}}}|| jS rK   )r   r  r   r+  r4   r4   r5   r     s   zBaseGrouper._sorted_idsN)TT)
r   r   r   r   r   rJ   r   rJ   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  )r+   rJ   r+   r(  )r+   r-  )r+   r/  r+   r   )r+   r5  )r   )
rF   rG   rH   rG   r   r
   r   r   r+   r	   )F)r2   r    r   r   r;  rJ   r+   r	   )r2   r    r   r   r+   rB  )r   r   r   rG  r   r
   r+   rH  )&r   r   r   r   r   rN   propertyr   r   r   r   r   r   r   r   r   r   r  r   r   r  rC   r  r'  rI   r   r,  r)  r   r1  r  r8  r:  rA  r>  rS  r   r   r4   r4   r4   r5   r   0  s   
 
		
 )r   c                   @  s   e Zd ZU dZded< ded< 	d-d.d	d
Zedd Zed/ddZ	ed0ddZ
d1d2ddZedd Zed3ddZed4d d!Zed5d"d#Zed6d%d&Zed7d(d)Zed8d+d,ZdS )9
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                 C  s6   t || _t|| _|| _t| jt| jksJ d S rK   )r   r^  r   r_  indexerr;   )rM   r^  r_  r`  r4   r4   r5   rN     s   

zBinGrouper.__init__c                 C  s   dd t | j| jD }|S )r  c                 S  s   i | ]\}}|t ur||qS r4   )r   )r   rQ  valuer4   r4   r5   
<dictcomp>  s
    z%BinGrouper.groups.<locals>.<dictcomp>)r   r_  r^  )rM   r   r4   r4   r5   r    s   zBinGrouper.groupsr   c                 C  s   dS )Nr8   r4   r   r4   r4   r5   r     s   zBinGrouper.nkeysr	  c                 C  s2   | j \}}}| jd urt|| jf}|| }|S rK   )r   r`  r.   lexsort)rM   r   r   sorterr4   r4   r5   r,    s
   
zBinGrouper.codes_infor   r   r*   r   r
   c                 #  s    |dkr fdd}n fdd}t  j| }d}t| j| jD ]\}}|tur2||||fV  |}q"||k rF| jd ||dfV  dS dS )r   r   c                   s    j | | S rK   rL  startedger   r4   r5   <lambda>  s    z)BinGrouper.get_iterator.<locals>.<lambda>c                   s    j d d | |f S rK   re  rf  ri  r4   r5   rj    s    r   N)r;   rJ  r   r^  r_  r   )rM   r   r   slicerlengthrg  rh  labelr4   ri  r5   r     s   	zBinGrouper.get_iteratorc                 C  sP   t t}d}t| j| jD ]\}}||k r%|tur#tt||||< |}q|S r%  )collectionsdefaultdictr   r   r_  r^  r   range)rM   r   r   rm  binr4   r4   r5   r     s   
zBinGrouper.indicesr(  c                 C  s   | j }tj|tjd}ttjd| jf }t|}|t| jkr+t	t||}nt	tjdt|f |}t|||fS )Nr   r   r   )
r   r.   r.  r   diffr_r^  r   r;   repeat)rM   r   r*  repr   r4   r4   r5   r   (  s   zBinGrouper.group_infolist[np.ndarray]c                 C  s2   t jdt | jdd  | jd d kd f gS )Nr   r8   r   )r.   rs  flatnonzeror^  r   r4   r4   r5   r1  :  s   2zBinGrouper.reconstructed_codesc                 C  s0   t | jdkrt| jd r| jdd  S | jS )Nr   r8   )r;   r_  r   r   r4   r4   r5   r  ?  s   zBinGrouper.result_indexr  c                 C  s   | j gS rK   )r_  r   r4   r4   r5   r   F  s   zBinGrouper.levelsr  c                 C  s
   | j jgS rK   )r_  rU   r   r4   r4   r5   r  J  r   zBinGrouper.namesr   c                 C  s6   | j }| jd }||}tj||dd |jd}|gS )Nr   F)in_axisleveluniques)r_  r   r  r   Groupingr7   )rM   levr   r   r   r4   r4   r5   r   N  s   

zBinGrouper.groupingsrK   r+   r,   rU  rW  rV  )r   r*   r   r
   rZ  )r+   rv  r[  rX  rY  rT  )r   r   r   r   r   rN   r   r  r\  r   r,  r   r   r   r1  r  r   r  r   r4   r4   r4   r5   r]    s6   
 

r]  r   r
   rJ   c                 C  sP   t | trt|dkrdS | j| || S t | tr&| j| || S dS )Nr8   F)r-   r    r;   rJ  equalsr   )r2   rJ  r   r4   r4   r5   rK  Y  s   

rK  c                   @  s>   e Zd ZdddddZdddZedddZdddZdS ) r   r   r   r   r   r   r	  r   r   r   r   r   r
   r+   r,   c                C  s>   || _ t|| _|| _|| _|| _|| _t|tsJ |d S rK   )	r   r   r   r   _slabelsr   r   r-   r   )rM   r   r   r   r   r   r   r4   r4   r5   rN   i  s   

zDataSplitter.__init__r(   c                 c  sV    | j }| jdkrd S t| j| j\}}t||D ]\}}| |t||V  qd S r%  )_sorted_datar   r   generate_slicesr  r   _chopslice)rM   sdatastartsendsrg  endr4   r4   r5   r   }  s   
zDataSplitter.__iter__c                 C  s   | j j| j| jdS )Nr   )r   r  r   r   r   r4   r4   r5   r    r   zDataSplitter._sorted_data	slice_objr  r*   c                 C  s   t | rK   r   )rM   r  r  r4   r4   r5   r    s   zDataSplitter._chopN)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   rN   r   r   r  r  r4   r4   r4   r5   r   h  s    	
r   c                   @     e Zd Zd	ddZdS )
SeriesSplitterr  r    r  r  r+   c                 C  s2   |j |}|j||jd}|j|_|j|ddS )NrJ  r"  method)_mgr	get_slice_constructor_from_mgrrJ  rU   _name__finalize__)rM   r  r  mgrserr4   r4   r5   r    s   zSeriesSplitter._chopN)r  r    r  r  r+   r    r   r   r   r  r4   r4   r4   r5   r        r  c                   @  r  )
FrameSplitterr  r   r  r  r+   c                 C  s4   |j j|d| j d}|j||jd}|j|ddS )Nr8   r   r  r"  r  )r  r  r   r  rJ  r  )rM   r  r  r  dfr4   r4   r5   r    s   zFrameSplitter._chopN)r  r   r  r  r+   r   r  r4   r4   r4   r5   r    r  r  r   r   r*   r   r	  r   r   r   r   c                C  s(   t | trt}nt}|| |||||dS )N)r   r   r   )r-   r    r  r  )r   r   r   r   r   r   klassr4   r4   r5   r     s   
	r   r}  )r   r
   r+   rJ   )r   r*   r   r	  r   r   r   r	  r   r	  r   r
   r+   r   )Kr   
__future__r   rn  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   r   pandas.core.dtypes.commonr   r   r   r   r   pandas.core.dtypes.missingr   r   pandas.core.framer   pandas.core.groupbyr   pandas.core.indexes.apir   r   r   r   pandas.core.seriesr    pandas.core.sortingr!   r"   r#   r$   r%   r&   collections.abcr'   r(   r)   pandas.core.genericr*   r6   r=   r>   r   r]  rK  r   r  r  r   r4   r4   r4   r5   <module>   sP     	
   J    
!*	