zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
U
¬ý°d‘=ã
@sHUddlmZddlmZmZddlZddlmZ    ddl
m Z m Z m Z ddlmZmZddlmZmZddlmZmZmZdd    lmZmZdd
lmZddlmmm Z!dd lm"Z"m#Z#dd l$m%Z%m&Z&dd l'm(Z(e)e!j*ƒZ*de+d<e* ,ddi¡e&dddddddddg    eƒe&dddddd d!d"ged#d$Gd%d„de%ƒƒƒZ-dS)&é)Ú annotations)ÚAnyÚHashableN)Úindex)ÚDtypeÚDtypeObjÚnpt)Úcache_readonlyÚdoc)Úis_categorical_dtypeÚ    is_scalar)Úis_valid_na_for_dtypeÚisnaÚnotna)Ú CategoricalÚcontains)Ú extract_array)ÚIndexÚmaybe_extract_name)ÚNDArrayBackedExtensionIndexÚ inherit_names)Ú pprint_thingzdict[str, str]Ú_index_doc_kwargsZ target_klassÚCategoricalIndexZargsortÚtolistÚcodesÚ
categoriesÚorderedZ_reverse_indexerZ searchsortedÚminÚmaxZrename_categoriesZreorder_categoriesZadd_categoriesZremove_categoriesZremove_unused_categoriesZset_categoriesZ
as_orderedZ as_unorderedT)ÚwrapcsZeZdZUdZdZeZedd„ƒZe    ddœdd„ƒZ
d    e d
<d e d <d e d<de d<de d<eddœdd„ƒZ dCdddddœdd„Z ddœdd„Zdddœd d!„Zed"d#„ƒZ‡fd$d%„Zd&d'd&d(œd)d*„Zed'dœd+d,„ƒZeejƒd-dd.œd/d0„ƒZdDd1dœ‡fd2d3„ Zd4dœd5d6„Zddœd7d8„Zd9dd:œd;d<„Zd=d>„Zd?dd d@œdAdB„Z‡ZS)Era]
    Index based on an underlying :class:`Categorical`.
 
    CategoricalIndex, like Categorical, can only take on a limited,
    and usually fixed, number of possible values (`categories`). Also,
    like Categorical, it might have an order, but numerical operations
    (additions, divisions, ...) are not possible.
 
    Parameters
    ----------
    data : array-like (1-dimensional)
        The values of the categorical. If `categories` are given, values not in
        `categories` will be replaced with NaN.
    categories : index-like, optional
        The categories for the categorical. Items need to be unique.
        If the categories are not given here (and also not in `dtype`), they
        will be inferred from the `data`.
    ordered : bool, optional
        Whether or not this categorical is treated as an ordered
        categorical. If not given here or in `dtype`, the resulting
        categorical will be unordered.
    dtype : CategoricalDtype or "category", optional
        If :class:`CategoricalDtype`, cannot be used together with
        `categories` or `ordered`.
    copy : bool, default False
        Make a copy of input ndarray.
    name : object, optional
        Name to be stored in the index.
 
    Attributes
    ----------
    codes
    categories
    ordered
 
    Methods
    -------
    rename_categories
    reorder_categories
    add_categories
    remove_categories
    remove_unused_categories
    set_categories
    as_ordered
    as_unordered
    map
 
    Raises
    ------
    ValueError
        If the categories do not validate.
    TypeError
        If an explicit ``ordered=True`` is given but no `categories` and the
        `values` are not sortable.
 
    See Also
    --------
    Index : The base pandas Index type.
    Categorical : A categorical array.
    CategoricalDtype : Type for categorical data.
 
    Notes
    -----
    See the `user guide
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#categoricalindex>`__
    for more.
 
    Examples
    --------
    >>> pd.CategoricalIndex(["a", "b", "c", "a", "b", "c"])
    CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
                     categories=['a', 'b', 'c'], ordered=False, dtype='category')
 
    ``CategoricalIndex`` can also be instantiated from a ``Categorical``:
 
    >>> c = pd.Categorical(["a", "b", "c", "a", "b", "c"])
    >>> pd.CategoricalIndex(c)
    CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
                     categories=['a', 'b', 'c'], ordered=False, dtype='category')
 
    Ordered ``CategoricalIndex`` can have a min and max value.
 
    >>> ci = pd.CategoricalIndex(
    ...     ["a", "b", "c", "a", "b", "c"], ordered=True, categories=["c", "b", "a"]
    ... )
    >>> ci
    CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c'],
                     categories=['c', 'b', 'a'], ordered=True, dtype='category')
    >>> ci.min()
    'c'
    ZcategoricalindexcCs|jjS©N)rÚ_can_hold_strings©Úself©r%úSd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/core/indexes/category.pyr"°sz"CategoricalIndex._can_hold_stringsÚbool)ÚreturncCs|jjSr!)rÚ_should_fallback_to_positionalr#r%r%r&r)´sz/CategoricalIndex._should_fallback_to_positionalz
