
    Mh&%                        S SK Jr  S SKJr  S SKJrJrJr  S SKr	S SK
JrJr  S SKJrJr  \(       a  S SKJr  S SKJrJr  S S	KJr   " S
 S5      r\" \R0                  5       " S S5      5       r " S S5      rg)    )annotations)Iterable)TYPE_CHECKINGLiteralcastN)cache_readonlydoc)
is_integeris_list_like)PositionalIndexer)	DataFrameSeries)groupbyc                      \ rS rSrSr\SS j5       r    SS jrSS jrSS jr	SS jr
SS jr\SS	 j5       r\SS
 j5       rSrg)GroupByIndexingMixin    z4
Mixin for adding ._positional_selector to GroupBy.
c                h    [         (       a  [        [        R                  U 5      nOU n[	        U5      $ )a  
Return positional selection for each group.

``groupby._positional_selector[i:j]`` is similar to
``groupby.apply(lambda x: x.iloc[i:j])``
but much faster and preserves the original index and order.

``_positional_selector[]`` is compatible with and extends :meth:`~GroupBy.head`
and :meth:`~GroupBy.tail`. For example:

- ``head(5)``
- ``_positional_selector[5:-5]``
- ``tail(5)``

together return all the rows.

Allowed inputs for the index are:

- An integer valued iterable, e.g. ``range(2, 4)``.
- A comma separated list of integers and slices, e.g. ``5``, ``2, 4``, ``2:4``.

The output format is the same as :meth:`~GroupBy.head` and
:meth:`~GroupBy.tail`, namely
a subset of the ``DataFrame`` or ``Series`` with the index and order preserved.

Returns
-------
Series
    The filtered subset of the original Series.
DataFrame
    The filtered subset of the original DataFrame.

See Also
--------
DataFrame.iloc : Purely integer-location based indexing for selection by
    position.
GroupBy.head : Return first n rows of each group.
GroupBy.tail : Return last n rows of each group.
GroupBy.nth : Take the nth row from each group if n is an int, or a
    subset of rows, if n is a list of ints.

Notes
-----
- The slice step cannot be negative.
- If the index specification results in overlaps, the item is not duplicated.
- If the index specification changes the order of items, then
  they are returned in their original order.
  By contrast, ``DataFrame.iloc`` can change the row order.
- ``groupby()`` parameters such as as_index and dropna are ignored.

The differences between ``_positional_selector[]`` and :meth:`~GroupBy.nth`
with ``as_index=False`` are:

- Input to ``_positional_selector`` can include
  one or more slices whereas ``nth``
  just handles an integer or a list of integers.
- ``_positional_selector`` can  accept a slice relative to the
  last row of each group.
- ``_positional_selector`` does not have an equivalent to the
  ``nth()`` ``dropna`` parameter.

Examples
--------
>>> df = pd.DataFrame([["a", 1], ["a", 2], ["a", 3], ["b", 4], ["b", 5]],
...                   columns=["A", "B"])
>>> df.groupby("A")._positional_selector[1:2]
   A  B
1  a  2
4  b  5

>>> df.groupby("A")._positional_selector[1, -1]
   A  B
1  a  2
2  a  3
4  b  5
)r   r   r   GroupByGroupByPositionalSelectorselfgroupby_selfs     N/var/www/html/env/lib/python3.13/site-packages/pandas/core/groupby/indexing.py_positional_selector)GroupByIndexingMixin._positional_selector%   s*    \ =6LL(66    c                   [        U5      (       al  [        S [        [        U5       5       5      (       a'  U R	                  [        [        [
           U5      5      nOU R                  [        [        U5      5      nOo[        U[        5      (       a  U R                  U5      nOH[        U5      (       a   U R                  [        [
        U5      5      nO[        S[        U5       S35      e[        U[        5      (       a&  U(       a  U R                   S:  nOU R                   S:  n[        ["        R$                  U5      $ )Nc              3  8   #    U  H  n[        U5      v   M     g 7fN)r
   ).0is     r   	<genexpr>JGroupByIndexingMixin._make_mask_from_positional_indexer.<locals>.<genexpr>   s     >*=Q:a==*=s   zInvalid index zE. Must be integer, list-like, slice or a tuple of integers and slicesr   )r   allr   r   _make_mask_from_listint_make_mask_from_tupletuple
isinstanceslice_make_mask_from_slicer
   _make_mask_from_int	TypeErrortypebool_ascending_countnpndarrayr   argmasks      r   "_make_mask_from_positional_indexer7GroupByIndexingMixin._make_mask_from_positional_indexer{   s     >$x*=>>>00hsmS1IJ11$uc2BCU##--c2D__++DcN;D c ,& &  dD!!,,1,,q0BJJ%%r   c                R    US:  a  U R                   U:H  $ U R                  U* S-
  :H  $ )Nr      )r0   _descending_count)r   r4   s     r   r,   (GroupByIndexingMixin._make_mask_from_int   s1    !8((C//))sdQh77r   c                6   U Vs/ s H  o"S:  d  M
  UPM     nnU Vs/ s H  o"S:  d  M
  U* S-
  PM     nnSnU(       a$  U[         R                  " U R                  U5      -  nU(       a$  U[         R                  " U R                  U5      -  nU$ s  snf s  snf )Nr   r9   F)r1   isinr0   r:   )r   argsr4   positivenegativer5   s         r   r%   )GroupByIndexingMixin._make_mask_from_list   s    #'44C!8C44(,8aHSD1H8"'BGGD118<<DBGGD22H==D 58s   	BB	B
Bc                   SnU Hv  n[        U5      (       a#  X R                  [        [        U5      5      -  nM6  [	        U[
        5      (       a  X R                  U5      -  nM`  [        S[        U5       S35      e   U$ )NFzInvalid argument z. Should be int or slice.)	r
   r,   r   r&   r)   r*   r+   
ValueErrorr.   )r   r>   r5   r4   s       r   r'   *GroupByIndexingMixin._make_mask_from_tuple   sx    "'C#00c3@@C''22377 'S	{2KL   r   c                p   UR                   nUR                  nUR                  nUb  US:  a  [        SU S35      eSnUc  SnUc  US:  a  XPR                  U-  S:H  -  nOUS:  a/  XPR                  U:  -  nUS:  a  XPR                  U-
  U-  S:H  -  nOqXPR
                  U* :  -  nU R
                  U-   S-   nU R                  U R
                  -   US-   -   S:  n[        R                  " XpR                  U5      nXVU-  S:H  -  nUb+  US:  a  XPR                  U:  -  nU$ XPR
                  U* :  -  nU$ )Nr   zInvalid step z. Must be non-negativeTr9   )startstopsteprC   r0   r:   r1   where)r   r4   rF   rG   rH   r5   offset_arraylimit_arrays           r   r+   *GroupByIndexingMixin._make_mask_from_slice   sq   		xxxxq}TF2HIJJ"&<D=ax--499aZ))U22Dax..6$>!CC **eV33D11E9A=L%%(>(>>%!)LK 88K1F1FUL4'1,,Dqy--44  ..4%77r   c                    [         (       a*  [        [        R                  U 5      nUR	                  5       $ U nUR	                  5       $ r   r   r   r   r   _cumcount_arrayr   s     r   r0   %GroupByIndexingMixin._ascending_count   s>    =6L ++--  L++--r   c                p    [         (       a  [        [        R                  U 5      nOU nUR	                  SS9$ )NF)	ascendingrN   r   s     r   r:   &GroupByIndexingMixin._descending_count   s0    =6LL++e+<<r    N)returnr   )r4   PositionalIndexer | tuplerU   
np.ndarray)r4   r&   rU   rW   )r>   zIterable[int]rU   bool | np.ndarray)r>   r(   rU   rX   )r4   r*   rU   rX   )rU   rW   )__name__
__module____qualname____firstlineno____doc__r   r   r6   r,   r%   r'   r+   r0   r:   __static_attributes__rT   r   r   r   r       sr     S7 S7j&&& 
&:8(T . . = =r   r   c                  (    \ rS rSrSS jrSS jrSrg)r      c                    Xl         g r   groupby_objectr   rc   s     r   __init__"GroupByPositionalSelector.__init__       ,r   c                n    U R                   R                  U5      nU R                   R                  U5      $ )a<  
Select by positional index per group.

Implements GroupBy._positional_selector

Parameters
----------
arg : PositionalIndexer | tuple
    Allowed values are:
    - int
    - int valued iterable such as list or range
    - slice with step either None or positive
    - tuple of integers and slices

Returns
-------
Series
    The filtered subset of the original groupby Series.
DataFrame
    The filtered subset of the original groupby DataFrame.

See Also
--------
DataFrame.iloc : Integer-location based indexing for selection by position.
GroupBy.head : Return first n rows of each group.
GroupBy.tail : Return last n rows of each group.
GroupBy._positional_selector : Return positional selection for each group.
GroupBy.nth : Take the nth row from each group if n is an int, or a
    subset of rows, if n is a list of ints.
)rc   r6   _mask_selected_objr3   s      r   __getitem__%GroupByPositionalSelector.__getitem__   s1    > ""EEcJ""55d;;r   rb   Nrc   zgroupby.GroupByrU   None)r4   rV   rU   DataFrame | Series)rY   rZ   r[   r\   re   rj   r^   rT   r   r   r   r      s    - <r   r   c                  F    \ rS rSrSrSS jr S	     S
S jjrSS jrSrg)GroupByNthSelectori   zG
Dynamically substituted for GroupBy.nth to enable both call and index
c                    Xl         g r   rb   rd   s     r   re   GroupByNthSelector.__init__%  rg   r   Nc                8    U R                   R                  X5      $ r   rc   _nth)r   ndropnas      r   __call__GroupByNthSelector.__call__(  s    
 ""''22r   c                8    U R                   R                  U5      $ r   rt   )r   rv   s     r   rj   GroupByNthSelector.__getitem__/  s    ""''**r   rb   rl   r   )rv   rV   rw   zLiteral['any', 'all', None]rU   rn   )rv   rV   rU   rn   )	rY   rZ   r[   r\   r]   re   rx   rj   r^   rT   r   r   rp   rp      s7    - /33$3 ,3 
	3+r   rp   )
__future__r   collections.abcr   typingr   r   r   numpyr1   pandas.util._decoratorsr   r	   pandas.core.dtypes.commonr
   r   pandas._typingr   pandasr   r   pandas.core.groupbyr   r   r   r   rp   rT   r   r   <module>r      ss    " $  

 0 ,U= U=p ../$< $< 0$<N+ +r   