a
    DBìj¤–  ã                	   @  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	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/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; e�rXddl<m=Z=m>Z>m?Z? ddl@mAZA ddœdd„ZBdd„ ZCG dd„ dƒZDG dd„ dƒZEG dd„ deEƒZFdd d!œd"d#„ZGG d$d%„ d%e	e ƒZHG d&d'„ d'eHƒZIG d(d)„ d)eHƒZJdd*œd+d,d-d,d,dd%d.œd/d0„ZKdS )1a  
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Ú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ÚNone©Úreturnc                 C  s    t | tjƒr|tkrtdƒ‚d S )NzMust produce aggregated value)Ú
isinstanceÚnpÚndarrayÚobjectÚ
ValueError)ÚobjÚdtype© r6   úV/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/groupby/ops.pyÚcheck_result_arrayQ   s    r8   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   )Úhasattrr9   ÚndimÚlen)Úresr6   r6   r7   Úextract_result\   s
    
r?   c                   @  sÀ  e Zd ZU dZeg d¢ƒZdddddœdd„Zeje	j
d	d
�eje	j
dd
�ddeje	jdd�eje	j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 d!d"œd#œZd$ed%< d&d'iZeddd(œd)d*„ƒZeejddd+dd,œd-d.„ƒƒZd/d/d0œd1d2„Zd3d/d4d5œd6d7„Zd+d+d8œd9d:„Zd+d+d8œd;d<„Zed=d=d>d?œd/d3d3d/d@d@dAd/dBœdCdD„ƒZed>dEœd/d3d3d/d@d@dAd/dBœdFdG„ƒZedHdIddJœdKdL„ƒZedMdNœdIdHd3d/d3dIdOœdPdQ„ƒZd=S )RÚ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ÚstrÚboolr,   )ÚkindÚhowÚhas_dropped_nar.   c                 C  s   || _ || _|| _d S ©N©rJ   rK   rL   )ÚselfrJ   rK   rL   r6   r6   r7   Ú__init__~   s    zWrappedCythonOp.__init__rA   )Zval_testrB   Z	group_sumZ
group_prodrF   ©ÚnamerG   Z	group_minZ	group_maxZ
group_meanZgroup_median_float64Ú	group_varÚstdÚsemZ
group_skewZ	group_nthÚ
group_lastZ
group_ohlc)rA   rB   ÚsumÚprodrF   rG   ÚminÚmaxÚmeanÚmedianÚvarrT   rU   ÚskewÚfirstÚlastÚohlcZgroup_cumprodZgroup_cumsumZgroup_cumminZgroup_cummaxZ
group_rank)ÚcumprodÚcumsumZcumminZcummaxrC   )Ú	aggregateÚ	transformzdict[str, dict]Ú_CYTHON_FUNCTIONSra   é   )rK   r.   c                 C  s   || j d v rdS dS )Nrd   re   )rf   )ÚclsrK   r6   r6   r7   Úget_kind_from_how¢   s    z!WrappedCythonOp.get_kind_from_howznp.dtype)rJ   rK   r5   Ú
is_numericc                 C  sª   |j }| j| | }t|ƒr"|}n
tt|ƒ}|r4|S |t t¡krœ|dv rbtd|› d|› d�ƒ‚n6|dv rn|S |dkrxn d|j	vr˜td|› d|› d�ƒ‚|S td|ƒ‚d S )	N)r\   rb   z2function is not implemented for this dtype: [how->z,dtype->ú])rT   rU   rF   rG   r^   r2   zPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/)
rR   rf   ÚcallableÚgetattrÚ
libgroupbyr0   r5   r2   ÚNotImplementedErrorZ__signatures__)rh   rJ   rK   r5   rj   Z	dtype_strZftypeÚfr6   r6   r7   Ú_get_cython_functionª   sB    
ÿÿÿ
ÿÿÿýz$WrappedCythonOp._get_cython_functionz
np.ndarray)Úvaluesr.   c                 C  sp   | j }|dv rt|ƒ}nT|jjdv rl|dv s<| jdkrF| jrFt|ƒ}n&|dv rl|jjdkrdt|ƒ}nt|ƒ}|S )zÖ
        Cast numeric dtypes to float64 for functions that only support that.

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

        Returns
        -------
        values : np.ndarray
        )r\   rT   rU   r^   Úiu)r]   r[   re   )rW   ra   rX   rc   rb   Úi)rK   r   r5   rJ   rL   r   r   )rO   rr   rK   r6   r6   r7   Ú_get_cython_valsÕ   s    
