
    Eg&%                        d dl mZ d dlmZ d dlmZmZmZ d dlZ	d dl
mZmZ d dlmZmZ erd dlmZ d dlmZmZ d d	lmZ  G d
 d          Z eej                   G d d                      Z G d d          ZdS )    )annotations)Iterable)TYPE_CHECKINGLiteralcastN)cache_readonlydoc)
is_integeris_list_like)PositionalIndexer)	DataFrameSeries)groupbyc                      e Zd ZdZedd            ZddZdd
ZddZddZ	ddZ
edd            Zedd            ZdS )GroupByIndexingMixinz<
    Mixin for adding ._positional_selector to GroupBy.
    returnGroupByPositionalSelectorc                h    t           rt          t          j        |           }n| }t	          |          S )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   GroupByr   selfgroupby_selfs     Q/var/www/sysmax/venv/lib/python3.11/site-packages/pandas/core/groupby/indexing.py_positional_selectorz)GroupByIndexingMixin._positional_selector%   s3    \  	 66LLL(666    argPositionalIndexer | tuple
np.ndarrayc                   t          |          rt          d t          t          |          D                       r4|                     t          t          t
                   |                    }n|                     t          t          |                    }nt          |t                    r| 
                    |          }nXt          |          r)|                     t          t
          |                    }n t          dt          |           d          t          |t                    r|r| j        dk    }n| j        dk     }t          t"          j        |          S )Nc              3  4   K   | ]}t          |          V  d S N)r
   ).0is     r   	<genexpr>zJGroupByIndexingMixin._make_mask_from_positional_indexer.<locals>.<genexpr>   s(      >>Q:a==>>>>>>r   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   r   masks      r   "_make_mask_from_positional_indexerz7GroupByIndexingMixin._make_mask_from_positional_indexer{   sJ     	>>$x*=*=>>>>> D00hsmS1I1IJJ11$uc2B2BCCU## 		--c22DD__ 	++DcNN;;DD&c & & &   dD!! 	1 1,1,q0BJ%%%r   r'   c                B    |dk    r| j         |k    S | j        | dz
  k    S )Nr      )r1   _descending_count)r   r   s     r   r-   z(GroupByIndexingMixin._make_mask_from_int   s-    !88(C//)sdQh77r   argsIterable[int]bool | np.ndarrayc                    d |D             }d |D             }d}|r|t          j        | j        |          z  }|r|t          j        | j        |          z  }|S )Nc                    g | ]
}|d k    |S )r    r"   r   s     r   
<listcomp>z=GroupByIndexingMixin._make_mask_from_list.<locals>.<listcomp>   s    444C3!88C888r   c                &    g | ]}|d k     | dz
  S )r   r8   r?   r@   s     r   rA   z=GroupByIndexingMixin._make_mask_from_list.<locals>.<listcomp>   s#    888aSD1Hr   F)r2   isinr1   r9   )r   r:   positivenegativer5   s        r   r&   z)GroupByIndexingMixin._make_mask_from_list   st    44444488888"' 	=BGD18<<<D 	>BGD2H===Dr   r)   c                $   d}|D ]}t          |          r,||                     t          t          |                    z  }=t	          |t
                    r||                     |          z  }kt          dt          |           d          |S )NFzInvalid argument z. Should be int or slice.)	r
   r-   r   r'   r*   r+   r,   
ValueErrorr/   )r   r:   r5   r   s       r   r(   z*GroupByIndexingMixin._make_mask_from_tuple   s    "' 	 	C# 00c3@@@C'' 223777 LS		LLL   r   r+   c                   |j         }|j        }|j        }||dk     rt          d| d          d}|d}||dk    r|| j        |z  dk    z  }n|dk    r)|| j        |k    z  }|dk    r|| j        |z
  |z  dk    z  }n\|| j        | k     z  }| j        |z   dz   }| j        | j        z   |dz   z   dk     }t          j        || j        |          }|||z  dk    z  }|$|dk    r|| j        |k     z  }n|| j        | k    z  }|S )Nr   zInvalid step z. Must be non-negativeTr8   )startstopsteprG   r1   r9   r2   where)r   r   rI   rJ   rK   r5   offset_arraylimit_arrays           r   r,   z*GroupByIndexingMixin._make_mask_from_slice   sf   	xxqITIIIJJJ"&<D=axx-499aZZD)U22Daxx.6$>!CC D*eV33D1E9A=L%(>>%!)LK 8K1FUULL4'1,,Dqyy-44.4%77r   c                r    t           rt          t          j        |           }n| }|                                S r!   r   r   r   r   _cumcount_arrayr   s     r   r1   z%GroupByIndexingMixin._ascending_count   s4     	 66LLL++---r   c                v    t           rt          t          j        |           }n| }|                    d          S )NF)	ascendingrP   r   s     r   r9   z&GroupByIndexingMixin._descending_count   s9     	 66LLL++e+<<<r   N)r   r   )r   r   r   r   )r   r'   r   r   )r:   r;   r   r<   )r:   r)   r   r<   )r   r+   r   r<   )r   r   )__name__
__module____qualname____doc__r   r   r6   r-   r&   r(   r,   r1   r9   r?   r   r   r   r       s          S7 S7 S7 ^S7j& & & &:8 8 8 8      ( ( ( (T . . . ^. = = = ^= = =r   r   c                      e Zd ZddZdd	Zd
S )r   groupby_objectgroupby.GroupByr   Nonec                    || _         d S r!   rY   r   rY   s     r   __init__z"GroupByPositionalSelector.__init__       ,r   r   r   DataFrame | Seriesc                j    | j                             |          }| j                             |          S )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.
        )rY   r6   _mask_selected_objr4   s      r   __getitem__z%GroupByPositionalSelector.__getitem__   s1    > "EEcJJ"55d;;;r   NrY   rZ   r   r[   )r   r   r   ra   )rT   rU   rV   r_   rd   r?   r   r   r   r      s<        - - - - <  <  <  <  <  <r   r   c                  .    e Zd ZdZddZ	 dddZddZdS )GroupByNthSelectorzO
    Dynamically substituted for GroupBy.nth to enable both call and index
    rY   rZ   r   r[   c                    || _         d S r!   r]   r^   s     r   r_   zGroupByNthSelector.__init__%  r`   r   Nnr   dropnaLiteral['any', 'all', None]ra   c                8    | j                             ||          S r!   rY   _nth)r   ri   rj   s      r   __call__zGroupByNthSelector.__call__(  s    
 "''6222r   c                6    | j                             |          S r!   rm   )r   ri   s     r   rd   zGroupByNthSelector.__getitem__/  s    "''***r   re   r!   )ri   r   rj   rk   r   ra   )ri   r   r   ra   )rT   rU   rV   rW   r_   ro   rd   r?   r   r   rg   rg      sd         - - - - /33 3 3 3 3+ + + + + +r   rg   )
__future__r   collections.abcr   typingr   r   r   numpyr2   pandas.util._decoratorsr   r	   pandas.core.dtypes.commonr
   r   pandas._typingr   pandasr   r   pandas.core.groupbyr   r   r   r   rg   r?   r   r   <module>rz      s   " " " " " " $ $ $ $ $ $                    
       
  ,000000        ,+++++U= U= U= U= U= U= U= U=p .//$< $< $< $< $< $< $< 0/$<N+ + + + + + + + + +r   