
    `Vg                       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c mZ ddlmc 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rddl^m_Z_  G d d      Z` G d d      Za G d dea      Zbd'dZc G d  d!ee         Zd G d" d#ed      Ze G d$ d%ed      Zf	 d(	 	 	 	 	 	 	 	 	 d)d&Zgy)*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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                     e Zd ZdZ eg d      Zd,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-d              Zd.dZd/d0dZd1dZd2dZd2d Zd3d!Ze	 	 	 	 	 	 	 	 	 	 d4d"       Zd5d#Z	 	 	 	 	 	 d6d$Ze	 	 	 	 	 	 	 	 	 	 d7d%       Zeddd&	 	 	 	 	 	 	 	 	 	 	 	 	 d8d'       Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 d8d(       Zed)d*	 	 	 	 	 	 	 	 	 	 	 d9d+       Zy):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idxmaxc                .    || _         || _        || _        y Nkindhowhas_dropped_na)selfrL   rM   rN   s       X/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/pandas/core/groupby/ops.py__init__zWrappedCythonOp.__init__z   s    	,    	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cummaxrD   )	aggregate	transform>   r`   r_   r]   re   rf   r^   rD   rd   ro   rn   rm   rf      N)maxsizec                &   |j                   }| j                  |   |   }t        t        |      }|r|S |t	        j
                  t              k(  r8|dv rt        d| d| d      d|j                  vrt        d| d| d      |S t        d|      )Nrb   rl   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npdtyperw   NotImplementedError__signatures__)clsrL   rM   r}   
is_numeric	dtype_strftypefs           rP   _get_cython_functionz$WrappedCythonOp._get_cython_function   s     JJ	%%d+C0 J&Hbhhv&&++) E)A7  !1!11) E)A7  H%0 rR   c                2   | j                   }|dv rt        |      }|S |j                  j                  dv ra|dv s| j                  dk(  r| j                  rt        |      }|S |dv r1|j                  j                  dk(  rt        |      }|S 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)rc   ra   rq   )r]   rf   r^   rm   r   )rM   r   r}   rL   rN   r   r   )rO   valuesrM   s      rP   _get_cython_valsz WrappedCythonOp._get_cython_vals   s     hh'' $F+F   \\*,o%		[(T-@-@ (/  99<<$$+)&1F  +62FrR   c                t   | j                   }|ryt        |t              rB|dv rt        | d| d      |dvrt	        | d      |j
                  st	        | d      yt        |      rt	        | d      t        |      r|dv rt        d| d      yt        |      r|dv rt        d	| d      yy)
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^   rm   rl   z type does not support z operations)rD   z dtype not supportedz!datetime64 type does not support )r^   rl   z"timedelta64 type does not support )	rM   
isinstancer&   	TypeErrorr~   orderedr#   r   r$   )rO   r}   r   rM   s       rP   _disallow_invalid_opsz%WrappedCythonOp._disallow_invalid_ops   s     hhe-. ::5')@[ QRRH$)UG3G*HII]])UG3G*HII # u &/C&DEE$U+ ::"CC5 TUU ;!%())"DSE UVV * )rR   c                    | j                   }| j                  }| j                  j                  |d      }|dk(  r|df}|S |dkD  rt	        d      |dk(  r|j
                  }|S |f|j
                  dd  z   }|S )N   rf   rr   z<arity of more than 1 is not supported for the 'how' argumentrq   )rM   rL   _cython_aritygetr~   shape)rO   ngroupsr   rM   rL   arity	out_shapes          rP   _get_output_shapez!WrappedCythonOp._get_output_shape  s    hhyy""&&sA. &= !I  QY%N  [ I  !
V\\!"%55IrR   c                    | j                   }|dk(  rd}n(t        |      r|j                   |j                   }nd}t	        j
                  |      S )NrD   float64rw   )rM   r"   rL   itemsizer|   r}   )rO   r}   rM   	out_dtypes       rP   _get_out_dtypezWrappedCythonOp._get_out_dtype.  sJ    hh&=!I&$zzl5>>*:;	$	xx	""rR   c                <   | j                   }|dv rA|t        j                  t              k(  r#t        j                  t        j                        S |S |dv rFt        |      st        |      r|S t        |      r#t        j                  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]   rm   r]   r^   )ra   rb   rc   )	rM   r|   r}   boolint64r    r   r"   r   )rO   r}   rM   s      rP   _get_result_dtypez!WrappedCythonOp._get_result_dtype:  s     hh22&xx))  --e$(8(?!%(xx

++rR   c                2    | j                   | j                  v S rJ   )rM   _MASKED_CYTHON_FUNCTIONSrO   s    rP   	uses_maskzWrappedCythonOp.uses_maskT  s    xx48888rR   c                   t        |t              r'| j                         r | j                  |f|||d|S t        |t              re| j                         rU| j
                  dk(  sJ |j                  sJ |j                         }|j                  } | j                  |f||||d|}|S | j                  |      } | j                  |f|||dd|}| j
                  | j                  v r|S | j                  ||      S )zs
        If we have an ExtensionArray, unwrap, call _cython_operation, and
        re-wrap if appropriate.
        	min_countr   comp_idsrD   r   r   r   maskN)r   r1   r    _masked_ea_wrap_cython_operationr)   rM   r   r'   _ndarray_cython_op_ndim_compat_ea_to_cython_valuescast_blocklist_reconstruct_ea_result)	rO   r   r   r   r   kwargsr   npvalues
res_valuess	            rP   _ea_wrap_cython_operationz)WrappedCythonOp._ea_wrap_cython_operationW  s5    fo.4>>3C8488#!	
   ,1A88v%%%>>!>;;=DH444#! J ,,V40T00

 

 88t*** **6:>>rR   c                L   t        |t        t        t        f      r|j                  j                  d      }|S t        |j                  t        t        f      r#|j                  dt        j                        }|S t        |j                  t              r7|j                  |j                  j                  t        j                        }|S t        |j                  t              r'|j                  t        t        j                        }|S t!        d|j                         )NM8[ns]r   )na_valuez,function is not implemented for this dtype: )r   r*   r,   r-   r   viewr}   r.   r0   to_numpyr|   nanr/   numpy_dtyper3   rw   r~   )rO   r   r   s      rP   r   z$WrappedCythonOp._ea_to_cython_values  s    f}k>JK ++H5H  |\&BCy266BH  m4v||'?'?"&&QH  k2v?H
  &>v||nM rR   c                R   t        |j                  t              r/|j                  }|j                         }|j	                  ||      S t        |j                  t
              r]| j                  |j                  j                        }t        j                  |      }|j                         }|j	                  ||      S t        |t        t        t        f      rG|j                  dk(  sJ |j                  |j                  j                        }|j                  |      S t        )zL
        Construct an ExtensionArray result from an ndarray result.
        r}   r   )r   r}   r3   construct_array_type_from_sequencer2   r   r   from_numpy_dtyper*   r-   r,   r   r   _from_backing_datar~   )rO   r   r   r}   string_array_cls	new_dtypemasked_array_clss          rP   r   z&WrappedCythonOp._reconstruct_ea_result  s     fllK0LLE$99;#22:U2KKo6..v||/G/GHI#44Y?E$99;#22:U2KK LM##x///#)>)>?J,,Z88!!rR   c           	     `   |}|j                   }| j                  dk7  r|j                         }nt        j                  |t
              }|j                  }	 | j                  |	f|||||d|}
