o
    ôT·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r«ddl<m=Z=m>Z>m?Z? ddl@mAZA d5dd„ZBdd„ ZCG dd„ dƒZDG dd„ dƒZEG dd„ deEƒZFd6d"d#„ZGG d$d%„ d%e	e ƒZHG d&d'„ d'eHƒZIG d(d)„ d)eHƒZJdd*œd7d3d4„ZKd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Ú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Ú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© r5   úZ/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/pandas/core/groupby/ops.pyÚcheck_result_arrayQ   s
   ür7   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   )Úhasattrr8   ÚndimÚlen)Úresr5   r5   r6   Ú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dad4d5„ƒZeejdbd9d:„ƒƒZdcd=d>„ZdddBdC„ZdedDdE„ZdedFdG„ZedHdHdIdJœdfdRdS„ƒZedIdTœdfdUdV„ƒZedgdZd[„ƒZed\d]œdhd^d_„ƒZdHS )iÚ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©rG   rI   rJ   )ÚselfrG   rI   rJ   r5   r5   r6   Ú__init__~   s   
zWrappedCythonOp.__init__r@   )Úval_testrA   ÚsumÚ	group_sumÚprodÚ
group_prodrE   ©ÚnamerF   Ú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ÚcummaxrB   )Ú	aggregateÚ	transformzdict[str, dict]Ú_CYTHON_FUNCTIONSé   c                 C  s   || j d v r	dS dS )Nrt   ru   )rv   )ÚclsrI   r5   r5   r6   Úget_kind_from_how¢   s   z!WrappedCythonOp.get_kind_from_howr4   ú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]   rp   z2function is not implemented for this dtype: [how->z,dtype->ú])ra   rb   rE   rF   rc   r1   zPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/)
rV   rv   ÚcallableÚgetattrÚ
libgroupbyr/   r4   r1   ÚNotImplementedErrorÚ__signatures__)rx   rG   rI   r4   r{   Ú	dtype_strÚftypeÚfr5   r5   r6   Ú_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]   ra   rb   rc   Úiu)r_   r[   ru   )rQ   ri   rS   rq   rp   Úi)rI   r   r4   rG   rJ   r   r   )rN   r†   rI   r5   r5   r6   Ú_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 )Nr9   ri   z<arity of more than 1 is not supported for the 'how' argumentru   )rI   rG   Ú_cython_arityÚgetr€   Úshape)rN   r‹   r†   rI   rG   ÚarityÚ	out_shaper5   r5   r6   Ú_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 )NrB   Úfloat64©rE   rF   ÚintpÚiufcbr1   )rI   rG   Úitemsizer/   r4   )rN   r4   rI   Ú	out_dtyper5   r5   r6   Ú_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.
        )rQ   rq   rQ   rS   rp   )r[   r]   r_   ra   rb   ÚfcÚiub)rI   r/   r4   rK   Úint64rG   r“   )rN   r4   rI   r5   r5   r6   Ú_get_result_dtype  s   û

