Ë
    ¤eh&%  ã                  óê   — 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j0                  «       G d„ d«      «       Z G d„ d«      Zy)é    )Úannotations)ÚIterable)ÚTYPE_CHECKINGÚLiteralÚcastN)Úcache_readonlyÚdoc)Ú
is_integerÚis_list_like)ÚPositionalIndexer)Ú	DataFrameÚSeries)Úgroupbyc                  óv   — 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y
)ÚGroupByIndexingMixinz<
    Mixin for adding ._positional_selector to GroupBy.
    c                ór   — t         r%t        t        j                  | «      }t	        |«      S | }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   ÚGroupByÚGroupByPositionalSelector©ÚselfÚgroupby_selfs     úZ/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/pandas/core/groupby/indexing.pyÚ_positional_selectorz)GroupByIndexingMixin._positional_selector%   s7   € õ\ ä¤§¡°Ó6ˆLô )¨Ó6Ð6ð  ˆLä(¨Ó6Ð6ó    c                óF  — t        |«      rgt        d„ t        t        |«      D «       «      r'| j	                  t        t        t
           |«      «      }n…| j                  t        t        |«      «      }net        |t        «      r| j                  |«      }nCt        |«      r | j                  t        t
        |«      «      }nt        dt        |«      › d�«      ‚t        |t        «      r!|r| j                   dk\  }n| j                   dk  }t        t"        j$                  |«      S )Nc              3  ó2   K  — | ]  }t        |«      –— Œ y ­w©N)r
   )Ú.0Úis     r   ú	<genexpr>zJGroupByIndexingMixin._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_indexerz7GroupByIndexingMixin._make_mask_from_positional_indexer{   sì   € ô ˜ÔÜÑ>¬$¬x¸Ó*=Ô>Ô>Ø×0Ñ0´´h¼s±mÀSÓ1IÓJ‘à×1Ñ1´$´u¸cÓ2BÓC‘ä˜œUÔ#Ø×-Ñ-¨cÓ2‰DÜ˜Œ_Ø×+Ñ+¬D´°c«NÓ;‰DäØ ¤ c£ ð ,&ð &óð ô �dœDÔ!ÙØ×,Ñ,°Ñ1‘à×,Ñ,¨qÑ0�ä”B—J‘J Ó%Ð%r   c                óP   — |dk\  r| j                   |k(  S | j                  | dz
  k(  S )Nr   é   )r-   Ú_descending_count)r   r1   s     r   r)   z(GroupByIndexingMixin._make_mask_from_int˜   s1   € Ø�!Š8Ø×(Ñ(¨CÑ/Ð/à×)Ñ)¨s¨d°Q©hÑ7Ð7r   c                ó  — |D �cg c]
  }|dk\  sŒ	|‘Œ }}|D �cg c]  }|dk  sŒ	| dz
  ‘Œ }}d}|r#|t        j                  | j                  |«      z  }|r#|t        j                  | j                  |«      z  }|S c c}w c c}w )Nr   r5   F)r.   Úisinr-   r6   )r   Úargsr1   ÚpositiveÚnegativer2   s         r   r"   z)GroupByIndexingMixin._make_mask_from_listž   sˆ   € Ø#'Ö4˜C¨3°!«8’CÐ4ˆÐ4Ø(,Ö8 °°a³�S�D˜1“HÐ8ˆÐ8à"'ˆáØ”B—G‘G˜D×1Ñ1°8Ó<Ñ<ˆDáØ”B—G‘G˜D×2Ñ2°HÓ=Ñ=ˆDàˆùò 5ùÚ8s   …
A=�A=š
B¥Bc                óì   — d}|D ]l  }t        |«      r#|| j                  t        t        |«      «      z  }Œ1t	        |t
        «      r|| j                  |«      z  }ŒVt        dt        |«      › d�«      ‚ |S )NFzInvalid argument z. Should be int or slice.)	r
   r)   r   r#   r&   r'   r(   Ú