| j                  dk(  r t        j                  |d      j                  }|j                  |
|      S )z
        Equivalent of `_ea_wrap_cython_operation`, but optimized for masked EA's
        and cython algorithms which accept a mask.
        rp   r   r   r   r   r   result_maskrf   )rr   r   )_maskrL   copyr|   zerosr   _datar   rM   tileT_maybe_mask_result)rO   r   r   r   r   r   orig_valuesr   r   arrr   s              rP   r   z0WrappedCythonOp._masked_ea_wrap_cython_operation  s      ||99#))+K((7$7Kll0T00
#
 

 88v''+v688K --j+FFrR   )r   r   c          	        |j                   dk(  r[|d d d f   }|	|d d d f   }|	|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   )
rO   r   r   r   r   r   r   r   values2dress
             rP   r   z&WrappedCythonOp._cython_op_ndim_compat  s     ;;!dAgHD!G}&)$'2&$&&#!' C yy|q 1v 55L#t##
#
 
 	
rR   c          
         |}|j                   }	t        |	      }
t        |	      }|r|j                  d      }d}
nt	        |	      r|j                  d      }|j                   dk(  r|j                  t        j                        }|j                  }||j                  }||j                  }| j                  ||      }| j                  | j                  | j                  |j                   |
      }| j                  |      }| j                  |j                         }t        t        j                   ||            }| j                  dk(  rt        j"                  |t        j$                        }| j                  dv r |||||||||       n| j                  d	v r |||||||||
       nn| j                  dv r ||||||||       nP ||||||fi | nB| j'                         r$| j                  dk7  r||d<    |d||||||d| n |d|||||d| | j                  dk(  rt)        |j                         r~|s|t+        | j                  dv rdnd|      }|k  }|j-                         rK|%| j'                         r||   j/                         s&J |j                  d      }t        j0                  ||<   |j                  }| j                  | j2                  vr)| j5                  |j                         }t7        ||      }|S |}|S )Nr   Tuint8float16r   rp   )r_   r`   ra   re   rd   )outcountsr   labelsr   r   r   is_datetimelike)r]   )r   r   r   r   r   r   r   r   )rf   r^   )r   r   r   rD   r   )r   r   r   r   r   r   )r   r   r   r   r   )r]   r^   r   r   r    )r}   r"   r%   r   r   astyper|   float32r   r   r   rL   rM   r   r   r(   emptyr   r   r   r!   r`   anyallr   r   r   r   )rO   r   r   r   r   r   r   r   r   r}   r   r   r   funcr   resultr   cutoffempty_groups	res_dtype	op_results                        rP   r   zWrappedCythonOp._call_cython_op  s    %e,