z!WrappedCythonOp._get_result_dtypeNr   )ÚmaskÚresult_maskÚinitialÚ	min_countÚcomp_idsrž   únpt.NDArray[np.bool_] | NonerŸ   r    r   c             	   K  s¦   |j dkrC|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 )Nr9   )r¡   r‹   r¢   rž   rŸ   r    r   )r;   Ú_call_cython_opr�   ÚT)rN   r†   r¡   r‹   r¢   rž   rŸ   r    ÚkwargsÚvalues2dr=   r5   r5   r6   Ú_cython_op_ndim_compat7  sB   
ÿùø
ÿùøz&WrappedCythonOp._cython_op_ndim_compat)r    c                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¿| jdkr®||d< |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�r{| jdv�r{|j jdv �r{|�s{t| jdv �rUd nd!|ƒ}||k }| 	¡ �r{|d u�rq||  ¡ �spJ ‚n
| d"¡}tj||< |j}| j| jv�r’|  |	j ¡}t||ƒ}|S |}|S )$Nr–   ÚmMrœ   TÚbÚuint8Úfloat16)r@   rA   ÚskipnaF)Úcopy©r4   rt   )rE   rF   rW   rY   r[   rg   re   rQ   rQ   r    )ÚoutÚcountsr†   Úlabelsr¡   rž   rŸ   Úis_datetimelike)rb   ra   r_   ri   rS   r]   )ra   rb   r³   )r¡   rž   rŸ   )r°   r†   r²   rž   rŸ   )rc   )r°   r±   r†   r²   rž   rŸ   z is not implementedrB   rŸ   )r°   r†   r²   r‹   r³   rž   r”   rˆ   )rQ   rS   r   r9   r“   r5   )r4   rG   ÚviewÚastyper/   Úfloat32rI   r   r1   r@   r®   rK   Úint8r¥   r’   r…   rŠ   r™   r   ÚemptyÚzerosrœ   r€   rY   rA   ÚnanÚcast_blocklistr�   r   )rN   r†   r¡   r‹   r¢   rž   rŸ   r    r¦   Úorig_valuesr4   r{   r³   r‘   Úfuncr˜   Úresultr±   ÚcutoffÚempty_groupsÚ	res_dtypeÚ	op_resultr5   r5   r6   r¤   f  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 2r9   r   )r;   r€   r   r4   )rN   rÃ   r†   r5   r5   r6   Ú_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.
        )rI   rJ   r¡   r‹   ÚidsN)r¡   r‹   r¢   rž   r5   )rÅ   r.   r/   r0   Ú_groupby_oprI   rJ   r¨   )rN   r†   rÃ   r¡   r¢   r‹   r¦   r5   r5   r6   Úcython_operation  s*   ûú	ÿûúz WrappedCythonOp.cython_operation)rG   rH   rI   rH   rJ   rK   r,   r-   )rI   rH   r,   rH   )rG   rH   rI   rH   r4   rz   r{   rK   )r†   r‡   r,   r‡   )r‹   rŒ   r†   r‡   r,   r   )r4   rz   r,   rz   )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»   rO   Ú	functoolsÚpartialr   Úgroup_any_allÚgroup_idxmin_idxmaxr`   rv   Ú__annotations__r�   Úclassmethodry   Úcacher…   rŠ   r’   r™   r�   r   r¨   r¤   rÅ   rÉ   r5   r5   r5   r6   r?   j   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d€dFdG„ƒZeedsdHdI„ƒƒZed�dKdL„ƒZed‚dMdN„ƒZedƒdPdQ„ƒZe	Rd„d…dXdY„ƒZ e	Zd†d‡d_d`„ƒZ!edˆdbdc„ƒZ"e	dtd‰dgdh„ƒ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]ÚsortrK   Údropnar,   r-   c                 C  s2   t |tƒs	J |ƒ‚|| _t|ƒ| _|| _|| _d S rL   )r.   r   rÃ   ÚlistÚ
_groupingsÚ_sortrÚ   )rN   rÃ   r×   rÙ   rÚ   r5   r5   r6   rO   K  s
   

zBaseGrouper.__init__úlist[grouper.Grouping]c                 C  s   | j S rL   )rÜ   ©rN   r5   r5   r6   r×   Y  s   zBaseGrouper.groupingsr   c                 C  s   t dd„ | jD ƒƒS )Nc                 s  s   � | ]}|j V  qd S rL   )r‹   ©Ú.0Úpingr5   r5   r6   Ú	<genexpr>_  s   € z$BaseGrouper.shape.<locals>.<genexpr>)Útupler×   rß   r5   r5   r6   r�   ]  ó   zBaseGrouper.shapeúIterator[Hashable]c                 C  ó
   t | jƒS rL   )ÚiterÚindicesrß   r5   r5   r6   Ú__iter__a  s   
