o
    5c                     @  sh  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
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  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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF ddlGmHZHmIZI ddlJmKZK ddlLmMZM ddlNmOZO ddlPmQZQmRZRmSZSmTZT ddlUmVZV ddlWmXZXmYZYmZZZm[Z[m\Z\m]Z] erddl^m_Z_ G dd dZ`G dd dZaG dd deaZbd2d#d$ZcG d%d& d&ee ZdG d'd( d(edZeG d)d* d*edZf	d3d4d0d1ZgdS )5a  
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HashableIteratorNoReturnSequencefinal)NaTlib)	ArrayLikeDtypeObj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is_bool_dtypeis_complex_dtypeis_datetime64_any_dtypeis_float_dtypeis_integer_dtypeis_numeric_dtype	is_sparseis_timedelta64_dtypeneeds_i8_conversion)CategoricalDtype)isna
maybe_fill)CategoricalDatetimeArrayExtensionArrayPeriodArrayTimedeltaArray)BooleanDtype)FloatingDtype)IntegerDtype)BaseMaskedArrayBaseMaskedDtype)StringDtype)	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)NDFramec                   @  s"  e Zd ZdZeg dZdVd
dZddddddddddd
dddddddZh dZdd iZ	e
ejd!d"dWd&d'ZdXd*d+ZdYdZd.d/Zd[d3d4Zd\d5d6Zd\d7d8Zd]d9d:Zed^d?d@Zd_dAdBZd`dDdEZedadGdHZed!d!dIdbdMdNZedbdOdPZedQdRdcdTdUZd!S )d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.
    )rankcountsizeidxminidxmaxkindstrhowhas_dropped_naboolreturnNonec                 C  s   || _ || _|| _d S NrG   rI   rJ   )selfrG   rI   rJ    rQ   M/var/www/html/gps/gps/lib/python3.10/site-packages/pandas/core/groupby/ops.py__init__z   s   
zWrappedCythonOp.__init__	group_sum
group_prod	group_min	group_max
group_meangroup_median_float64	group_var	group_nth
group_last
group_ohlc)
sumprodminmaxmeanmedianvarfirstlastohlcgroup_cumprod_float64group_cumsumgroup_cummingroup_cummax
group_rank)cumprodcumsumcummincummaxrB   )	aggregate	transform>   ra   r`   r^   rf   rg   r_   rB   re   rp   ro   rn   rg      N)maxsizedtypenp.dtype
is_numericc                 C  s   |j }| j| | }tt|}|r|S |ttkr;|dv r)td| d| dd|jvr9td| d| d|S td|)Nrc   rm   z2function is not implemented for this dtype: [how->z,dtype->]objectzPThis should not be reached. Please report a bug at github.com/pandas-dev/pandas/)	name_CYTHON_FUNCTIONSgetattr
libgroupbynpru   rz   NotImplementedError__signatures__)clsrG   rI   ru   rw   	dtype_strftypefrQ   rQ   rR   _get_cython_function   s6   

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
        rx   )iu)rd   rb   rr   )r^   rg   r_   rn   r   )rI   r   ru   rG   rJ   r   r   )rP   r   rI   rQ   rQ   rR   _get_cython_vals   s$   	z WrappedCythonOp._get_cython_valsFr   c                 C  s   | j }|rdS t|tr1|dv rt| d| d|dvr%t| d|js/t| ddS t|r<t| dt|rN|dv rLtd| ddS t|r^|dv r`td	| ddS dS )
z
        Check if we can do this operation with our cython functions.

        Raises
        ------
        NotImplementedError
            This is either not a valid function for this dtype, or
            valid but not implemented in cython.
        N)r^   r_   rn   rm   z type does not support z operations)rB   z dtype not supportedz!datetime64 type does not support )r_   rm   z"timedelta64 type does not support )	rI   
isinstancer%   	TypeErrorr   orderedr"   r   r#   )rP   ru   rw   rI   rQ   rQ   rR   _disallow_invalid_ops   s,   