-e4[[)FJ5![[)F<<9$]]2::.F66D&)mm**7F;	((DHHfllJW&&v.''5	BHHYi@A99#XXgRXX6FxxBB!!#' +$3	 W$!!# +'$3	 --' + VVVXyKFK ~~88v%,7F=) !##$3   !##$3  99#  -o$((o"=Q1iP%##%".4>>3C*<8<<>>> "(y!9/1vv|,884...
 ..{/@/@AI/	BI  IrR   )r   c                  |j                   dkD  rt        d      |j                   dk(  r|dk(  s#J |       t        |j                        s|dk(  sJ |j                  }t	        |      }| j                  ||       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 2r   r   r   Nr   )r   r~   r   r}   r"   r   r   r|   ndarrayr   r   )	rO   r   axisr   r   r   r   r}   r   s	            rP   cython_operationz WrappedCythonOp.cython_operation  s     ;;?%&VWW[[A19"d"9$V\\2 199%e,
 	""5*5&"**-1411#!	
   +t**

 
 	
rR   )rL   strrM   r   rN   r   returnNone)rL   r   rM   r   r}   np.dtyper   r   )r   
np.ndarrayr   r   F)r}   r   r   r   )r   intr   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   r1   r   r   r   r   r   r   r   r1   )r   r   r   r   r   r   r   r   r   npt.NDArray[np.bool_] | Noner   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__	frozensetr   rQ   ry   r   r   classmethod	functools	lru_cacher   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rR   rP   rC   rC   h   s    LMN-   ,   
 /$$$ 
0  QKM Y& )1?C ' B"J(WT&
#49 8?8? 8? 	8?
 8? 
8? 8?v,"$"2<"	"6 &G&G &G 	&G
 &G 
&G &GP  .248)
)
 	)

 )
 )
 +)
 2)
 
)
 )
V @@ 	@
 @ @ +@ 2@ 
@ @D  .
 .
 	.

 .
 .
 .
 
.
 .
rR   rC   c                     e Zd ZU dZded<   	 	 	 	 	 d"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#dZed$d       Zed%d       Zd&dZ	ed'd	       Z
	 d(	 	 	 	 	 d)d