zBaseGrouper.__iter__rŒ   c                 C  rç   rL   )r<   r×   rß   r5   r5   r6   Únkeysd  ó   
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)rN   rí   rÃ   ÚsplitterÚkeysr5   r5   r6   Ú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)rN   rí   rÃ   rÇ   Ú_r‹   r5   r5   r6   rñ   w  s   úzBaseGrouper._get_splitterc                 C  s6   t | jƒdkr| jd S | j\}}}t||| j| jƒS ©Nr9   r   )r<   r×   Úlevelsrú   r$   Úcodes©rN   rÇ   rý   r‹   r5   r5   r6   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}r9   r   c                 S  ó   g | ]}|j ‘qS r5   ©r   rà   r5   r5   r6   Ú
<listcomp>™  ó    z'BaseGrouper.indices.<locals>.<listcomp>c                 S  r  r5   ©Ú_group_indexrà   r5   r5   r6   r  š  r  )r<   r×   r.   Úresult_indexr   ré   r'   )rN   Ú
codes_listrõ   r5   r5   r6   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"   rJ   r/   Úwhererq   r&   r‹   Útake)rN   Úgroup_indexrý   rž   Ú	null_gapsr¾   r5   r5   r6   Úresult_ilocs�  s   ÿzBaseGrouper.result_ilocsú#list[npt.NDArray[np.signedinteger]]c                 C  ó   dd„ | j D ƒS )Nc                 S  r  r5   r  rà   r5   r5   r6   r  »  r  z%BaseGrouper.codes.<locals>.<listcomp>©r×   rß   r5   r5   r6   r   ¸  s   zBaseGrouper.codesúlist[Index]c                 C  r  )Nc                 S  r  r5   r  rà   r5   r5   r6   r  ¿  r  z&BaseGrouper.levels.<locals>.<listcomp>r  rß   r5   r5   r6   rÿ   ½  ó   zBaseGrouper.levelsúlist[Hashable]c                 C  r  )Nc                 S  r  r5   rU   rà   r5   r5   r6   r  Ã  r  z%BaseGrouper.names.<locals>.<listcomp>r  rß   r5   r5   r6   Ú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)Úindexr4   r®   )rú   r/   Úbincountr!   r	  )rN   rÇ   rý   r‹   r°   r5   r5   r6   rD   Å  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}r9   r   )r<   r×   ÚgroupsÚgrouping_vectorr.   rÖ   Úappendr   Úfrom_arraysrÃ   Úgroupby)rN   Ú
to_groupbyrâ   Úgvr  r5   r5   r6   r   Ò  s   


zBaseGrouper.groupsc                 C  s   t | jd ƒjS ©Nr   )r   rú   Úis_monotonic_increasingrß   r5   r5   r6   Ú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   )rK   rú   r@   rß   r5   r5   r6   rJ   è  s   zBaseGrouper.has_dropped_naú6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]c                 C  s&   |   ¡ \}}t|ƒ}t|ƒ}|||fS rL   )Ú_get_compressed_codesr<   r   )rN   r¢   Úobs_group_idsr‹   r5   r5   r6   rú   ð  s   
zBaseGrouper.group_infoc                 C  s   | j \}}}|S rL   )rú   ©rN   rÇ   rý   r5   r5   r6   Ú
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 )Nr9   Tr  r  r   r¯   )r<   r×   r%   r   r�   r"   rÝ   r/   Úaranger  r•   )rN   r  râ   r5   r5   r6   r+  ÿ  s
   
z!BaseGrouper._get_compressed_codesc                 C  rç   rL   )r<   r	  rß   r5   r5   r6   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�   )rN   r   rÇ   Úobs_idsrý   r5   r5   r6   Ú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 )Nr9   r   c                 S  r  r5   )Ú_result_indexrà   r5   r5   r6   r    r  z,BaseGrouper.result_index.<locals>.<listcomp>F)rÿ   r   Úverify_integrityr  )r<   r×   r4  Úrenamer  r3  r   )rN   r   rÿ   r5   r5   r6   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ó   r3  r   r  r"  )rN   Ú	name_listrâ   r   rÿ   r5   r5   r6   Úget_group_levels"  s   zBaseGrouper.get_group_levelsrÆ   rG   rH   rI   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.
        )ru   rt   rM   )r†   rÃ   r¡   r¢   r‹   Nr5   )r?   rJ   rú   r‹   rÉ   )rN   rG   r†   rI   rÃ   r¡   r¦   Úcy_oprÇ   rý   r‹   r5   r5   r6   Ú_cython_operation5  s   ûúzBaseGrouper._cython_operationFr3   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.   r8   r/   r0   Ú_aggregate_series_pure_pythonr	   Úmaybe_convert_objectsr   r4   )rN   r3   r½   r=  r¾   Únpvaluesr°   r5   r5   r6   Ú