ÿÿ

z WrappedCythonOp._get_cython_valsÚintr   )Úngroupsrr   r.   c                 C  sf   | j }| j}| j |d¡}|dkr,||f}n6|dkr>tdƒ‚n$|dkrN|j}n|f|jdd …  }|S )Nr:   ra   z<arity of more than 1 is not supported for the 'how' argumentre   )rK   rJ   Ú_cython_arityÚgetro   Úshape)rO   rw   rr   rK   rJ   ZarityÚ	out_shaper6   r6   r7   Ú_get_output_shapeú   s    
ÿz!WrappedCythonOp._get_output_shape)r5   r.   c                 C  sL   | j }|dkrd}n.|dv r"d}n |jdv r>|j› |j› �}nd}t |¡S )NrC   Úfloat64©rF   rG   ÚintpÚiufcbr2   )rK   rJ   Úitemsizer0   r5   )rO   r5   rK   Ú	out_dtyper6   r6   r7   Ú_get_out_dtype  s    
zWrappedCythonOp._get_out_dtypec                 C  sZ   | j }|dv r*|t t¡krVt tj¡S n,|dv rV|jdv r@|S |jdv rVt 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.
        )rW   rc   rW   rX   rb   )r[   r\   r]   rT   rU   ÚfcZiub)rK   r0   r5   rI   Úint64rJ   r}   )rO   r5   rK   r6   r6   r7   Ú_get_result_dtype  s    