z%WrappedCythonOp._disallow_invalid_opsngroupsintr   c                 C  sh   | j }| j}| j|d}|dkr|df}|S |dkrtd|dkr(|j}|S |f|jdd   }|S )N   rg   rs   z<arity of more than 1 is not supported for the 'how' argumentrr   )rI   rG   _cython_aritygetr   shape)rP   r   r   rI   rG   arity	out_shaperQ   rQ   rR   _get_output_shape  s   	z!WrappedCythonOp._get_output_shapec                 C  s<   | j }|dkr
d}nt|r|j |j }nd}t|S )NrB   float64rz   )rI   r!   rG   itemsizer   ru   )rP   ru   rI   	out_dtyperQ   rQ   rR   _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.t|s"t|r$|S t|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.
        )r^   rn   r^   r_   )rb   rc   rd   )	rI   r   ru   rK   int64r   r   r!   r   )rP   ru   rI   rQ   rQ   rR   _get_result_dtype:  s   z!WrappedCythonOp._get_result_dtypec                 C  s   | j | jv S rN   )rI   _MASKED_CYTHON_FUNCTIONSrP   rQ   rQ   rR   	uses_maskT  s   zWrappedCythonOp.uses_maskr*   	min_countcomp_idsr   c           	      K  s   t |tr|  r| j|f|||d|S t |trB|  rB| jdks&J |js+J | }|j}| j	|f||||d|}|S | 
|}| j	|f|||dd|}| j| jv r]|S | ||S )zs
        If we have an ExtensionArray, unwrap, call _cython_operation, and
        re-wrap if appropriate.
        r   r   r   rB   r   r   r   maskN)r   r0   r    _masked_ea_wrap_cython_operationr(   rI   r   r&   _ndarray_cython_op_ndim_compat_ea_to_cython_valuescast_blocklist_reconstruct_ea_result)	rP   r   r   r   r   kwargsr   npvalues
res_valuesrQ   rQ   rR   _ea_wrap_cython_operationW  sP   

	z)WrappedCythonOp._ea_wrap_cython_operationc                 C  s   t |tttfr|jd}|S t |jttfr"|j	dt
jd}|S t |jtr4|j	|jjt
jd}|S t |jtrD|j	tt
jd}|S td|j )NM8[ns]r   )na_valuez,function is not implemented for this dtype: )r   r)   r+   r,   r   viewru   r-   r/   to_numpyr   nanr.   numpy_dtyper2   rz   r   )rP   r   r   rQ   rQ   rR   r     s   
z$WrappedCythonOp._ea_to_cython_valuesr   c                 C  s   t |jtr|j}| }|j||dS t |jtr1| |jj}t|}| }|j||dS t |t	t
tfrL|jdks@J ||jj}||S t)zL
        Construct an ExtensionArray result from an ndarray result.
        ru   r   )r   ru   r2   construct_array_type_from_sequencer1   r   r   from_numpy_dtyper)   r,   r+   r   r   _from_backing_datar   )rP   r   r   ru   string_array_cls	new_dtypemasked_array_clsrQ   rQ   rR   r     s   

z&WrappedCythonOp._reconstruct_ea_resultr0   c                 K  st   |}|j }| jdkr| }ntj|td}|j}	| j|	f|||||d|}
| jdkr4t	|dj
}||
|S )z
        Equivalent of `_ea_wrap_cython_operation`, but optimized for masked EA's
        and cython algorithms which accept a mask.
        rq   r   r   r   r   r   result_maskrg   )rs   r   )_maskrG   copyr   zerosrK   _datar   rI   tileT_maybe_mask_result)rP   r   r   r   r   r   orig_valuesr   r   arrr   rQ   rQ   rR   r     s(   



z0WrappedCythonOp._masked_ea_wrap_cython_operation)r   r   r   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 )Nr   r   r   )ndim_call_cython_opr   r   )
rP   r   r   r   r   r   r   r   values2dresrQ   rQ   rR   r     s>   
	z&WrappedCythonOp._cython_op_ndim_compatc             
   K  sl  |}|j }	t|	}