Zed(d*d       Zd Zeed               Ze	 d(	 	 	 	 	 	 	 d+d       Zed,d       Zed-d       Zeed.d              Zed/d       Zed0d       Zed1d       Zed2d       Zeed3d              Zeed3d              Zed4d       Zeed-d              Ze	 	 d5d       Zeed'd              Zed6d       Zed7d       Z ed8d       Z!e	 d9	 	 	 	 	 	 	 	 	 d:d       Z"e	 d;	 	 	 	 	 	 	 d<d        Z#e	 	 	 	 	 	 d=d!       Z$y)>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

    r7   r   Nc                    t        |t              sJ |       || _        t        |      | _        || _        || _        || _        || _        || _	        y rJ   )
r   r7   r   list
_groupings_sort
group_keysmutatedindexerdropna)rO   r   	groupingssortr  r  r	  r
  s           rP   rQ   zBaseGrouper.__init__  sN     $&,,&	26y/
$rR   c                    | j                   S rJ   )r  r   s    rP   r  zBaseGrouper.groupings  s    rR   c                :    t        d | j                  D              S )Nc              3  4   K   | ]  }|j                     y wrJ   )r   .0pings     rP   	<genexpr>z$BaseGrouper.shape.<locals>.<genexpr>   s     =dT\\=   )tupler  r   s    rP   r   zBaseGrouper.shape  s    =dnn===rR   c                ,    t        | j                        S rJ   )iterindicesr   s    rP   __iter__zBaseGrouper.__iter__  s    DLL!!rR   c                ,    t        | j                        S rJ   )lenr  r   s    rP   nkeyszBaseGrouper.nkeys  s    4>>""rR   c              #  v   K   | j                  ||      }| j                  }t        ||      E d{    y7 w)
        Groupby iterator

        Returns
        -------
        Generator yielding sequence of (name, subsetted object)
        for each group
        r   N)_get_splittergroup_keys_seqzip)rO   datar   splitterkeyss        rP   get_iteratorzBaseGrouper.get_iterator	  s8      %%d%6""tX&&&s   /979c                @    | j                   \  }}}t        ||||      S )zV
        Returns
        -------
        Generator yielding subsetted objects
        r  )
group_infoget_splitter)rO   r#  r   ids_r   s         rP   r   zBaseGrouper._get_splitter  s$     //QD#wT::rR   c                4    | 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   s    rP   _get_grouperzBaseGrouper._get_grouper"  s     ~~a 000rR   c                    t        | j                        dk(  r| j                  d   S | j                  \  }}}t	        ||| j                  | j
                        S Nr   r   )r  r  levelsr(  r=   codes)rO   r*  r+  r   s       rP   r!  zBaseGrouper.group_keys_seq+  sM     t~~!#;;q>!"ooOCG &c7DKKLLrR   c                   | j                   }| j                  ||      }| j                  }g }t        ||      }|D ]R  \  }	}
t        j                  |
d|	       |
j                  } ||
      }|st        |||      sd}|j                  |       T t        |      dk(  r$t        |dd       dv r ||j                  d d        ||fS )Nr  rx   Tr   r   )madskewr]   r^   )r  r   r!  r"  rw   __setattr__axes_is_indexed_likeappendr  rz   iloc)rO   r   r#  r   r  r$  r  result_valueszippedkeygroup
group_axesr   s                rP   applyzBaseGrouper.apply6  s     ,,%%d%6((
 Z*  	&JCufc2 JE(C#3CT#J  %	& z?aGAz4$@ E
 %
 diimg%%rR   c                P   t        | j                        dk(  r3t        | j                  t              r| j                  d   j
                  S | j                  D cg c]  }|j                   }}| j                  D cg c]  }|j                   }}t        ||      S c c}w c c}w )z"dict {group name -> group indices}r   r   )	r  r  r   result_indexr6   r  r3  group_indexr@   )rO   r  
codes_listr%  s       rP   r  zBaseGrouper.indicesY  s     t~~!#
43D3DFV(W>>!$,,,-1^^<Tdjj<
<-1^^<T  <<
D11 =<s   B=B#c                   t        | j                  | j                  | j                  d      }t	        || j                        \  }}| j
                  r8t        j                  |dk\        }t        j                  |dk(        |   }||   }t        || j                        }| j
                  r|t        j                  |      z  }|S )zR
        Get the original integer locations of result_index in the input.
        Tr  xnullr  r   r   )r>   r3  r   r  r;   rN   r|   whererm   r?   r   take)rO   rD  r+  r   	null_gapsr   s         rP   result_ilocszBaseGrouper.result_ilocsc  s     &JJ