z!WrappedCythonOp._get_result_dtypeNr   )ÚmaskÚresult_maskÚinitialznpt.NDArray[np.bool_] | Noner   )rr   Ú	min_countrw   Úcomp_idsr‡   rˆ   r‰   r.   c             	   K  s¦   |j dkr†|d d d …f }	|d ur2|d d d …f }|d urJ|d d d …f }| j|	f||||||dœ|¤Ž}
|
jd dkr€|
d S |
jS | j|f||||||dœ|¤ŽS )Nr:   )rŠ   rw   r‹   r‡   rˆ   r‰   r   )r<   Ú_call_cython_oprz   ÚT)rO   rr   rŠ   rw   r‹   r‡   rˆ   r‰   ÚkwargsZvalues2dr>   r6   r6   r7   Ú_cython_op_ndim_compat7  sB    
ÿùø
ÿùøz&WrappedCythonOp._cython_op_ndim_compat)r‰   c                K  s6  |}	|j }
|
jdv }|
jdv }|r2| d¡}d}n|
jdkrF| d¡}|j dkr\| tj¡}| jdv r¸|d u rvt|ƒ}|
tkrž|d	 rž| 	¡ rž| 
¡ }d||< |jtd
d� tj¡}d}|j}|d urÚ|j}|d urÚ|j}|  ||¡}|  | j| j|j |¡}|  |¡}|  |j ¡}ttj||d�ƒ}| jdk�rHtj|tjd�}| jdv �r‚| jdk�r`||d< |f ||||||||dœ|¤Ž nÄ| jdv �rÂ| jdv �r¢||d< |||||f|||dœ|¤Ž n„| jdv �rø|f |||||dœ|¤Ž |jtd
d�}nN| jdv �r6|f ||||||dœ|¤Ž |
tk�rF| t¡}nt| j› d�ƒ‚n0| jdk�r\||d< |f ||||||dœ|¤Ž | jdk�r| jdv�r|j jdv �r|�st| jdv �r¶d nd!|ƒ}||k }| 	¡ �r|d u�rî||  ¡ �sJ ‚n| d"¡}tj||< |j}| j| jv�r.|  |	j ¡}t||ƒ}n|}|S )#Nr€   ZmMr…   TÚbZuint8Zfloat16)rA   rB   ZskipnaF)Úcopy©r5   rd   )rF   rG   rY   rZ   r[   r`   r_   rW   rW   r‰   )ÚoutÚcountsrr   ÚlabelsrŠ   r‡   rˆ   Úis_datetimelike)rU   rT   r]   ra   rX   r\   )rT   rU   r–   )rŠ   r‡   rˆ   )r“   rr   r•   r‡   rˆ   )r^   )r“   r”   rr   r•   r‡   rˆ   z is not implementedrC   rˆ   )r“   rr   r•   rw   r–   r‡   r~   rs   )rW   rX   r   r:   r}   )r5   rJ   ÚviewZastyper0   Zfloat32rK   r   r2   rA   r‘   rI   Zint8r�   r|   rq   ru   rƒ   r   ÚemptyZzerosr…   ro   rZ   rB   ÚnanÚcast_blocklistr†   r   )rO   rr   rŠ   rw   r‹   r‡   rˆ   r‰   rŽ   Zorig_valuesr5   rj   r–   r{   Úfuncr‚   Úresultr”   ÚcutoffZempty_groupsZ	res_dtypeÚ	op_resultr6   r6   r7   rŒ   f  sæ    








ø	÷üùø
ûúúù	
úù




zWrappedCythonOp._call_cython_opr   r
   )Úaxisrr   r.   c                 C  sH   |j dkrtdƒ‚|j dkr.|dksDJ |ƒ‚nt|jƒsD|dksDJ ‚d S )Né   z.number of dimensions is currently limited to 2r:   r   )r<   ro   r   r5   )rO   rŸ   rr   r6   r6   r7   Ú_validate_axis	  s    


zWrappedCythonOp._validate_axiséÿÿÿÿ)rŠ   )rr   rŸ   rŠ   r‹   rw   r.   c                K  sT   |   ||¡ t|tjƒs8|jf | j| j|||dœ|¤ŽS | j|f|||ddœ|¤ŽS )zW
        Call our cython function, with appropriate pre- and post- processing.
        )rK   rL   rŠ   rw   ÚidsN)rŠ   rw   r‹   r‡   )r¡   r/   r0   r1   Z_groupby_oprK   rL   r�   )rO   rr   rŸ   rŠ   r‹   rw   rŽ   r6   r6   r7   Úcython_operation  s*    ûú	ÿûúz WrappedCythonOp.cython_operation)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	frozensetrš   rP   Ú	functoolsÚpartialrn   Zgroup_any_allZgroup_idxmin_idxmaxrS   rf   Ú__annotations__rx   Úclassmethodri   Úcacherq   ru   r|   rƒ   r†   r   r�   rŒ   r¡   r¤   r6   r6   r6   r7   r@   j   sl   
