U
    MZf                     @  sZ  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 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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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZA ddlBmCZCmDZD ddlEmFZF ddlGmHZH ddlImJZJ ddlKmLZLmMZMmNZNmOZO ddlPmQZQ ddlRmSZSmTZTmUZUmVZVmWZWmXZX erddlYmZZZ G dd dZ[G dd dZ\G dd de\Z]ddddd Z^G d!d" d"ee Z_G d#d$ d$e_Z`G d%d& d&e_Zad-d'd(d)dd"d*d+d,ZbdS ).a  
Provide classes to perform the groupby aggregate operations.

These are not exposed to the user and provide implementations of the grouping
operations, primarily in cython. These classes (BaseGrouper and BinGrouper)
are contained *in* the SeriesGroupBy and DataFrameGroupBy objects.
    )annotationsN)TYPE_CHECKINGCallableGenericHashableIteratorSequencefinal)NaTlib)	ArrayLikeAxisInt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_period_dtype	is_sparseis_timedelta64_dtypeneeds_i8_conversion)CategoricalDtype)isna
maybe_fill)CategoricalDatetimeArrayExtensionArrayPeriodArrayTimedeltaArray)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dddddgZdddd	d
ddZddddddddddd
dddddddZdd iZe	e
jd!d"ddd#dd$d%d&Zd'd'd(d)d*ZdTd,dd-d.d/Zd0d'd1d2d3d4Zd#d#d5d6d7Zd#d#d5d8d9Zed:d0d0d'd;d<d=d>Zd:d'd(d?d@Zd:d'd:dAdBdCZedDd0d0d'dDd<dEdFZed!d!dGd'd0d0d'dHdHd'dIdJdKZed'd0d0d'dHdHd'dIdLdMZedNdOd;dPd0d'd0d;dQdRdSZd!S )U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sizeZidxminZidxmaxstrboolNone)kindhowhas_dropped_nareturnc                 C  s   || _ || _|| _d S NrF   rG   rH   )selfrF   rG   rH    rM   ;/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/groupby/ops.py__init__x   s    zWrappedCythonOp.__init__Z	group_sumZ
group_prodZ	group_minZ	group_maxZ
group_meanZgroup_median_float64Z	group_varZ	group_nthZ
group_lastZ
group_ohlc)
sumprodminmaxmeanmedianvarfirstlastohlcZgroup_cumprodZgroup_cumsumZgroup_cumminZgroup_cummaxZ
group_rank)cumprodcumsumZcumminZcummaxr@   )	aggregate	transformrY      N)maxsizeznp.dtype)rF   rG   dtype
is_numericc                 C  s   |j }| j| | }tt|}|r&|S |ttkrv|dkrRtd| d| dd|jkrrtd| d| d|S td|d S )N)rU   rZ   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npr`   rc   NotImplementedErrorZ__signatures__)clsrF   rG   r`   ra   Z	dtype_strZftypefrM   rM   rN   _get_cython_function   s&    

z$WrappedCythonOp._get_cython_function
np.ndarray)valuesrI   c                 C  sp   | j }|dkrt|}nT|jjdkrl|dks<| jdkrF| jrFt|}n&|dk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
        rU   )iu)rV   rT   r]   )rP   rY   rQ   r[   rZ   ro   )rG   r   r`   rF   rH   r   r   )rL   rn   rG   rM   rM   rN   _get_cython_vals   s    


z WrappedCythonOp._get_cython_valsFr   )r`   ra   c                 C  s   | j }|rdS t|trj|dkr4t| d| d|dkrR|jsRtd| d|dkrt| d	n|t|rt| d	ndt|r|dkrtd
| dnBt|r|dkrtd| dn t	|r|dkrtd| ddS )a+  
        Check if we can do this operation with our cython functions.

        Raises
        ------
        TypeError
            This is not a valid operation for this dtype.
        NotImplementedError
            This may be a valid operation, but does not have a cython implementation.
        N)rP   rQ   r[   rZ   z type does not support z operations)rR   rS   r@   zCannot perform z with non-ordered Categorical)r@   z dtype not supportedz!datetime64 type does not support zPeriod type does not support )rQ   rZ   z"timedelta64 type does not support )