t|	}|r|d}d}
n	t|	r |d}|j dkr+|tj}|j}|d ur<|j}|d ur<|j}| 	||}| 
| j| j|j |
}| |}| |j }ttj||d}| jdkrtj|tjd}| jdv r|||||||||d nY| jd	v r|||||||||d
 nG| jdv r||||||||d n6||||||fi | n)|  r| jdkr||d< |d||||||d| n|d|||||d| | jdkrt|j r|st| jdv rdnd|}||k }| r|d ur|  r||  sJ n
|d}tj||< |j}| j| jvr2| |j }t||}|S |}|S )Nr   Tuint8float16r   rq   )r`   ra   rb   rf   re   )outcountsr   labelsr   r   r   is_datetimelike)r^   )r   r   r   r   r   r   r   r   )rg   r_   )r   r   r   rB   r   )r   r   r   r   r   r   )r   r   r   r   r   )r^   r_   r   r   r   rQ   )ru   r!   r$   r   r   astyper   float32r   r   r   rG   rI   r   r   r'   emptyr   r   r   r    ra   anyallr   r   r   r   )rP   r   r   r   r   r   r   r   r   ru   rw   r   r   funcr   resultr   cutoffempty_groups	res_dtype	op_resultrQ   rQ   rR   r     s   












	



zWrappedCythonOp._call_cython_op)r   axisc          	      K  s   |j dkr	td|j dkr|dksJ |nt|js"|dks"J |j}t|}| || t|tjsB| j	|f|||d|S | j
|f|||dd|S )zW
        Call our cython function, with appropriate pre- and post- processing.
           z.number of dimensions is currently limited to 2r   r   r   Nr   )r   r   r   ru   r!   r   r   r   ndarrayr   r   )	rP   r   r   r   r   r   r   ru   rw   rQ   rQ   rR   cython_operation  s:   


z WrappedCythonOp.cython_operation)rG   rH   rI   rH   rJ   rK   rL   rM   )rG   rH   rI   rH   ru   rv   rw   rK   )r   r   rL   r   F)ru   r   rw   rK   )r   r   r   r   rL   r   )ru   rv   rL   rv   rL   rK   )
r   r*   r   r   r   r   r   r   rL   r   )r   r*   rL   r   )r   r*   r   r   rL   r*   )
r   r0   r   r   r   r   r   r   rL   r0   )r   r   r   r   r   r   r   r   r   r   r   r   rL   r   )r   r   r   r   r   r   r   r   r   r   rL   r   )__name__
__module____qualname____doc__	frozensetr   rS   r|   r   r   classmethod	functools	lru_cacher   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   rQ   rQ   rQ   rR   rA   h   sb    


!%
*



;
(+ rA   c                   @  s  e Zd ZU dZded< 					dodpddZedqddZedrddZdsddZ	edtddZ
	 dudvd$d%Zedudwd(d)Zd*d+ Zeed,d- Ze	 dudxd2d3Zedyd5d6Zedzd8d9Zeed{d;d<Zed|d>d?Zed}dAdBZed~dDdEZeddGdHZeeddIdJZeeddKdLZeddNdOZeedzdPdQZeddSdTZeedtdUdVZeddXdYZeddZd[Z edd]d^Z!e	_dddedfZ"e	dddjdkZ#eddmdnZ$dS )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
    group_keys : bool, default True
    mutated : bool, default False
    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

    r6   r   TFN	groupingsSequence[grouper.Grouping]sortrK   
group_keysmutatedindexernpt.NDArray[np.intp] | NonedropnarL   rM   c                 C  sD   t |ts	J ||| _t|| _|| _|| _|| _|| _|| _	d S rN   )
r   r6   r   list
_groupings_sortr   r   r   r   )rP   r   r   r   r   r   r   r   rQ   rQ   rR   rS     s   


zBaseGrouper.__init__list[grouper.Grouping]c                 C  s   | j S rN   )r   r   rQ   rQ   rR   r     s   zBaseGrouper.groupingsr   c                 C  s   t dd | jD S )Nc                 s      | ]}|j V  qd S rN   )r   .0pingrQ   rQ   rR   	<genexpr>       z$BaseGrouper.shape.<locals>.<genexpr>)tupler   r   rQ   rQ   rR   r        zBaseGrouper.shapeIterator[Hashable]c                 C  
   t | jS rN   )iterindicesr   rQ   rQ   rR   __iter__  s   
zBaseGrouper.__iter__r   c                 C  r  rN   )lenr   r   rQ   rQ   rR   nkeys     
zBaseGrouper.nkeysr   datar   #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)rP   r  r   splitterkeysrQ   rQ   rR   get_iterator	  s   zBaseGrouper.get_iteratorr@   DataSplitterc                 C  s   | j \}}}t||||dS )zV
        Returns
        -------
        Generator yielding subsetted objects
        r  )
group_infoget_splitter)rP   r  r   ids_r   rQ   rQ   rR   r    s   zBaseGrouper._get_splitterc                 C  s   | j d jS )
        We are a grouper as part of another's groupings.

        We have a specific method of grouping, so cannot
        convert to a Index for our grouper.
        r   )r   grouping_vectorr   rQ   rQ   rR   _get_grouper"  s   zBaseGrouper._get_grouperc                 C  s6   t | jdkr| jd S | j\}}}t||| j| jS Nr   r   )r	  r   levelsr  r<   codes)rP   r  r  r   rQ   rQ   rR   r  +  s   
zBaseGrouper.group_keys_seqr   r   DataFrame | Seriestuple[list, bool]c                 C  s   | j }| j||d}| j}g }t||}|D ]!\}	}
t|
d|	 |
j}||
}|s2t|||s2d}|| qt	|dkrOt
|dd dv rO||jd d  ||fS )Nr  r{   Tr   r   )madskewr^   r_   )r   r  r  r  rz   __setattr__axes_is_indexed_likeappendr	  r}   iloc)rP   r   r  r   r   r  r   result_valueszippedkeygroup
group_axesr   rQ   rQ   rR   apply6  s   
	zBaseGrouper.apply$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     g | ]}|j qS rQ   r   r   rQ   rQ   rR   
<listcomp>_      z'BaseGrouper.indices.<locals>.<listcomp>c                 S  r1  rQ   group_indexr   rQ   rQ   rR   r3  `  r4  )r	  r   r   result_indexr5   r  r?   )rP   
codes_listr  rQ   rQ   rR   r  Y  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   wherern   r>   r   take)rP   r6  r  r   	null_gapsr   rQ   rQ   rR   result_ilocsc  s   zBaseGrouper.result_ilocs#list[npt.NDArray[np.signedinteger]]c                 C     dd | j D S )Nc                 S  r1  rQ   r2  r   rQ   rQ   rR   r3    r4  z%BaseGrouper.codes.<locals>.<listcomp>r   r   rQ   rQ   rR   r   ~  s   zBaseGrouper.codeslist[Index]c                 C  rB  )Nc                 S  r1  rQ   r5  r   rQ   rQ   rR   r3    r4  z&BaseGrouper.levels.<locals>.<listcomp>rC  r   rQ   rQ   rR   r       zBaseGrouper.levelslist[Hashable]c                 C  rB  )Nc                 S  r1  rQ   )r{   r   rQ   rQ   rR   r3    r4  z%BaseGrouper.names.<locals>.<listcomp>rC  r   rQ   rQ   rR   names  rE  zBaseGrouper.namesr9   c                 C  s<   | j \}}}|rtj||dk |d}ng }t|| jddS )z&
        Compute group sizes.
        r   )	minlengthr   )indexru   )r  r   bincountr9   r7  )rP   r  r  r   r   rQ   rQ   rR   rD     s
   zBaseGrouper.sizedict[Hashable, np.ndarray]c                 C  sB   t | jdkr| jd jS tdd | jD  }t|}| j|S )!dict {group name -> group labels}r   r   c                 s  r   rN   )r  r   rQ   rQ   rR   r     r  z%BaseGrouper.groups.<locals>.<genexpr>)r	  r   groupsr  r6   r   groupby)rP   