ÿïûì)%÷".
÷" #
ûr@   c                   @  st  e Zd ZU dZded< diddddddœd	d
„Zeddœdd„ƒZeddœdd„ƒZddœdd„Z	eddœdd„ƒZ
djddddœdd„Zedkddd dœd!d"„ƒZeed#d$„ ƒƒZed%dœd&d'„ƒZed(dœd)d*„ƒZeed+dœd,d-„ƒƒZed.dœd/d0„ƒZed1dœd2d3„ƒZed4dœd5d6„ƒZed7dœd8d9„ƒZeeddœd:d;„ƒƒZeeddœd<d=„ƒƒZed>dœd?d@„ƒZed(dœdAdB„ƒZedCdœdDdE„ƒZeeddœdFdG„ƒƒZedHdœdIdJ„ƒZeddœdKdL„ƒZedMdœdNdO„ƒZedldQdQdddRdSœdTdU„ƒZ edmd4dWddRdXœdYdZ„ƒZ!ed4dWd[d\œd]d^„ƒZ"edndWd_dd`daœdbdc„ƒZ#eed(dœddde„ƒƒZ$eed(dœdfdg„ƒƒZ%dhS )oÚ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Ÿ   TzSequence[grouper.Grouping]rI   r,   )rŸ   Ú	groupingsÚsortÚdropnar.   c                 C  s2   t |tƒsJ |ƒ‚|| _t|ƒ| _|| _|| _d S rM   )r/   r   rŸ   ÚlistÚ
_groupingsÚ_sortr²   )rO   rŸ   r°   r±   r²   r6   r6   r7   rP   K  s
    
zBaseGrouper.__init__úlist[grouper.Grouping]r-   c                 C  s   | j S rM   )r´   ©rO   r6   r6   r7   r°   Y  s    zBaseGrouper.groupingsr   c                 C  s   t dd„ | jD ƒƒS )Nc                 s  s   | ]}|j V  qd S rM   )rw   ©Ú.0Úpingr6   r6   r7   Ú	<genexpr>_  ó    z$BaseGrouper.shape.<locals>.<genexpr>)Útupler°   r·   r6   r6   r7   rz   ]  s    zBaseGrouper.shapezIterator[Hashable]c                 C  s
   t | jƒS rM   )ÚiterÚindicesr·   r6   r6   r7   Ú__iter__a  s    zBaseGrouper.__iter__rv   c                 C  s
   t | jƒS rM   )r=   r°   r·   r6   r6   r7   Únkeysd  s    zBaseGrouper.nkeysr   r   r   z#Iterator[tuple[Hashable, NDFrameT]])ÚdatarŸ   r.   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)rO   rÂ   rŸ   ÚsplitterÚkeysr6   r6   r7   Úget_iteratorh  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)rO   rÂ   rŸ   r£   Ú_rw   r6   r6   r7   rÅ   w  s    úzBaseGrouper._get_splitterc                 C  s:   t | jƒdkr| jd S | j\}}}t||| j| jƒS d S ©Nr:   r   )r=   r°   ÚlevelsrÎ   r$   Úcodes©rO   r£   rÑ   rw   r6   r6   r7   rÆ   ˆ  s    
zBaseGrouper.group_keys_seqz$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}r:   r   c                 S  s   g | ]
}|j ‘qS r6   ©rÔ   r¸   r6   r6   r7   Ú
<listcomp>™  r¼   z'BaseGrouper.indices.<locals>.<listcomp>c                 S  s   g | ]
}|j ‘qS r6   ©Ú_group_indexr¸   r6   r6   r7   r×   š  r¼   )r=   r°   r/   Úresult_indexr   r¿   r'   )rO   Z
codes_listrÉ   r6   r6   r7   r¿   “  s
    zBaseGrouper.indicesúnpt.NDArray[np.intp]c                 C  s|   t | j| j| jdd�}t|| jd�\}}| jrVt |dk¡}t |dk¡| }|| }t	|| j
ƒ}| jrx|t ||¡7 }|S )zR
        Get the original integer locations of result_index in the input.
        T©r±   Úxnull©r±   r   r¢   )r%   rÔ   rz   rµ   r"   rL   r0   Úwhererc   r&   rw   Útake)rO   Úgroup_indexrÑ   r‡   Z	null_gapsrœ   r6   r6   r7   Úresult_ilocs�  s    ÿzBaseGrouper.result_ilocsz#list[npt.NDArray[np.signedinteger]]c                 C  s   dd„ | j D ƒS )Nc                 S  s   g | ]
}|j ‘qS r6   rÖ   r¸   r6   r6   r7   r×   »  r¼   z%BaseGrouper.codes.<locals>.<listcomp>©r°   r·   r6   r6   r7   rÔ   ¸  s    zBaseGrouper.codesúlist[Index]c                 C  s   dd„ | j D ƒS )Nc                 S  s   g | ]
}|j ‘qS r6   rØ   r¸   r6   r6   r7   r×   ¿  r¼   z&BaseGrouper.levels.<locals>.<listcomp>rã   r·   r6   r6   r7   rÓ   ½  s    zBaseGrouper.levelsúlist[Hashable]c                 C  s   dd„ | j D ƒS )Nc                 S  s   g | ]
}|j ‘qS r6   rQ   r¸   r6   r6   r7   r×   Ã  r¼   z%BaseGrouper.names.<locals>.<listcomp>rã   r·   r6   r6   r7   ÚnamesÁ  s    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¢   )Z	minlengthr…   F)Úindexr5   r‘   )rÎ   r0   Zbincountr!   rÚ   )rO   r£   rÑ   rw   r“   r6   r6   r7   rE   Å  s
    zBaseGrouper.sizezdict[Hashable, np.ndarray]c                 C  sr   t | jƒdkr| jd jS g }| jD ]2}|j}t|tƒsD| |¡ q$| |jd j¡ q$t |¡}| j	 
|¡S dS )ú!dict {group name -> group labels}r:   r   N)r=   r°   ÚgroupsZgrouping_vectorr/   r¯   Úappendr   Zfrom_arraysrŸ   Úgroupby)rO   Z
to_groupbyrº   Úgvrç   r6   r6   r7   ré   Ò  s    


zBaseGrouper.groupsc                 C  s   t | jd ƒjS ©Nr   )r   rÎ   Zis_monotonic_increasingr·   r6   r6   r7   Ú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   )rI   rÎ   rA   r·   r6   r6   r7   rL   è  s    zBaseGrouper.has_dropped_naú6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]c                 C  s&   |   ¡ \}}t|ƒ}t|ƒ}|||fS rM   )Ú_get_compressed_codesr=   r   )rO   r‹   Úobs_group_idsrw   r6   r6   r7   rÎ   ð  s    zBaseGrouper.group_infoc                 C  s   | j \}}}|S rM   )rÎ   ©rO   r£   rÑ   r6   r6   r7   Ú
codes_infoù  s    zBaseGrouper.codes_infoz:tuple[npt.NDArray[np.signedinteger], npt.NDArray[np.intp]]c                 C  sV   t | jƒdkr0t| j| jddd�}t|| jd�S | jd }|jtjt |j	ƒtj
d�fS )Nr:   TrÜ   rÞ   r   r’   )r=   r°   r%   rÔ   rz   r"   rµ   r0   ÚarangerÙ   r   )rO   rá   rº   r6   r6   r7   rð   ÿ  s
    
z!BaseGrouper._get_compressed_codesc                 C  s
   t | jƒS rM   )r=   rÚ   r·   r6   r6   r7   rw     s    zBaseGrouper.ngroupszlist[npt.NDArray[np.intp]]c                 C  s&   | j }| j\}}}t||| j|dd�S )NT)rÝ   )rÔ   rÎ   r#   rz   )rO   rÔ   r£   Zobs_idsrÑ   r6   r6   r7   Ú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 )Nr:   r   c                 S  s   g | ]
}|j ‘qS r6   )Ú_result_indexr¸   r6   r6   r7   r×     r¼   z,BaseGrouper.result_index.<locals>.<listcomp>F)rÓ   rÔ   Zverify_integrityræ   )r=   r°   rö   Úrenameræ   rõ   r   )rO   rÔ   rÓ   r6   r6   r7   rÚ     s    
ÿzBaseGrouper.result_indexz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°   Z_group_arraylikerÇ   rõ   r   rà   rê   )rO   Ú	name_listrº   rÔ   rÓ   r6   r6   r7   Úget_group_levels"  s    zBaseGrouper.get_group_levelsr¢   rH   r
   )rJ   rK   rŸ   rŠ   r.   c                 K  sJ   |dv sJ ‚t ||| jd�}| j\}}	}	| j}
|jf |||||
dœ|¤ŽS )z;
        Returns the values of a cython operation.
        )re   rd   rN   )rr   rŸ   rŠ   r‹   rw   )r@   rL   rÎ   rw   r¤   )rO   rJ   rr   rK   rŸ   rŠ   rŽ   Zcy_opr£   rÑ   rw   r6   r6   r7   Ú_cython_operation5  s    ûúzBaseGrouper._cython_operationFr   )r4   r›   Úpreserve_dtyper.   c                 C  sJ   t |jtjƒsd}|  ||¡}tj|dd�}|rBt||jdd�}n|}|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)Z	try_float)Znumeric_only)	r/   r9   r0   r1   Ú_aggregate_series_pure_pythonr	   Zmaybe_convert_objectsr   r5   )rO   r4   r›   rû   rœ   Znpvaluesr“   r6   r6   r7   Ú
agg_seriesQ  s    zBaseGrouper.agg_seriesznpt.NDArray[np.object_])r4   r›   r.   c                 C  sn   | j \}}}tj|dd�}d}| j|dd�}t|ƒD ]4\}}	||	ƒ}
t|
ƒ}
|s`t|
|	jƒ d}|
||< q4|S )NÚOr’   Fr   rÄ   T)rÎ   r0   r˜   rÅ   Ú	enumerater?   r8   r5   )rO   r4   r›   rÑ   rw   rœ   ZinitializedrÈ   rt   Úgroupr>   r6   r6   r7   rü   r  s    
z)BaseGrouper._aggregate_series_pure_pythonzDataFrame | Seriesztuple[list, bool])rp   rÂ   rŸ   r.   c                 C  s¤   d}| j ||d�}| j}g }t||ƒ}|D ]B\}	}
t |
d|	¡ |
j}||
ƒ}|sbt|||ƒsbd}| |¡ q*t|ƒdkrœt	|dd ƒdv rœ||j
d d… ƒ ||fS )NFrÄ   rR   Tr   r¥   )r^   rW   rX   )rÅ   rÆ   rÇ   r2   Ú__setattr__ÚaxesÚ_is_indexed_likerê   r=   rm   Úiloc)rO   rp   rÂ   rŸ   ZmutatedrÈ   Z
group_keysZresult_valuesZzippedÚkeyr   Z
group_axesr>   r6   r6   r7   Úapply_groupwiseŠ  s    
zBaseGrouper.apply_groupwisec                 C  s   | j \}}}t||ƒS rM   )rÎ   r&   rÕ   r6   r6   r7   rÐ   ´  s    zBaseGrouper._sort_idxc                 C  s   | j \}}}| | j¡S rM   )rÎ   rà   rÐ   rò   r6   r6   r7   rÏ   »  s    zBaseGrouper._sorted_idsN)TT)r   )r   )r¢   )F)r   )&r¥   r¦   r§   r¨   r¬   rP   Úpropertyr°   rz   rÀ   rÁ   rÊ   r   rÅ   r   rÆ   r¿   râ   rÔ   rÓ   ræ   rE   ré   rî   rL   rÎ   ró   rð   rw   rõ   rÚ   rù   rú   rý   rü   r  rÐ   rÏ   r6   r6   r6   r7   r¯   9  s�   
  û ÿ		
 ú ÿ  ÿ)r¯   c                   @  sì   e Zd ZU dZded< ded< d,ddœd	d