rG   
isinstancer&   	TypeErrororderedri   r#   r   r"   r$   )rL   r`   ra   rG   rM   rM   rN   _disallow_invalid_ops   s*    
z%WrappedCythonOp._disallow_invalid_opsintr   )ngroupsrn   rI   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 )N   rY   z<arity of more than 1 is not supported for the 'how' argumentr]   )rG   rF   _cython_aritygetri   shape)rL   rw   rn   rG   rF   arity	out_shaperM   rM   rN   _get_output_shape  s    
z!WrappedCythonOp._get_output_shape)r`   rI   c                 C  s<   | j }|dkrd}nt|r.|j |j }nd}t|S )Nr@   float64rc   )rG   r!   rF   itemsizerh   r`   )rL   r`   rG   	out_dtyperM   rM   rN   _get_out_dtype  s    zWrappedCythonOp._get_out_dtypec                 C  s^   | j }|dkr*|ttkrZttjS n0|dkrZt|sBt|rF|S t|rZt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   r[   rP   rQ   rZ   )rT   rU   rV   )	rG   rh   r`   rD   int64r   r   r!   r   )rL   r`   rG   rM   rM   rN   _get_result_dtype*  s    z!WrappedCythonOp._get_result_dtyper+   r   )rn   	min_countrw   comp_idsrI   c           	      K  s   t |tr$| j|f|||d|S t |trt| jdks<t|jsFt| }|j}| j	|f||||d|}|S | 
|}| j	|f|||dd|}| j| jkr|S | ||S )zs
        If we have an ExtensionArray, unwrap, call _cython_operation, and
        re-wrap if appropriate.
        r   rw   r   r@   r   rw   r   maskN)rr   r.    _masked_ea_wrap_cython_operationr)   rG   AssertionErrorrt   r'   _ndarray_cython_op_ndim_compat_ea_to_cython_valuescast_blocklist_reconstruct_ea_result)	rL   rn   r   rw   r   kwargsr   npvalues
res_valuesrM   rM   rN   _ea_wrap_cython_operationD  sP    



	z)WrappedCythonOp._ea_wrap_cython_operationc                 C  sP   t |tttfr|jd}n.t |jtr<|jt	t
jd}ntd|j |S )NM8[ns])Zna_valuez,function is not implemented for this dtype: )rr   r*   r,   r-   r   viewr`   r0   Zto_numpyrc   rh   nanri   )rL   rn   r   rM   rM   rN   r     s    
z$WrappedCythonOp._ea_to_cython_values)rn   r   rI   c                 C  sf   t |jtr(|j}| }|j||dS t |tttfr^|jdksFt|	|j
j}||S tdS )zL
        Construct an ExtensionArray result from an ndarray result.
        r`   r   N)rr   r`   r0   Zconstruct_array_typeZ_from_sequencer*   r-   r,   r   r   r   Z_from_backing_datari   )rL   rn   r   r`   Zstring_array_clsrM   rM   rN   r     s    
z&WrappedCythonOp._reconstruct_ea_resultr.   c                 K  s   |}|j }| jdkr| }ntj|td}|j}	| j|	f|||||d|}
| jdkr|| j	
| jd}t||dfj}||
|S )z
        Equivalent of `_ea_wrap_cython_operation`, but optimized for masked EA's
        and cython algorithms which accept a mask.
        r\   r   r   rw   r   r   result_maskrY   rx   )Z_maskrF   copyrh   zerosrD   _datar   rG   ry   rz   ZtileTZ_maybe_mask_result)rL   rn   r   rw   r   r   orig_valuesr   r   Zarrr   r|   rM   rM   rN   r     s*    



z0WrappedCythonOp._masked_ea_wrap_cython_operation)r   r   znpt.NDArray[np.bool_] | None)rn   r   rw   r   r   r   rI   c          
      K  s   |j dkr|d d d f }|d k	r2|d d d f }|d k	rJ|d d d f }| j|f|||||d|}	|	jd dkr~|	d S |	jS | j|f|||||d|S )Nrx   r   r   )ndim_call_cython_opr{   r   )
rL   rn   r   rw   r   r   r   r   Zvalues2dresrM   rM   rN   r     s>    
	z&WrappedCythonOp._cython_op_ndim_compatc             
   K  s"  |}|j }	t|	}
t|	}|r.|d}d}
nt|	r@|d}|j dkrV|tj}|j}|d k	rx|j}|d k	rx|j}| 	||}| 
| j| j|j |
}| |}| |j }ttj||d}| jdkrBtj|tjd}| jdkr|||||||||d n<| jd	kr0|||||f|||d
| nt| j dn0| jdkrV||d< |f ||||||d| | jdkrt|j r|st| jdkrdnd|}||k }| r|d k	r||  stn|d}tj||< |j}| j| jkr| |j }t||}n|}|S )Nr   TZuint8Zfloat16r   r\   )rR   rS   rT   rX   rW   rP   )outcountsrn   labelsr   r   r   is_datetimelike)rV   rY   rQ   rU   )r   r   r   z is not implementedr@   r   )r   rn   r   rw   r   r   )rP   rQ   r   rx   r   )r`   r!   r%   r   r   Zastyperh   Zfloat32r   r~   rl   rF   rG   rq   r   r(   emptyr   r   ri   r    rS   anyallr   r   r   r   r   )rL   rn   r   rw   r   r   r   r   r   r`   ra   r   r}   funcr   resultr   cutoffZempty_groupsZ	res_dtype	op_resultrM   rM   rN   r     s    









zWrappedCythonOp._call_cython_op)r   r   )rn   axisr   r   rw   rI   c          	      K  s   |j dkrtd|j dkr.|dksDt|nt|jsD|dksDt|j}t|}| || t|tj	s| 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 2rx   r   r   Nr   )r   ri   r   r   r`   r!   ru   rr   rh   ndarrayr   r   )	rL   rn   r   r   r   rw   r   r`   ra   rM   rM   rN   cython_operationi  s:    


z WrappedCythonOp.cython_operation)F)__name__
__module____qualname____doc__	frozensetr   rO   re   ry   classmethod	functools	lru_cacherl   rq   ru   r~   r   r   r	   r   r   r   r   r   r   r   rM   rM   rM   rN   r?   f   sZ   
 &,;) +mr?   c                   @  sH  e Zd ZU dZded< ded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
dfddddddZedgddd dd!d"Zeed#d$ Zedhd%d&dd'd(d)d*Zed+dd,d-Zed.dd/d0Zeed1dd2d3Zed4dd5d6Zed7dd8d9Zed:dd;d<Zed=dd>d?Zeeddd@dAZeedddBdCZedDddEdFZed.ddGdHZedIddJdKZeedddLdMZedNddOdPZedddQdRZedSddTdUZ edidWdWdddXdYdZd[Z!edjd:d%ddXd]d^d_Z"ed:d%d`dadbdcZ#ddS )k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

    r4   r   TzSequence[grouper.Grouping]rD   rE   )r   	groupingssortdropnarI   c                 C  s2   t |tst||| _t|| _|| _|| _d S rJ   )rr   r4   r   r   list