4
 .k

KQ88K1,-D		+"34T:I%d+K'T\\Bbggi00FrR   c                T    | j                   D cg c]  }|j                   c}S c c}w rJ   )r  r3  rO   r  s     rP   r3  zBaseGrouper.codes~  s      (,~~6t

666   %c                T    | j                   D cg c]  }|j                   c}S c c}w rJ   )r  rD  rO  s     rP   r2  zBaseGrouper.levels  s     -1^^<T  <<<rP  c                T    | j                   D cg c]  }|j                   c}S c c}w rJ   )r  rx   rO  s     rP   nameszBaseGrouper.names  s    &*nn5d		555rP  c                    | j                   \  }}}|rt        j                  ||dk7     |      }ng }t        || j                  d      S )z&
        Compute group sizes.
        r   )	minlengthr   )indexr}   )r(  r|   bincountr:   rC  )rO   r*  r+  r   r   s        rP   rF   zBaseGrouper.size  sH    
 //Q++c#)n@CCc!2!2'BBrR   c                    t        | j                        dk(  r| j                  d   j                  S t        d | j                  D         }t	        |      }| j
                  j                  |      S )!dict {group name -> group labels}r   r   c              3  4   K   | ]  }|j                     y wrJ   )r.  r  s     rP   r  z%BaseGrouper.groups.<locals>.<genexpr>  s     Ot33Or  )r  r  groupsr"  r7   r   groupby)rO   
to_groupbyrV  s      rP   r[  zBaseGrouper.groups  s]     t~~!#>>!$+++OOPJ*%E99$$U++rR   c                F    t        | j                  d         j                  S Nr   )r7   r(  is_monotonic_increasingr   s    rP   is_monotoniczBaseGrouper.is_monotonic  s     T__Q'(@@@rR   c                T    t        | j                  d   dk  j                               S )zE
        Whether grouper has null value(s) that are dropped.
        r   )r   r(  r   r   s    rP   rN   zBaseGrouper.has_dropped_na  s&     T__Q'!+00233rR   c                ^    | j                         \  }}t        |      }t        |      }|||fS rJ   )_get_compressed_codesr  r   )rO   r   obs_group_idsr   s       rP   r(  zBaseGrouper.group_info  s6    "&"<"<">-m$&x0//rR   c                    | j                   \  }}}| j                  1t        j                  || j                  f      }||   }t	        |      }|S rJ   )r(  r	  r|   lexsortr   )rO   r*  r+  sorters       rP   
codes_infozBaseGrouper.codes_info  sN     OO	Q<<#ZZdll 34Ff+C%c*C 
rR   c                L   t        | j                        dkD  r:t        | j                  | j                  dd      }t        || j                        S | j                  d   }|j                  t        j                  t        |j                        t        j                        fS )Nr   TrG  rI  r   r   )r  r  r>   r3  r   r;   r  r|   arangerD  intp)rO   rD  r  s      rP   rd  z!BaseGrouper._get_compressed_codes  sx    
 t~~")$**djjtSWXK'$**EE ~~a zz299S)9)9%:"''JJJrR   c                ,    t        | j                        S rJ   )r  rC  r   s    rP   r   zBaseGrouper.ngroups  s     4$$%%rR   c                n    | j                   }| j                  \  }}}t        ||| j                  |d      S )NT)rH  )r3  r(  r<   r   )rO   r3  r*  obs_idsr+  s        rP   reconstructed_codeszBaseGrouper.reconstructed_codes  s1    

//Wa#C$**e4PPrR   c                6   t        | j                        dk(  r5| j                  d   j                  j                  | j                  d         S | j
                  }| j                  D cg c]  }|j                   }}t        ||d| j                        S c c}w )Nr   r   F)r2  r3  verify_integrityrS  )r  r  rC  renamerS  rp  r8   )rO   r3  r  r2  s       rP   rC  zBaseGrouper.result_index  s    t~~!#>>!$1188AGG((04?$##??djj
 	
 @s   (Bc                ,   t        | j                        dk(  r| j                  d   j                  gS g }t        | j                  | j                        D ]<  \  }}t        |      }|j                  j                  |      }|j                  |       > |S r1  )r  r  group_arrayliker"  rp  r   rK  r:  )rO   	name_listr  r3  r2  s        rP   get_group_levelszBaseGrouper.get_group_levels  s     t~~!#NN1%5566	t~~t/G/GH 	%KD%'.E))..u5FV$		% rR   c           	         |dv sJ t        ||| j                        }| j                  \  }}	}	| j                  }
 |j                  d|||||
