1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
U
¬ý°d9&ã@s°ddlmZddlmZmZmZmZddlZddl    m
Z
ddl m Z m Z ddlmZmZerxddlmZmZddlmZGd    d
„d
ƒZe ejƒGd d „d ƒƒZGd d„dƒZdS)é)Ú annotations)Ú TYPE_CHECKINGÚIterableÚLiteralÚcastN)ÚPositionalIndexer)Úcache_readonlyÚdoc)Ú
is_integerÚ is_list_like)Ú    DataFrameÚSeries)Úgroupbyc@s–eZdZdZeddœdd„ƒZdddœd    d
„Zd ddœd d „Zdddœdd„Zdddœdd„Z    dddœdd„Z
eddœdd„ƒZ eddœdd„ƒZ dS)ÚGroupByIndexingMixinz<
    Mixin for adding ._positional_selector to GroupBy.
    ÚGroupByPositionalSelector)ÚreturncCstrttj|ƒ}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
        )rrrÚGroupByr©ÚselfZ groupby_self©rúSd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/core/groupby/indexing.pyÚ_positional_selector$sNz)GroupByIndexingMixin._positional_selectorúPositionalIndexer | tuplez
np.ndarray©ÚargrcCs¼t|ƒrHtdd„tt|ƒDƒƒr6| ttt|ƒ¡}qŒ| tt|ƒ¡}nDt|t    ƒr^| 
|¡}n.t |ƒrx|  tt|ƒ¡}nt dt|ƒ›dƒ‚t|tƒr°|r¦|jdk}n
|jdk}ttj|ƒS)Ncss|]}t|ƒVqdS©N)r
)Ú.0ÚirrrÚ    <genexpr>szJGroupByIndexingMixin._make_mask_from_positional_indexer.<locals>.<genexpr>zInvalid index zE. Must be integer, list-like, slice or a tuple of integers and slicesr)r ÚallrrÚ_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ÚnpZndarray©rrÚmaskrrrÚ"_make_mask_from_positional_indexerzs 
 ÿ
 
z7GroupByIndexingMixin._make_mask_from_positional_indexerr!cCs&|dkr|j|kS|j| dkSdS)Nré)r+Ú_descending_count)rrrrrr'—s
z(GroupByIndexingMixin._make_mask_from_intz Iterable[int]zbool | np.ndarray)ÚargsrcCsPdd„|Dƒ}dd„|Dƒ}d}|r6|t |j|¡O}|rL|t |j|¡O}|S)NcSsg|]}|dkr|‘qS)rr©rrrrrÚ
<listcomp>žsz=GroupByIndexingMixin._make_mask_from_list.<locals>.<listcomp>cSsg|]}|dkr| d‘qS)rr0rr3rrrr4ŸsF)r,Úisinr+r1)rr2ZpositiveÚnegativer.rrrr sz)GroupByIndexingMixin._make_mask_from_listr#cCs^d}|D]P}t|ƒr*|| tt|ƒ¡O}qt|tƒrD|| |¡O}qtdt|ƒ›dƒ‚q|S)NFzInvalid argument z. Should be int or slice.)    r
r'rr!r$r%r&Ú
ValueErrorr))rr2r.rrrrr"«s
ÿz*GroupByIndexingMixin._make_mask_from_tupler%cCs*|j}|j}|j}|dk    r2|dkr2td|›dƒ‚d}|dkrBd}|dkrf|dkrò||j|dkM}nŒ|dkrœ||j|kM}|dkrò||j||dkM}nV||j| kM}|j|d}|j|j|ddk}t ||j|¡}|||dkM}|dk    r&|dkr||j|kM}n||j| kM}|S)Nrz Invalid step z. Must be non-negativeTr0)ÚstartÚstopÚstepr7r+r1r,Úwhere)rrr8r9r:r.Z offset_arrayZ limit_arrayrrrr&ºs6þ
 
z*GroupByIndexingMixin._make_mask_from_slicecCstrttj|ƒ}n|}| ¡Sr©rrrrZ_cumcount_arrayrrrrr+äsz%GroupByIndexingMixin._ascending_countcCs"trttj|ƒ}n|}|jddS)NF)Z    ascendingr<rrrrr1ísz&GroupByIndexingMixin._descending_countN) Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrr/r'r r"r&r+r1rrrrrsU*rc@s,eZdZdddœdd„Zdddœd    d
„Zd S) rúgroupby.GroupByÚNone©Úgroupby_objectrcCs
||_dSr©rD©rrDrrrÚ__init__ùsz"GroupByPositionalSelector.__init__rúDataFrame | SeriesrcCs|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.
        )rDr/Z_mask_selected_objr-rrrÚ __getitem__üs z%GroupByPositionalSelector.__getitem__N)r=r>r?rGrIrrrrr÷src@sDeZdZdZdddœdd„Zddd    d
d œd d „Zdd
dœdd„ZdS)ÚGroupByNthSelectorzO
    Dynamically substituted for GroupBy.nth to enable both call and index
    rArBrCcCs
||_dSrrErFrrrrG$szGroupByNthSelector.__init__NrzLiteral[('any', 'all', None)]rH)ÚnÚdropnarcCs|j ||¡Sr©rDZ_nth)rrKrLrrrÚ__call__'szGroupByNthSelector.__call__)rKrcCs |j |¡SrrM)rrKrrrrI.szGroupByNthSelector.__getitem__)N)r=r>r?r@rGrNrIrrrrrJs
ýrJ)Ú
__future__rÚtypingrrrrÚnumpyr,Zpandas._typingrZpandas.util._decoratorsrr    Zpandas.core.dtypes.commonr
r Zpandasr r Zpandas.core.groupbyrrrrrJrrrrÚ<module>s   Y'