„Zedd„ ƒZeddœdd„ƒZ	eddœdd„ƒZ
d-dddœdd„Zedd„ ƒZeddœdd„ƒZeddœdd „ƒZeddœd!d"„ƒZed#dœd$d%„ƒZed&dœd'd(„ƒZed)dœ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                 C  s6   t |ƒ| _t|ƒ| _|| _t| jƒt| jƒks2J ‚d S rM   )r   r	  r    r
  Úindexerr=   )rO   r	  r
  r  r6   r6   r7   rP   ä  s    

zBinGrouper.__init__c                 C  s   dd„ t | j| jƒD ƒ}|S )rè   c                 S  s   i | ]\}}|t ur||“qS r6   )r   )r¹   r  Úvaluer6   r6   r7   Ú
<dictcomp>÷  s   þz%BinGrouper.groups.<locals>.<dictcomp>)rÇ   r
  r	  )rO   rœ   r6   r6   r7   ré   ò  s    þzBinGrouper.groupsrv   c                 C  s   dS )Nr:   r6   r·   r6   r6   r7   rÁ   þ  s    zBinGrouper.nkeysrÛ   c                 C  s2   | j \}}}| jd ur.t || jf¡}|| }|S rM   )rÎ   r  r0   Zlexsort)rO   r£   rÑ   Zsorterr6   r6   r7   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rb||||ƒfV  |}qB||k r†| jd ||dƒfV  dS )rÃ   r   c                   s   ˆ j | |… S rM   ©r  ©ÚstartÚedge©rÂ   r6   r7   Ú<lambda>  r¼   z)BinGrouper.get_iterator.<locals>.<lambda>c                   s   ˆ j d d …| |…f S rM   r  r  r  r6   r7   r    r¼   r¢   N)r=   r  rÇ   r	  r
  r   )rO   rÂ   rŸ   ZslicerÚlengthr  r  Úlabelr6   r  r7   rÊ     s    	zBinGrouper.get_iteratorc                 C  sP   t  t¡}d}t| j| jƒD ].\}}||k r|turFtt||ƒƒ||< |}q|S rí   )ÚcollectionsÚdefaultdictr³   rÇ   r
  r	  r   Úrange)rO   r¿   rt   r  Úbinr6   r6   r7   r¿   %  s    