d|S )z;
        Returns the values of a cython operation.
        )rq   rp   rK   )r   r   r   r   r   r   )rC   rN   r(  r   r   )rO   rL   r   rM   r   r   r   cy_opr*  r+  r   s              rP   _cython_operationzBaseGrouper._cython_operation  sr     1111Ts4CVCVWOO	Q,,%u%% 

 
 	
rR   c                H   t        |      dk(  r| j                  ||      }nKt        |j                  t        j
                        s| j                  ||      }d}n| j                  ||      }t        j                  |d      }|rt        ||j                  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   r}   )rO   objr   preserve_dtyper   r   r   s          rP   
agg_serieszBaseGrouper.agg_series  s    & s8q=77TBFCKK477TBF "N 77TBF,,VuE-h		PTUC 
 C
rR   c                   | j                   \  }}}t        j                  |t              }t        j                  |d      }d}t        |||d      }	t        |	      D ]]  \  }
} ||      }t        j                  |      }|s"t        j                  ||j                         d}|j                  d   ||
<   |||
<   _ |S )Nr   OFr   r  T)r(  r|   r   r   r   r)  	enumeratelibreductionextract_resultcheck_result_arrayr}   r   )rO   r  r   r*  r+  r   r   r   initializedr$  r   r?  r   s                rP   r~  z)BaseGrouper._aggregate_series_pure_pythonB  s     //Q'-'-  S':!(+ 
	HAuu+C--c2C//U[[A"AF1IF1I
	 rR   )TTFNT)r   r7   r  zSequence[grouper.Grouping]r  r   r  r   r  r   r	  znpt.NDArray[np.intp] | Noner
  r   r   r   r   zlist[grouper.Grouping])r   r   )r   zIterator[Hashable]r   r   r   )r#  r   r   r   r   z#Iterator[tuple[Hashable, NDFrameT]])r#  rA   r   r   r   DataSplitter)r   r   r#  zDataFrame | Seriesr   r   r   ztuple[list, bool])r   z$dict[Hashable, npt.NDArray[np.intp]]r   npt.NDArray[np.intp])r   z#list[npt.NDArray[np.signedinteger]]r   zlist[Index]r   zlist[Hashable])r   r:   )r   zdict[Hashable, np.ndarray]r   r   z6tuple[npt.NDArray[np.intp], npt.NDArray[np.intp], int])r   z:tuple[npt.NDArray[np.signedinteger], npt.NDArray[np.intp]])r   zlist[npt.NDArray[np.intp]]r   r7   )r   zlist[ArrayLike])r   )