_groupings_sortr   )rL   r   r   r   r   rM   rM   rN   rO     s
    
zBaseGrouper.__init__list[grouper.Grouping]rI   c                 C  s   | j S rJ   )r   rL   rM   rM   rN   r     s    zBaseGrouper.groupingsr   c                 C  s   t dd | jD S )Nc                 s  s   | ]}|j V  qd S rJ   )rw   .0pingrM   rM   rN   	<genexpr>  s     z$BaseGrouper.shape.<locals>.<genexpr>)tupler   r   rM   rM   rN   r{     s    zBaseGrouper.shapezIterator[Hashable]c                 C  s
   t | jS rJ   )iterindicesr   rM   rM   rN   __iter__  s    zBaseGrouper.__iter__rv   c                 C  s
   t | jS rJ   )lenr   r   rM   rM   rN   nkeys  s    zBaseGrouper.nkeysr   r   r   z#Iterator[tuple[Hashable, NDFrameT]])datar   rI   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)rL   r   r   splitterkeysrM   rM   rN   get_iterator  s    zBaseGrouper.get_iteratorr>   DataSplitterc                 C  s   | j \}}}t||||dS )zV
        Returns
        -------
        Generator yielding subsetted objects
        r   )
group_infor   )rL   r   r   ids_rw   rM   rM   rN   r     s    zBaseGrouper._get_splitterc                 C  s:   t | jdkr| jd S | j\}}}t||| j| jS d S Nrx   r   )r   r   levelsr   r:   codes)rL   r   r   rw   rM   rM   rN   r     s    
zBaseGrouper.group_keys_seqr   zDataFrame | Seriesztuple[list, bool])rk   r   r   rI   c                 C  s   d}| j ||d}| j}g }t||}|D ]B\}	}
t|
d|	 |
j}||
}|sbt|||sbd}|| q*t|dkrt	|dd dkr||j
d d  ||fS )NFr   rd   Tr   r   )ZskewrP   rQ   )r   r   r   rc   __setattr__axes_is_indexed_likeappendr   rf   iloc)rL   rk   r   r   Zmutatedr   Z
group_keysZresult_valuesZzippedkeygroupZ
group_axesr   rM   rM   rN   apply  s    
zBaseGrouper.applyz$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}rx   r   c                 S  s   g | ]
}|j qS rM   r   r   rM   rM   rN   
<listcomp>  s     z'BaseGrouper.indices.<locals>.<listcomp>c                 S  s   g | ]
}|j qS rM   group_indexr   rM   rM   rN   r     s     )r   r   rr   result_indexr3   r   r=   )rL   Z
codes_listr   rM   rM   rN   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   r{   r   r8   rH   rh   wherer[   r<   rw   take)rL   r   r   r   Z	null_gapsr   rM   rM   rN   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 rM   r   r   rM   rM   rN   r   8  s     z%BaseGrouper.codes.<locals>.<listcomp>r   r   rM   rM   rN   r   5  s    zBaseGrouper.codeslist[Index]c                 C  s   dd | j D S )Nc                 S  s   g | ]
}|j qS rM   r   r   rM   rM   rN   r   <  s     z&BaseGrouper.levels.<locals>.<listcomp>r   r   rM   rM   rN   r   :  s    zBaseGrouper.levelslist[Hashable]c                 C  s   dd | j D S )Nc                 S  s   g | ]
}|j qS rM   )rd   r   rM   rM   rN   r   @  s     z%BaseGrouper.names.<locals>.<listcomp>r   r   rM   rM   rN   names>  s    zBaseGrouper.namesr7   c                 C  s<   | j \}}}|r(tj||dk |d}ng }t|| jddS )z&
        Compute group sizes.
        r   )Z	minlengthr   )indexr`   )r   rh   Zbincountr7   r   )rL   r   r   rw   r   rM   rM   rN   rB   B  s
    zBaseGrouper.sizezdict[Hashable, np.ndarray]c                 C  sF   t | jdkr| jd jS tdd | jD  }t|}| j|S dS )!dict {group name -> group labels}rx   r   c                 s  s   | ]}|j V  qd S rJ   )Zgrouping_vectorr   rM   rM   rN   r   U  s     z%BaseGrouper.groups.<locals>.<genexpr>N)r   r   groupsr   r4   r   groupby)rL   Z
to_groupbyr   rM   rM   rN   r   O  s
    zBaseGrouper.groupsc                 C  s   t | jd jS Nr   )r4   r   Zis_monotonic_increasingr   rM   rM   rN   is_monotonicY  s    zBaseGrouper.is_monotonicc                 C  s   t | jd dk  S )zE
        Whether grouper has null value(s) that are dropped.
        r   )rD   r   r   r   rM   rM   rN   rH   _  s    zBaseGrouper.has_dropped_na6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int]c                 C  s&   |   \}}t|}t|}|||fS rJ   )_get_compressed_codesr   r   )rL   r   obs_group_idsrw   rM   rM   rN   r   g  s    zBaseGrouper.group_infoc                 C  s   | j \}}}|S rJ   )r   )rL   r   r   rM   rM   rN   
codes_infop  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 )Nrx   Tr   r   r   r   )r   r   r;   r   r{   r8   r   rh   aranger   intp)rL   r   r   rM   rM   rN   r   v  s
    
z!BaseGrouper._get_compressed_codesc                 C  s
   t | jS rJ   )r   r   r   rM   rM   rN   rw     s    zBaseGrouper.ngroupszlist[npt.NDArray[np.intp]]c                 C  s&   | j }| j\}}}t||| j|ddS )NT)r   )r   r   r9   r{   )rL   r   r   Zobs_idsr   rM   rM   rN   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 )Nrx   r   c                 S  s   g | ]
}|j qS rM   )r   r   rM   rM   rN   r     s     z,BaseGrouper.result_index.<locals>.<listcomp>F)r   r   Zverify_integrityr   )r   r   r   renamer   r  r5   )rL   r   r   rM   rM   rN   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   )rL   	name_listr   r   r   rM   rM   rN   get_group_levels  s    zBaseGrouper.get_group_levelsr   rC   r   )rF   rG   r   r   rI   c                 K  sJ   |dkst t||| jd}| j\}}	}	| j}
|jf |||||
d|S )z;
        Returns the values of a cython operation.
        )r]   r\   rK   )rn   r   r   r   rw   )r   r?   rH   r   rw   r   )rL   rF   rn   rG   r   r   r   Zcy_opr   r   rw   rM   rM   rN   _cython_operation  s    zBaseGrouper._cython_operationF)objr   preserve_dtyperI   c                 C  sV   t |dkrt|jtjsd}| ||}tj|dd}|rNt||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
        r   TF)Z	try_float)Znumeric_only)
r   rr   _valuesrh   r   _aggregate_series_pure_pythonr   Zmaybe_convert_objectsr   r`   )rL   r  r   r	  r   r   r   rM   rM   rN   
agg_series  s    zBaseGrouper.agg_seriesznpt.NDArray[np.object_])r  r   rI   c                 C  sr   | j \}}}tj|dd}d}| j|dd}t|D ]8\}}	||	}
t|
}
|sdt|
|	j d}|
||< q4|S )NOr   Fr   r   T)	r   rh   r   r   	enumeratelibreductionZextract_resultZcheck_result_arrayr`   )rL   r  r   r   rw   r   Zinitializedr   ro   r   r   rM   rM   rN   r    s    

z)BaseGrouper._aggregate_series_pure_pythonN)TT)r   )r   )r   )r   )F)$r   r   r   r   __annotations__rO   propertyr   r{   r   r   r   r	   r   r   r   r   r   r   r   r   r   rB   r   r   rH   r   r   r   rw   r  r   r  r  r  r  rM   rM   rM   rN   r     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]binsr4   	binlabelsNrE   r   c                 C  s6   t || _t|| _|| _t| jt| jks2td S rJ   )r   r  r6   r  indexerr   r   )rL   r  r  r  rM   rM   rN   rO   &  s    

zBinGrouper.__init__c                 C  s   dd t | j| jD }|S )r   c                 S  s   i | ]\}}|t k	r||qS rM   )r
   )r   r   valuerM   rM   rN   
<dictcomp>9  s    z%BinGrouper.groups.<locals>.<dictcomp>)r   r  r  )rL   r   rM   rM   rN   r   4  s    zBinGrouper.groupsrv   c                 C  s   dS )Nrx   rM   r   rM   rM   rN   r   @  s    zBinGrouper.nkeysr   c                 C  s2   | j \}}}| jd k	r.t|| jf}|| }|S rJ   )r   r  rh   Zlexsort)rL   r   r   ZsorterrM   rM   rN   r   E  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k	rb||||fV  |}qB||k r| jd ||dfV  dS )r   r   c                   s    j | | S rJ   r   startedger   rM   rN   <lambda>X      z)BinGrouper.get_iterator.<locals>.<lambda>c                   s    j d d | |f S rJ   r  r  r  rM   rN   r  Z  r  r   N)r   r   r   r  r  r
   )rL   r   r   Zslicerlengthr  r  labelrM   r  rN   r   N  s    	zBinGrouper.get_iteratorc                 C  sP   t t}d}t| j| jD ].\}}||k r|tk	rFtt||||< |}q|S r   )collectionsdefaultdictr   r   r  r  r
   range)rL   r   ro   r   binrM   rM   rN   r   g  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   rh   r  r  Zdiffr_r  r   r   repeat)rL   rw   r   repr   rM   rM   rN   r   s  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   rx   r   )rh   r%  Zflatnonzeror  r   rM   rM   rN   r    s    zBinGrouper.reconstructed_codesc                 C  s0   t | jdkr*t| jd r*| jdd  S | jS )Nr   rx   )r   r  r'   r   rM   rM   rN   r     s    zBinGrouper.result_indexr   c                 C  s   | j gS rJ   )r  r   rM   rM   rN   r     s    zBinGrouper.levelsr   c                 C  s
   | j jgS rJ   )r  rd   r   rM   rM   rN   r     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   r2   ZGroupingr
  )rL   Zlevr   r   r   rM   rM   rN   r     s    

    zBinGrouper.groupings)N)r   )r   r   r   r   r  rO   r   r   r  r   r   r   r   r   r  r   r   r   r   rM   rM   rM   rN   r    s4   
 

r  r   rD   )r   rI   c                 C  sP   t | tr.t|dkrdS | j| || S t | trL| j| || S dS )Nrx   F)rr   r7   r   r   equalsr1   )r  r   r   rM   rM   rN   r     s    

r   c                   @  sx   e Zd Zdd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dddddZ	dS )r   r   r   r   rv   r   rE   r   r   rw   r   rI   c                 C  s2   || _ t|| _|| _|| _t|ts.t|d S rJ   )r   r   r   rw   r   rr   rv   r   )rL   r   r   rw   r   rM   rM   rN   rO     s
    
zDataSplitter.__init__r   c                 C  s   | j | jS rJ   )r   r   	_sort_idxr   rM   rM   rN   _slabels  s    zDataSplitter._slabelsc                 C  s   t | j| jS rJ   )r<   r   rw   r   rM   rM   rN   r+    s    zDataSplitter._sort_idxr   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)rL   sdataZstartsZendsr  endrM   rM   rN   r     s    
zDataSplitter.__iter__c                 C  s   | j j| j| jdS )Nr   )r   r   r+  r   r   rM   rM   rN   r-    s    zDataSplitter._sorted_datar/  r>   )	slice_objrI   c                 C  s   t | d S rJ   r   )rL   r0  r2  rM   rM   rN   r.    s    zDataSplitter._chopN)r   )
r   r   r   rO   r   r,  r+  r   r-  r.  rM   rM   rM   rN   r     s    r   c                   @  s   e Zd ZddddddZdS )SeriesSplitterr7   r/  r0  r2  rI   c                 C  s,   |j |}|j||jdd}|j|ddS )NT)rd   Zfastpathr   method)_mgr	get_slice_constructorrd   __finalize__)rL   r0  r2  mgrZserrM   rM   rN   r.    s    zSeriesSplitter._chopNr   r   r   r.  rM   rM   rM   rN   r3    s   r3  c                   @  s   e Zd ZddddddZdS )FrameSplitterr1   r/  r4  c                 C  s.   |j j|d| j d}||}|j|ddS )Nrx   r   r   r5  )r7  r8  r   r9  r:  )rL   r0  r2  r;  ZdfrM   rM   rN   r.    s    
zFrameSplitter._chopNr<  rM   rM   rM   rN   r=    s   r=  r>   rm   rv   r*  c                 C  s"   t | trt}nt}|| |||S rJ   )rr   r7   r3  r=  )r   r   rw   r   klassrM   rM   rN   r     s    
r   )r   )cr   
__future__r   r!  r   typingr   r   r   r   r   r   r	   Znumpyrh   Zpandas._libsr
   r   Zpandas._libs.groupbyZ_libsr   rg   Zpandas._libs.reductionZ	reductionr  Zpandas._typingr   r   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   r   r   r   r   r    r!   r"   r#   r$   r%   Zpandas.core.dtypes.dtypesr&   Zpandas.core.dtypes.missingr'   r(   Zpandas.core.arraysr)   r*   r+   r,   r-   Zpandas.core.arrays.maskedr.   r/   Zpandas.core.arrays.string_r0   Zpandas.core.framer1   Zpandas.core.groupbyr2   Zpandas.core.indexes.apir3   r4   r5   r6   Zpandas.core.seriesr7   Zpandas.core.sortingr8   r9   r:   r;   r<   r=   Zpandas.core.genericr>   r?   r   r  r   r   r3  r=  r   rM   rM   rM   rN   <module>   sR   $
 D 	    9  k !. 