zBinGrouper.indicesrï   c                 C  s€   | j }tj|tjd�}t tjd| jf ¡}t|ƒ}|t| jƒkrVt 	t |¡|¡}nt 	tjdt |¡f |¡}t|ƒ||fS )Nr’   r   r¢   )
rw   r0   rô   r   ÚdiffÚr_r	  r   r=   Úrepeat)rO   rw   rñ   Úrepr‹   r6   r6   r7   rÎ   1  s    ýzBinGrouper.group_infozlist[np.ndarray]c                 C  s2   t jdt  | jdd … | jd d… k¡d f gS )Nr   r:   r¢   )r0   r  Zflatnonzeror	  r·   r6   r6   r7   rõ   C  s    zBinGrouper.reconstructed_codesc                 C  s0   t | jƒdkr*t| jd ƒr*| jdd … S | jS )Nr   r:   )r=   r
  r   r·   r6   r6   r7   rÚ   H  s    zBinGrouper.result_indexrä   c                 C  s   | j gS rM   )r
  r·   r6   r6   r7   rÓ   O  s    zBinGrouper.levelsrå   c                 C  s
   | j jgS rM   )r
  rR   r·   r6   r6   r7   ræ   S  s    zBinGrouper.namesr¶   c                 C  s6   | j }| jd }| |¡}tj||dd |jd�}|gS )Nr   F)Zin_axisÚlevelZuniques)r
  rÎ   rà   r   ZGroupingr9   )rO   ZlevrÔ   r•   rº   r6   r6   r7   r°   W  s    