rL   r   rM   r   r   r   r   r   r   r   r   )r  r:   r   r   r  r   r   r   )r  r:   r   r   r   znpt.NDArray[np.object_])%r   r   r   r   __annotations__rQ   propertyr  r   r  r  r&  r   r   r/  r   r!  rA  r  rM  r3  r2  rS  rF   r[  ra  rN   r(  ri  rd  r   rp  rC  rw  rz  r  r~  r   rR   rP   r  r    sj   * K /3 . 	
   -  
(   > >" # # +,''$''	,' ; ;1 M  M AB & &!3 &;> &	 &  &D 2 2  4 7  7 = = 6 6 
C 
C , , A  A 4  4 0 0 	  	 
K	C
K 
K &  & Q Q
 
 
  $  

 	

 
 
 

 
6 BG''!)';?'	' 'R !)	  rR   r  c                      e Zd ZU dZded<   ded<   ded<   	 	 d	 	 	 dd	Zed
        Zedd       Z	d Z
dddZed        Zedd       Zedd       Zedd       Zedd       Zedd       Zed d       Zd!dZy)"
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]binsr7   	binlabelsr   r  Nc                    t        |      | _        t        |      | _        || _        || _        t        | j                        t        | j                        k(  sJ y rJ   )r   r  r9   r  r  r	  r  )rO   r  r  r  r	  s        rP   rQ   zBinGrouper.__init__  sK     !&	%i0 4>>"c$))n444rR   c                    t        | j                  | j                        D ci c]  \  }}|t        ur|| }}}|S c c}}w )rY  )r"  r  r  r   )rO   r>  valuer   s       rP   r[  zBinGrouper.groups  sK     "$..$))<
U#~ J
 

 
s   >c                     y)Nr   r   r   s    rP   r  zBinGrouper.nkeys  s     rR   c                    | S )r-  r   r   s    rP   r/  zBinGrouper._get_grouper  s	     rR   c              #     K   |dk(  rfd}nfd}t        j                  |         }d}t        | j                  | j                        D ]  \  }}|t
        ur| |||      f |} ||k  r| j                  d    ||d      f yyw)r  r   c                "    j                   | | S rJ   r;  startedger#  s     rP   <lambda>z)BinGrouper.get_iterator.<locals>.<lambda>  s    5)> rR   c                .    j                   d d | |f   S rJ   r  r  s     rP   r  z)BinGrouper.get_iterator.<locals>.<lambda>  s    1eDj=)A rR   r   N)r  r8  r"  r  r  r   )rO   r#  r   slicerlengthr  r  labels    `      rP   r&  zBinGrouper.get_iterator  s      19>FAFTYYt_%tyy$..9 	KD%CVE4000E	
 6>..$fUD&999 s   BBc                    t        j                  t              }d}t        | j                  | j
                        D ]-  \  }}||k  s|t        urt        t        ||            ||<   |}/ |S r_  )collectionsdefaultdictr  r"  r  r  r   range)rO   r  r   r  bins        rP   r  zBinGrouper.indices  sh    ))$/dnndii8 	JE33w#%)%3-%8GEN		
 rR   c                   | j                   }t        j                  |t        j                        }t        j                  t        j
                  d| j                  f         }t        |      }|t        | j                        k(  r*t        j                  t        j                  |      |      }n<t        j                  t        j
                  dt        j                  |      f   |      }t        |      ||fS )Nr   r   r   )
r   r|   rk  rl  diffr_r  r   r  repeat)rO   r   re  repr   s        rP   r(  zBinGrouper.group_info  s    ,,		'9ggbeeAtyyL)*!#&c$))n$yy7!3S9Hyyr299W+='=!>DH  )
 	
rR   c                    t         j                  dt        j                  | j                  dd  | j                  d d k7        dz   f   gS )Nr   r   r   )r|   r  flatnonzeror  r   s    rP   rp  zBinGrouper.reconstructed_codes  sB     a		!"3B(GH1LLMNNrR   c                    t        | j                        dk7  r't        | j                  d         r| j                  dd  S | j                  S )Nr   r   )r  r  r'   r   s    rP   rC  zBinGrouper.result_index  s>    t~~!#T^^A->(?>>!"%%~~rR   c                    | j                   gS rJ   )r  r   s    rP   r2  zBinGrouper.levels  s    rR   c                0    | j                   j                  gS rJ   )r  rx   r   s    rP   rS  zBinGrouper.names  s    ##$$rR   c                R    | j                   }t        j                  ||dd       }|gS )NF)in_axislevel)r  r5   Grouping)rO   levr  s      rP   r  zBinGrouper.groupings  s(    nnS%tDvrR   c                    t        d      )Nz=This should not be reached; use _aggregate_series_pure_python)r~   )rO   r  r   s      rP   _aggregate_series_fastz!BinGrouper._aggregate_series_fast  s    !K
 	
rR   )FN)r  r   r   r   r  r  )r#  rA   r   r   r  )r   zlist[np.ndarray]r  r  r  r  )r  r:   r   r   r   r	   )r   r   r   r   r  rQ   r   r[  r  r  r/  r&  r  r(  rp  rC  r2  rS  r  r  r   rR   rP   r  r  ^  s    8  M 5 	5 
5  	 	  :2 	 	 
 
" O O       % %  