agg_seriesQ  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>   r7   r4   )rN   r3   r½   rý   r‹   r¾   Úinitializedrô   r‰   Úgroupr=   r5   r5   r6   r@  r  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ð   rV   Tr   rÊ   )rc   rQ   rS   )rñ   rò   ró   r1   Ú__setattr__ÚaxesÚ_is_indexed_liker"  r<   r~   Úiloc)rN   r„   rí   rÃ   Úmutatedrô   Ú
group_keysÚresult_valuesÚzippedÚkeyrH  Ú
group_axesr=   r5   r5   r6   Úapply_groupwiseŠ  s   
zBaseGrouper.apply_groupwisec                 C  s   | j \}}}t||ƒS rL   )rú   r&   r  r5   r5   r6   rü   ´  s   
zBaseGrouper._sort_idxc                 C  s   | j \}}}| | j¡S rL   )rú   r  rü   r-  r5   r5   r6   rû   »  s   zBaseGrouper._sorted_idsN)TT)
rÃ   r   r×   rØ   rÙ   rK   rÚ   rK   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,   rK   ©r,   r*  )r,   r/  )r,   r1  ©r,   r   )r,   r7  )rÆ   )
rG   rH   rI   rH   rÃ   r   r¡   rŒ   r,   r
   )F)r3   r!   r½   r   r=  rK   r,   r
   )r3   r!   r½   r   r,   rD  )r„   r   rí   rI  rÃ   r   r,   rJ  )&rÊ   rË   rÌ   rÍ   rÓ   rO   Úpropertyr×   r�   rê   rë   rö   r   rñ   r   rò   ré   r  r   rÿ   r  rD   r   r)  rJ   rú   r.  r+  r‹   r3  r	  r:  r<  rC  r@  rU  rü   rû   r5   r5   r5   r6   rÖ   9  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 rL   )r   r`  r    ra  Úindexerr<   )rN   r`  ra  rb  r5   r5   r6   rO   ä  s   