to_groupbyrI  rQ   rQ   rR   rM    s
   zBaseGrouper.groupsc                 C  s   t | jd jS Nr   )r6   r  is_monotonic_increasingr   rQ   rQ   rR   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   rQ   rQ   rR   rJ     s   zBaseGrouper.has_dropped_na6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]c                 C  s&   |   \}}t|}t|}|||fS rN   )_get_compressed_codesr	  r   )rP   r   obs_group_idsr   rQ   rQ   rR   r    s   
zBaseGrouper.group_infoc                 C  s:   | j \}}}| jd urt|| jf}|| }t|}|S rN   )r  r   r   lexsortr   )rP   r  r  sorterrQ   rQ   rR   
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 )Nr   Tr:  r<  r   r   )r	  r   r=   r   r   r:   r   r   aranger6  intp)rP   r6  r   rQ   rQ   rR   rT    s
   
z!BaseGrouper._get_compressed_codesc                 C  r  rN   )r	  r7  r   rQ   rQ   rR   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   )rP   r   r  obs_idsr  rQ   rQ   rR   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  r1  rQ   )r7  r   rQ   rQ   rR   r3    r4  z,BaseGrouper.result_index.<locals>.<listcomp>F)r  r   verify_integrityrG  )r	  r   r7  renamerG  r^  r7   )rP   r   r  rQ   rQ   rR   r7    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  r^  r   r>  r(  )rP   	name_listr   r   r  rQ   rQ   rR   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.
        )rr   rq   rO   )r   r   r   r   r   NrQ   )rA   rJ   r  r   r   )rP   rG   r   rI   r   r   r   cy_opr  r  r   rQ   rQ   rR   _cython_operation  s   zBaseGrouper._cython_operationobjr   preserve_dtypec                 C  st   t |dkr| ||}nt|jtjs| ||}d}n| ||}tj|dd}|r6t||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
        r   TF)	try_float)numeric_only)