rR   r  c                    t        | t              r0t        |      dkD  ry| j                  |   j	                  ||         S t        | t
              r!| j                  |   j	                  ||         S y)Nr   F)r   r:   r  r8  equalsr4   )r  r8  r   s      rP   r9  r9    sa    #vt9q=xx~$$T$Z00	C	#xx~$$T$Z00rR   c                  n    e Zd Z	 d	 	 	 	 	 	 	 	 	 d	dZed
d       Zed
d       Zd Zedd       ZddZ	y)r  c                |    || _         t        |      | _        || _        || _        t        |t              sJ |       y rJ   )r#  r   r   r   r   r   r   )rO   r#  r   r   r   s        rP   rQ   zDataSplitter.__init__  s:     	)&1	$$*d*$rR   c                L    | j                   j                  | j                        S rJ   )r   rK  	_sort_idxr   s    rP   slabelszDataSplitter.slabels  s     {{//rR   c                B    t        | j                  | j                        S rJ   )r?   r   r   r   s    rP   r  zDataSplitter._sort_idx$  s     &dkk4<<@@rR   c              #    K   | j                   }| j                  dk(  ry t        j                  | j                  | j                        \  }}t        ||      D ]#  \  }}| j                  |t        ||             % y wr_  )sorted_datar   r   generate_slicesr  r"  _chopslice)rO   sdatastartsendsr  ends         rP   r  zDataSplitter.__iter__)  sr       <<1 **4<<Ffd+ 	7JE3**UE%$566	7s   A=A?c                d    | j                   j                  | j                  | j                        S )Nr  )r#  rK  r  r   r   s    rP   r  zDataSplitter.sorted_data6  s!    yy~~dnn499~==rR   c                    t        |       rJ   r   )rO   r  	slice_objs      rP   r  zDataSplitter._chop:  s    !$''rR   Nr  )
r#  r   r   r  r   r   r   r   r   r   r  )r   r   )r  r  r   rA   )
r   r   r   rQ   r   r  r  r  r  r  r   rR   rP   r  r    s     ++ %+ 	+
 + 
+ 0 0 A A7 > >(rR   r  c                      e Zd ZddZy)SeriesSplitterc                    |j                   j                  |      }|j                  ||j                  d      }|j	                  |d      S )NT)rx   fastpathr\  method)_mgr	get_slice_constructorrx   __finalize__)rO   r  r  mgrsers        rP   r  zSeriesSplitter._chop?  sG    jj""9-  5:: Ei88rR   N)r  r:   r  r  r   r:   r   r   r   r  r   rR   rP   r  r  >  s    9rR   r  c                      e Zd ZddZy)FrameSplitterc                    |j                   j                  |d| j                  z
        }|j                  |      }|j	                  |d      S )Nr   r  r\  r  )r  r  r   r  r  )rO   r  r  r  dfs        rP   r  zFrameSplitter._chopG  sH     jj""91tyy="A$uY77rR   N)r  r4   r  r  r   r4   r  r   rR   rP   r  r  F  s    8rR   r  c                R    t        | t              rt        }nt        } || |||      S rJ   )r   r:   r  r  )r#  r   r   r   klasss        rP   r)  r)  R  s+     $$2 vw--rR   )r   r   r   r   r  )
r#  rA   r   r   r   r   r   r   r   r  )hr   
__future__r   r  r   typingr   r   r   r   r   r	   r
   r   numpyr|   pandas._libsr   r   pandas._libs.groupby_libsr\  r{   pandas._libs.reduction	reductionr  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.integerr0   pandas.core.arrays.maskedr1   r2   pandas.core.arrays.string_r3   pandas.core.framer4   pandas.core.groupbyr5   pandas.core.indexes.apir6   r7   r8   r9   pandas.core.seriesr:   pandas.core.sortingr;   r<   r=   r>   r?   r@   pandas.core.genericrA   rC   r  r  r9  r  r  r  r)  r   rR   rP   <module>r     sC   #  	 	 	  * ) - -  . 2     7
  4 5 3 3 ' '  &  +c	
 c	
LM M``
 `
F+(78$ +(\9\ 9	8L 	8 BC	.
	.%	.03	.;>	.	.rR   