zBinGrouper.__init__c                 C  s   dd„ t | j| jƒD ƒ}|S )r  c                 S  s   i | ]\}}|t ur||“qS r5   )r   )rá   rS  Úvaluer5   r5   r6   Ú
<dictcomp>÷  s
    þz%BinGrouper.groups.<locals>.<dictcomp>)ró   ra  r`  )rN   r¾   r5   r5   r6   r   ò  s   þzBinGrouper.groupsrŒ   c                 C  s   dS )Nr9   r5   rß   r5   r5   r6   rë   þ  s   zBinGrouper.nkeysr  c                 C  s2   | j \}}}| jd urt || jf¡}|| }|S rL   )rú   rb  r/   Úlexsort)rN   rÇ   rý   Úsorterr5   r5   r6   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 rL   ©rN  ©ÚstartÚedge©rí   r5   r6   Ú<lambda>  s    z)BinGrouper.get_iterator.<locals>.<lambda>c                   s   ˆ j d d …| |…f S rL   rg  rh  rk  r5   r6   rl    s    rÆ   N)r<   rL  ró   r`  ra  r   )rN   rí   rÃ   ÚslicerÚlengthri  rj  Úlabelr5   rk  r6   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ó   ra  r`  r   Úrange)rN   ré   r‰   ro  Úbinr5   r5   r6   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/   r0  r•   ÚdiffÚr_r`  r   r<   Úrepeat)rN   r‹   r,  Úrepr¢   r5   r5   r6   rú   1  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   r9   rÆ   )r/   ru  Úflatnonzeror`  rß   r5   r5   r6   r3  C  s   2zBinGrouper.reconstructed_codesc                 C  s0   t | jƒdkrt| jd ƒr| jdd … S | jS )Nr   r9   )r<   ra  r   rß   r5   r5   r6   r	  H  s   zBinGrouper.result_indexr  c                 C  s   | j gS rL   )ra  rß   r5   r5   r6   rÿ   O  s   zBinGrouper.levelsr  c                 C  s
   | j jgS rL   )ra  rV   rß   r5   r5   r6   r  S  rì   zBinGrouper.namesrÞ   c                 C  s6   | j }| jd }| |¡}tj||dd |jd�}|gS )Nr   F)Úin_axisÚlevelÚuniques)ra  rú   r  r   ÚGroupingr8   )rN   Úlevr   r²   râ   r5   r5   r6   r×   W  s   

ÿzBinGrouper.groupingsrL   ©r,   r-   rW  rY  rX  )rí   r+   rÃ   r   r\  )r,   rx  r]  rZ  r[  rV  )rÊ   rË   rÌ   rÍ   rÓ   rO   r   r   r^  rë   r.  rö   ré   rú   r3  r	  rÿ   r  r×   r5   r5   r5   r6   r_  Â  s6   
 ü

r_  rÃ   r   rK   c                 C  sP   t | tƒrt|ƒdkrdS | j|  || ¡S t | tƒr&| j|  || ¡S dS )Nr9   F)r.   r!   r<   rL  Úequalsr   )r3   rL  rÃ   r5   r5   r6   rM  b  s   

rM  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 rL   )	rí   r   r²   r‹   Ú_slabelsrü   rÃ   r.   rŒ   )rN   rí   r²   r‹   rù   rø   rÃ   r5   r5   r6   rO   r  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)rN   ÚsdataÚstartsÚendsri  Úendr5   r5   r6   rê   †  s   €
ÿzDataSplitter.__iter__c                 C  s   | j j| j| jd�S )Nrð   )rí   r  rü   rÃ   rß   r5   r5   r6   r‚  “  rå   zDataSplitter._sorted_dataÚ	slice_objr…  r+   c                 C  s   t | ƒ‚rL   r   )rN   r†  rŠ  r5   r5   r6   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Ì   rO   rê   r   r‚  r„  r5   r5   r5   r6   r÷   q  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©rL  r$  ©Úmethod)Ú_mgrÚ	get_sliceÚ_constructor_from_mgrrL  rV   Ú_nameÚ__finalize__)rN   r†  rŠ  ÚmgrÚserr5   r5   r6   r„  œ  s   zSeriesSplitter._chopN)r†  r!   rŠ  r…  r,   r!   ©rÊ   rË   rÌ   r„  r5   r5   r5   r6   rŒ  ›  ó    rŒ  c                   @  r‹  )
ÚFrameSplitterr†  r   rŠ  r…  r,   c                 C  s4   |j j|d| j d�}|j||jd�}|j|dd�S )Nr9   rð   r�  r$  rŽ  )r�  r‘  rÃ   r’  rL  r”  )rN   r†  rŠ  r•  Údfr5   r5   r6   r„  ¥  s   zFrameSplitter._chopN)r†  r   rŠ  r…  r,   r   r—  r5   r5   r5   r6   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Ã   Úklassr5   r5   r6   rñ   °  s   
	ÿrñ   r  )rÃ   r   r,   rK   )rí   r+   r²   r  r‹   rŒ   rù   r  rø   r  rÃ   r   r,   r÷   )LrÍ   Ú
__future__r   rp  rÏ   Útypingr   r   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+   r7   r>   r?   rÖ   r_  rM  r÷   rŒ  r™  rñ   r5   r5   r5   r6   Ú<module>   sP     	
   R    
!*	ù