ValueErrorr+   )r   r9   r2   r1   s       r   r$   z*GroupByIndexingMixin._make_mask_from_tuple¬   s|   € Ø"'ˆàò 	ˆCÜ˜#ŒØ˜×0Ñ0´´c¸3³Ó@Ñ@‘Ü˜C¤Ô'Ø˜×2Ñ2°3Ó7Ñ7‘ä Ø'¬¨S«	 {Ð2KÐLóð ð	ð ˆr   c                ól  — |j                   }|j                  }|j                  }|�|dk  rt        d|› d�«      ‚d}|€d}|€|dkD  r¾|| j                  |z  dk(  z  }n¨|dk\  r0|| j                  |k\  z  }|dkD  rŒ|| j                  |z
  |z  dk(  z  }ns|| 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  }|S || j
                  | k\  z  }|S )Nr   zInvalid step z. Must be non-negativeTr5   )ÚstartÚstopÚstepr=   r-   r6   r.   Úwhere)r   r1   r?   r@   rA   r2   Úoffset_arrayÚlimit_arrays           r   r(   z*GroupByIndexingMixin._make_mask_from_slice»   s  € Ø—	‘	ˆØ�x‰xˆØ�x‰xˆàÐ  q¢Ü˜}¨T¨FÐ2HÐIÓJÐJà"&ˆàˆ<ØˆDàˆ=Ø�aŠxØ˜×-Ñ-°Ñ4¸Ñ9Ñ9‘à�aŠZØ�D×)Ñ)¨UÑ2Ñ2ˆDà�aŠxØ˜×.Ñ.°Ñ6¸$Ñ>À!ÑCÑC‘ð �D×*Ñ*¨e¨VÑ3Ñ3ˆDà×1Ñ1°EÑ9¸AÑ=ˆLà×%Ñ%¨×(>Ñ(>Ñ>À%È!Á)ÑLØñˆKô Ÿ8™8 K°×1FÑ1FÈÓUˆLà�L 4Ñ'¨1Ñ,Ñ,ˆDàÐØ�qŠyØ˜×-Ñ-°Ñ4Ñ4�ð ˆð ˜×.Ñ.°4°%Ñ7Ñ7�àˆr   c                ó†   — t         r*t        t        j                  | «      }|j	                  «       S | }|j	                  «       S r   ©r   r   r   r   Ú_cumcount_arrayr   s     r   r-   z%GroupByIndexingMixin._ascending_countå   s<   € åÜ¤§¡°Ó6ˆLð ×+Ñ+Ó-Ð-ð  ˆLà×+Ñ+Ó-Ð-r   c                ól   — t         rt        t        j                  | «      }n| }|j	                  d¬«      S )NF)Ú	ascendingrF   r   s     r   r6   z&GroupByIndexingMixin._descending_countî   s.   € åÜ¤§¡°Ó6‰LàˆLà×+Ñ+°eÐ+Ó<Ð<r   N)Úreturnr   )r1   úPositionalIndexer | tuplerJ   ú
np.ndarray)r1   r#   rJ   rL   )r9   zIterable[int]rJ   úbool | np.ndarray)r9   r%   rJ   rM   )r1   r'   rJ   rM   )rJ   rL   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r3   r)   r"   r$   r(   r-   r6   © r   r   r   r       sr   „ ñð òS7ó ðS7ðj&à&ð&ð 
ó&ó:8óóó(ðT ò.ó ð.ð ò=ó ñ=r   r   c                  ó   — e Zd Zdd„Zdd„Zy)r   c                ó   — || _         y r   ©Úgroupby_object©r   rV   s     r   Ú__init__z"GroupByPositionalSelector.__init__ú   ó
   € Ø,ˆÕr   c                ón   — | j                   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.
        )rV   r3   Ú_mask_selected_objr0   s      r   Ú__getitem__z%GroupByPositionalSelector.__getitem__ý   s1   € ð> ×"Ñ"×EÑEÀcÓJˆØ×"Ñ"×5Ñ5°dÓ;Ð;r   N©rV   zgroupby.GroupByrJ   ÚNone)r1   rK   rJ   úDataFrame | Series)rN   rO   rP   rX   r\   rR   r   r   r   r   ø   s   „ ó-ô <r   r   c                  ó6   — e Zd ZdZdd„Z	 d	 	 	 	 	 dd„Zd	d„Zy)
ÚGroupByNthSelectorzO
    Dynamically substituted for GroupBy.nth to enable both call and index
    c                ó   — || _         y r   rU   rW   s     r   rX   zGroupByNthSelector.__init__%  rY   r   Nc                ó:   — | j                   j                  ||«      S r   ©rV   Ú_nth)r   ÚnÚdropnas      r   Ú__call__zGroupByNthSelector.__call__(  s   € ð
 ×"Ñ"×'Ñ'¨¨6Ó2Ð2r   c                ó8   — | j                   j                  |«      S r   rd   )r   rf   s     r   r\   zGroupByNthSelector.__getitem__/  s   € Ø×"Ñ"×'Ñ'¨Ó*Ð*r   r]   r   )rf   rK   rg   zLiteral['any', 'all', None]rJ   r_   )rf   rK   rJ   r_   )rN   rO   rP   rQ   rX   rh   r\   rR   r   r   ra   ra      s7   „ ñó-ð /3ð3à$ð3ð ,ð3ð 
ó	3ô+r   ra   )Ú
__future__r   Úcollections.abcr   Útypingr   r   r   Únumpyr.   Úpandas.util._decoratorsr   r	   Úpandas.core.dtypes.commonr
   r   Úpandas._typingr   Úpandasr   r   Úpandas.core.groupbyr   r   r   r   ra   rR   r   r   ú<module>rs      ss   ðÝ "å $÷ñ ó ÷÷
ñ
 Ý0÷õ ,÷U=ñ U=ñp Ð×.Ñ.Ó/÷$<ð $<ó 0ð$<÷N+ò +r   