r	  _aggregate_series_pure_pythonr   _valuesr   r   r   maybe_convert_objectsr   ru   )rP   rg  r   rh  r   r   r   rQ   rQ   rR   
agg_series  s   zBaseGrouper.agg_seriesnpt.NDArray[np.object_]c                 C  s   | j \}}}tj|td}tj|dd}d}t|||dd}	t|	D ]#\}
}||}t|}|s:t	||j
 d}|jd ||
< |||
< q"|S )Nr   OFr   r  T)r  r   r   r   r   r  	enumeratelibreductionextract_resultcheck_result_arrayru   r   )rP   rg  r   r  r  r   r   r   initializedr  r   r-  r   rQ   rQ   rR   rk  B  s   

z)BaseGrouper._aggregate_series_pure_python)TTFNT)r   r6   r   r   r   rK   r   rK   r   rK   r   r   r   rK   rL   rM   rL   r   )rL   r   )rL   r  rL   r   r   )r  r   r   r   rL   r  )r  r@   r   r   rL   r  )r   r   r  r!  r   r   rL   r"  )rL   r0  rL   r9  )rL   rA  rL   rD  rL   rF  )rL   r9   )rL   rK  r   rL   rS  )rL   rY  )rL   r\  rL   r6   )rL   ra  )r   )
rG   rH   rI   rH   r   r   r   r   rL   r   r   )rg  r9   r   r   rh  rK   rL   r   )rg  r9   r   r   rL   ro  )%r   r   r   r   __annotations__rS   propertyr   r   r  r
  r  r
   r  r  r   r  r/  r  r@  r   r  rG  rD   rM  rR  rJ   r  rX  rT  r   r^  r7  rd  rf  rn  rk  rQ   rQ   rQ   rR   r     s   
 
			"		
)r   c                   @  s   e Zd ZU dZded< ded< ded< 			d5d6ddZedd Zed7ddZ	dd Z
d8d9ddZedd Zed:ddZed;d!d"Zed<d#d$Zed=d&d'Zed>d)d*Zed?d,d-Zd@d3d4Zd	S )A
BinGrouperaU  
    This is an internal Grouper class

    Parameters
    ----------
    bins : the split index of binlabels to group the item of axis
    binlabels : the label list
    mutated : bool, default False
    indexer : np.ndarray[np.intp]

    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]binsr6   	binlabelsrK   r   FNrL   rM   c                 C  s<   t || _t|| _|| _|| _t| jt| jksJ d S rN   )r   r  r8   r  r   r   r	  )rP   r  r  r   r   rQ   rQ   rR   rS     s
   