np.ndarrayrrrz bool | NonerrÚ_dataÚ_valuesztype[libindex.IndexEngine]cCs.tjtjtjtjtjtjtjtj    i|j
j j Sr!) ÚnpZint8ÚlibindexZ
Int8EngineÚint16Z Int16EngineÚint32Z Int32EngineÚint64Z Int64EnginerÚdtypeÚtyper#r%r%r&Ú _engine_type¾süûzCategoricalIndex._engine_typeNFz Dtype | Noner)r1ÚcopyÚnamer(cCs>t|||ƒ}t|ƒr| |¡t|||||d}|j||dS)N)rrr1r4©r5)rr Z_raise_scalar_data_errorrÚ _simple_new)ÚclsÚdatarrr1r4r5r%r%r&Ú__new__Ìs     
ÿzCategoricalIndex.__new__cCs’t|ƒr$t|ƒ}| |¡sŽtdƒ‚nj|jr4tdƒ‚nZ|}t||jd}t|ƒ}| |¡     ¡sdtdƒ‚|j
}||kt |ƒt |ƒ@B     ¡sŽtdƒ‚|S)a\
        *this is an internal non-public method*
 
        provide a comparison between the dtype of self and other (coercing if
        needed)
 
        Parameters
        ----------
        other : Index
 
        Returns
        -------
        Categorical
 
        Raises
        ------
        TypeError if the dtypes are not compatible
        z8categories must match existing categories when appendingz8MultiIndex is not dtype-compatible with CategoricalIndex)r1z7cannot append a non-category item to a CategoricalIndex) r rZ#_categories_match_up_to_permutationÚ    TypeErrorZ    _is_multirr1rÚisinÚallr+r)r$ÚotherÚvaluesÚcatr%r%r&Ú_is_dtype_compatãs*
ÿ
ÿÿz!CategoricalIndex._is_dtype_compatÚobject)r>r(c    CsR| |¡rdSt|tƒsdSz| |¡}Wnttfk
rDYdSX|j |¡S)zç
        Determine if two CategoricalIndex objects contain the same elements.
 
        Returns
        -------
        bool
            If two CategoricalIndex objects have equal elements True,
            otherwise False.
        TF)Úis_Ú
isinstancerrAr;Ú
ValueErrorr*Úequals)r$r>r%r%r&rFs
 
 
zCategoricalIndex.equalscCs|jjSr!)rÚ_formatter_funcr#r%r%r&rG-sz CategoricalIndex._formatter_funccs8ddd |j ¡¡›dfd|jfg}tƒ ¡}||S)zG
        Return a list of tuples of the (attr,formatted_value)
        rú[z, ú]r)Újoinr*Z_repr_categoriesrÚsuperÚ _format_attrs)r$ÚattrsÚextra©Ú    __class__r%r&rL1sþû
zCategoricalIndex._format_attrsz    list[str]Ústr)ÚheaderÚna_repr(cs‡fdd„|jDƒ}||S)Ncs$g|]}t|ƒrt|ddnˆ‘qS))ú    ú Ú
)Z escape_chars)rr©Ú.0Úx©rSr%r&Ú
<listcomp>Bsÿz8CategoricalIndex._format_with_header.<locals>.<listcomp>©r+)r$rRrSÚresultr%rZr&Ú_format_with_headerAs
þz$CategoricalIndex._format_with_headercCsdS)NZ categoricalr%r#r%r%r&Ú inferred_typeJszCategoricalIndex.inferred_typer)Úkeyr(cCs$t||jjƒr|jSt|||jdS)N)Ú    container)r rr1ZhasnansrZ_engine©r$r`r%r%r&Ú __contains__NszCategoricalIndex.__contains__z)tuple[Index, npt.NDArray[np.intp] | None]cs<|dk    rtdƒ‚|dk    r tdƒ‚|dk    r0tdƒ‚tƒ |¡S)a
        Create index with target's values (move/add/delete values as necessary)
 
        Returns
        -------
        new_index : pd.Index
            Resulting index
        indexer : np.ndarray[np.intp] or None
            Indices of output values in original index
 
        Nz?argument method is not implemented for CategoricalIndex.reindexz>argument level is not implemented for CategoricalIndex.reindexz>argument limit is not implemented for CategoricalIndex.reindex)ÚNotImplementedErrorrKÚreindex)r$ÚtargetÚmethodÚlevelÚlimitZ    tolerancerOr%r&reVsÿÿÿzCategoricalIndex.reindexÚintcCs>z|j |¡WStk