ÿzBinGrouper.groupings)N)r   )r¥   r¦   r§   r¨   r¬   rP   r   ré   r  rÁ   ró   rÊ   r¿   rÎ   rõ   rÚ   rÓ   ræ   r°   r6   r6   r6   r7   r  Â  s4   
 ü

r  r   rI   )rŸ   r.   c                 C  sP   t | tƒr.t|ƒdkrdS | j|  || ¡S t | tƒrL| j|  || ¡S dS )Nr:   F)r/   r!   r=   r  Úequalsr   )r4   r  rŸ   r6   r6   r7   r  b  s    

r  c                	   @  s\   e Zd Zddœddddddddœd	d
„Zddœdd„Zeddœdd„ƒZdddœdd„ZdS )rË   r   rÄ   r   rÛ   rv   r   r,   ©rÂ   r•   rw   rÍ   rÌ   rŸ   r.   c                C  s>   || _ t|ƒ| _|| _|| _|| _|| _t|tƒs:J |ƒ‚d S rM   )	rÂ   r   r•   rw   Ú_slabelsrÐ   rŸ   r/   rv   )rO   rÂ   r•   rw   rÍ   rÌ   rŸ   r6   r6   r7   rP   r  s    

zDataSplitter.__init__r)   r-   c                 c  sT   | j }| jdkrd S t | j| j¡\}}t||ƒD ]\}}|  |t||ƒ¡V  q2d S rí   )Ú_sorted_datarw   r	   Zgenerate_slicesr!  rÇ   Ú_chopÚslice)rO   ÚsdataZstartsZendsr  Úendr6   r6   r7   rÀ   †  s    
zDataSplitter.__iter__c                 C  s   | j j| j| jd�S )NrÄ   )rÂ   rà   rÐ   rŸ   r·   r6   r6   r7   r"  “  s    zDataSplitter._sorted_datar$  r+   )Ú	slice_objr.   c                 C  s   t | ƒ‚d S rM   r   )rO   r%  r'  r6   r6   r7   r#  —  s    zDataSplitter._chopN)r¥   r¦   r§   rP   rÀ   r   r"  r#  r6   r6   r6   r7   rË   q  s   	ørË   c                   @  s   e Zd Zddddœdd„ZdS )ÚSeriesSplitterr!   r$  ©r%  r'  r.   c                 C  s2   |j  |¡}|j||jd�}|j|_|j|dd�S )N©r  rë   ©Úmethod)Ú_mgrÚ	get_sliceÚ_constructor_from_mgrr  rR   Ú_nameÚ__finalize__)rO   r%  r'  ÚmgrZserr6   r6   r7   r#  œ  s    zSeriesSplitter._chopN©r¥   r¦   r§   r#  r6   r6   r6   r7   r(  ›  s   r(  c                   @  s   e Zd Zddddœdd„ZdS )ÚFrameSplitterr   r$  r)  c                 C  s4   |j j|d| j d�}|j||jd�}|j|dd�S )Nr:   rÄ   r*  rë   r+  )r-  r.  rŸ   r/  r  r1  )rO   r%  r'  r2  Zdfr6   r6   r7   r#  ¥  s    zFrameSplitter._chopNr3  r6   r6   r6   r7   r4  ¤  s   r4  rÄ   r+   rÛ   rv   r   c                C  s(   t | tƒrt}nt}|| |||||d�S )N)rÍ   rÌ   rŸ   )r/   r!   r(  r4  )rÂ   r•   rw   rÍ   rÌ   rŸ   Úklassr6   r6   r7   rÅ   °  s    	
ÿrÅ   )Lr¨   Ú
__future__r   r  rª   Útypingr   r   r   r   r   Únumpyr0   Zpandas._libsr   r	   Zpandas._libs.groupbyZ_libsrë   rn   Zpandas._typingr
   r   r   r   r   Zpandas.errorsr   Zpandas.util._decoratorsr   Zpandas.core.dtypes.castr   r   Zpandas.core.dtypes.commonr   r   r   r   r   Zpandas.core.dtypes.missingr   r   Zpandas.core.framer   Zpandas.core.groupbyr   Zpandas.core.indexes.apir   r   r   r    Zpandas.core.seriesr!   Zpandas.core.sortingr"   r#   r$   r%   r&   r'   Úcollections.abcr(   r)   r*   Zpandas.core.genericr+   r8   r?   r@   r¯   r  r  rË   r(  r4  rÅ   r6   r6   r6   r7   Ú<module>   sN    	   R    !*	ù