zBinGrouper.__init__c                 C  s   dd t | j| jD }|S )rL  c                 S  s   i | ]\}}|t ur||qS rQ   )r   )r   r,  valuerQ   rQ   rR   
<dictcomp>  s    z%BinGrouper.groups.<locals>.<dictcomp>)r  r  r  )rP   r   rQ   rQ   rR   rM    s   zBinGrouper.groupsr   c                 C  s   dS )Nr   rQ   r   rQ   rQ   rR   r
    s   zBinGrouper.nkeysc                 C  s   | S )r  rQ   r   rQ   rQ   rR   r    s   zBinGrouper._get_grouperr   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 rN   r)  startedger  rQ   rR   <lambda>  s    z)BinGrouper.get_iterator.<locals>.<lambda>c                   s    j d d | |f S rN   r  r  r  rQ   rR   r    s    r   N)r	  r&  r  r  r  r   )rP   r  r   slicerlengthr  r  labelrQ   r  rR   r    s   	zBinGrouper.get_iteratorc                 C  sP   t t}d}t| j| jD ]\}}||k r%|tur#tt||||< |}q|S rP  )collectionsdefaultdictr   r  r  r  r   range)rP   r  r   r  binrQ   rQ   rR   r    s   
zBinGrouper.indicesrS  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   rZ  r[  diffr_r  r   r	  repeat)rP   r   rU  repr   rQ   rQ   rR   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   r   r   )r   r  flatnonzeror  r   rQ   rQ   rR   r^    s   2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   rQ   rQ   rR   r7    s   zBinGrouper.result_indexrD  c                 C  s   | j gS rN   )r  r   rQ   rQ   rR   r    s   zBinGrouper.levelsrF  c                 C  s
   | j jgS rN   )r  r{   r   rQ   rQ   rR   rG    r  zBinGrouper.namesr   c                 C  s   | j }tj||dd d}|gS )NF)in_axislevel)r  r4   Grouping)rP   levr   rQ   rQ   rR   r     s   zBinGrouper.groupingsrg  r9   r   r   r   c                 C  s   t d)Nz=This should not be reached; use _aggregate_series_pure_python)r   )rP   rg  r   rQ   rQ   rR   _aggregate_series_fast  s   z!BinGrouper._aggregate_series_fast)FN)r   rK   rL   rM   rw  rx  )r  r@   r   r   r|  )rL   r  r}  rz  r{  rv  )rg  r9   r   r   rL   r   )r   r   r   r   r~  rS   r   rM  r  r
  r  r  r  r  r^  r7  r  rG  r   r  rQ   rQ   rQ   rR   r  ^  s:   
 
	
r  r   r   rL   rK   c                 C  sP   t | trt|dkrdS | j| || S t | tr&| j| || S dS )Nr   F)r   r9   r	  r&  equalsr3   )rg  r&  r   rQ   rQ   rR   r'    s   