r8t||jjƒr2YdS‚YnXdS)Néÿÿÿÿ)r*Z _unbox_scalarÚKeyErrorr rr1rbr%r%r&Ú_maybe_cast_indexerus z$CategoricalIndex._maybe_cast_indexercCsft|tƒr|j}t|tƒr.|j |¡}|j}n*|j |¡}|j|j    j
dd}|j  |¡}t |ƒ  |¡S)NF)r4)rDrr*rZ_encode_with_my_categoriesÚ_codesrZ get_indexerZastyperr1Z_from_backing_datar2r7)r$r?r@rr%r%r&Ú_maybe_cast_listlike_indexers
 
   z-CategoricalIndex._maybe_cast_listlike_indexerr)r1r(cCs |j |¡Sr!)rÚ_is_comparable_dtype)r$r1r%r%r&rpsz%CategoricalIndex._is_comparable_dtypecCs|j |¡}t||jdS)aJ
 
        Map values using input an input mapping or function.
 
        Maps the values (their categories, not the codes) of the index to new
        categories. If the mapping correspondence is one-to-one the result is a
        :class:`~pandas.CategoricalIndex` which has the same order property as
        the original, otherwise an :class:`~pandas.Index` is returned.
 
        If a `dict` or :class:`~pandas.Series` is used any unmapped category is
        mapped to `NaN`. Note that if this happens an :class:`~pandas.Index`
        will be returned.
 
        Parameters
        ----------
        mapper : function, dict, or Series
            Mapping correspondence.
 
        Returns
        -------
        pandas.CategoricalIndex or pandas.Index
            Mapped index.
 
        See Also
        --------
        Index.map : Apply a mapping correspondence on an
            :class:`~pandas.Index`.
        Series.map : Apply a mapping correspondence on a
            :class:`~pandas.Series`.
        Series.apply : Apply more complex functions on a
            :class:`~pandas.Series`.
 
        Examples
        --------
        >>> idx = pd.CategoricalIndex(['a', 'b', 'c'])
        >>> idx
        CategoricalIndex(['a', 'b', 'c'], categories=['a', 'b', 'c'],
                          ordered=False, dtype='category')
        >>> idx.map(lambda x: x.upper())
        CategoricalIndex(['A', 'B', 'C'], categories=['A', 'B', 'C'],
                         ordered=False, dtype='category')
        >>> idx.map({'a': 'first', 'b': 'second', 'c': 'third'})
        CategoricalIndex(['first', 'second', 'third'], categories=['first',
                         'second', 'third'], ordered=False, dtype='category')
 
        If the mapping is one-to-one the ordering of the categories is
        preserved:
 
        >>> idx = pd.CategoricalIndex(['a', 'b', 'c'], ordered=True)
        >>> idx
        CategoricalIndex(['a', 'b', 'c'], categories=['a', 'b', 'c'],
                         ordered=True, dtype='category')
        >>> idx.map({'a': 3, 'b': 2, 'c': 1})
        CategoricalIndex([3, 2, 1], categories=[3, 2, 1], ordered=True,
                         dtype='category')
 
        If the mapping is not one-to-one an :class:`~pandas.Index` is returned:
 
        >>> idx.map({'a': 'first', 'b': 'second', 'c': 'first'})
        Index(['first', 'second', 'first'], dtype='object')
 
        If a `dict` is used, all unmapped categories are mapped to `NaN` and
        the result is an :class:`~pandas.Index`:
 
        >>> idx.map({'a': 'first', 'b': 'second'})
        Index(['first', 'second', nan], dtype='object')
        r6)r+Úmaprr5)r$ZmapperZmappedr%r%r&rq“sC zCategoricalIndex.mapz list[Index])Ú    to_concatr5r(csrzt ‡fdd„|Dƒ¡}Wn>tk
rZddlm}|dd„|Dƒƒ}t||dYSXtˆƒj||dSdS)Ncsg|]}ˆ |¡‘qSr%)rA)rXÚcr#r%r&r[Ýsz,CategoricalIndex._concat.<locals>.<listcomp>r)Ú concat_compatcSsg|]
}|j‘qSr%r\rWr%r%r&r[ãsr6)rZ_concat_same_typer;Zpandas.core.dtypes.concatrtrr2r7)r$rrr5r@rtÚresr%r#r&Ú_concatÙsÿ zCategoricalIndex._concat)NNNNFN)NNNN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__Z_typrZ    _data_clsÚpropertyr"r    r)Ú__annotations__r3r:rArFrGrLr^r_r
rrcrermrorprqrvÚ __classcell__r%r%rOr&r4sR
\
ù0
     ÿ
F).Ú
__future__rÚtypingrrÚnumpyr,Z pandas._libsrr-Zpandas._typingrrrZpandas.util._decoratorsr    r
Zpandas.core.dtypes.commonr r Zpandas.core.dtypes.missingr rrZpandas.core.arrays.categoricalrrZpandas.core.constructionrZpandas.core.indexes.baseÚcoreZindexesÚbaseZibaserrZpandas.core.indexes.extensionrrZpandas.io.formats.printingrÚdictrr|Úupdaterr%r%r%r&Ú<module>sV    ÷ ôø
ô