r'  c                   @  sV   e Zd Z	ddddZedddZedddZdd ZedddZdddZ	dS ) r  r   r  r   r   r9  r   r   r   rL   rM   c                 C  s2   || _ t|| _|| _|| _t|tsJ |d S rN   )r  r   r   r   r   r   r   )rP   r  r   r   r   rQ   rQ   rR   rS     s
   
zDataSplitter.__init__c                 C  s   | j | jS rN   )r   r>  	_sort_idxr   rQ   rQ   rR   slabels     zDataSplitter.slabelsc                 C  s   t | j| jS rN   )r>   r   r   r   rQ   rQ   rR   r  $  r  zDataSplitter._sort_idxc                 c  sV    | j }| jdkrd S t| j| j\}}t||D ]\}}| |t||V  qd S rP  )sorted_datar   r   generate_slicesr  r  _chopslice)rP   sdatastartsendsr  endrQ   rQ   rR   r  )  s   
zDataSplitter.__iter__c                 C  s   | j j| j| jdS )Nr  )r  r>  r  r   r   rQ   rQ   rR   r  6  r  zDataSplitter.sorted_data	slice_objr  r@   c                 C  s   t | rN   r   )rP   r  r  rQ   rQ   rR   r  :  s   zDataSplitter._chopNrx  )
r  r   r   r9  r   r   r   r   rL   rM   ry  )rL   r   )r  r  rL   r@   )
r   r   r   rS   r   r  r  r  r  r  rQ   rQ   rQ   rR   r    s    r  c                   @     e Zd Zd	ddZdS )
SeriesSplitterr  r9   r  r  rL   c                 C  s,   |j |}|j||jdd}|j|ddS )NT)r{   fastpathrN  method)_mgr	get_slice_constructorr{   __finalize__)rP   r  r  mgrserrQ   rQ   rR   r  ?  s   zSeriesSplitter._chopN)r  r9   r  r  rL   r9   r   r   r   r  rQ   rQ   rQ   rR   r  >      r  c                   @  r  )
FrameSplitterr  r3   r  r  rL   c                 C  s.   |j j|d| j d}||}|j|ddS )Nr   r  rN  r  )r  r  r   r  r  )rP   r  r  r  dfrQ   rQ   rR   r  G  s   
zFrameSplitter._chopN)r  r3   r  r  rL   r3   r  rQ   rQ   rQ   rR   r  F  r  r  r  r@   r   r   r   c                 C  s"   t | trt}nt}|| |||S rN   )r   r9   r  r  )r  r   r   r   klassrQ   rQ   rR   r  R  s   
r  )r   r   rL   rK   rx  )
r  r@   r   r   r   r   r   r   rL   r  )hr   
__future__r   r  r   typingr   r   r   r   r   r   r	   r
   numpyr   pandas._libsr   r   pandas._libs.groupby_libsrN  r~   pandas._libs.reduction	reductionrr  pandas._typingr   r   r   r   r   pandas.errorsr   pandas.util._decoratorsr   pandas.core.dtypes.castr   r   pandas.core.dtypes.commonr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   pandas.core.dtypes.dtypesr%   pandas.core.dtypes.missingr&   r'   pandas.core.arraysr(   r)   r*   r+   r,   pandas.core.arrays.booleanr-   pandas.core.arrays.floatingr.   pandas.core.arrays.integerr/   pandas.core.arrays.maskedr0   r1   pandas.core.arrays.string_r2   pandas.core.framer3   pandas.core.groupbyr4   pandas.core.indexes.apir5   r6   r7   r8   pandas.core.seriesr9   pandas.core.sortingr:   r;   r<   r=   r>   r?   pandas.core.genericr@   rA   r   r  r'  r  r  r  r  rQ   rQ   rQ   rR   <module>   s\    (@ 	    j    
$.