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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
U
¬ý°d…Hã2@s²dZddlmZddlZddlmZmZddlZddlm    Z    ddl
Z
ddl m Z m Z mZmZmZmZmZmZmZmZddlZddlZddlmZddlmZmZmZmZm Z dd    l!m"Z"dd
l#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)m*Z*m+Z+dd l,m-Z-dd l.m/Z/e rddl0m1Z1dd„Z2dd„Z3dddœdd„Z4dd„Z5dd„Z6ddœdd„Z7ddœdd „Z8ddœd!d"„Z9ddœd#d$„Z:d%dœd&d'„Z;ed†d)d*d+d,œd-d.„ƒZ<ed‡d/d*d0d,œd1d.„ƒZ<dˆd/d*d0d,œd2d.„Z<d‰d3d*d+d4œd5d6„Z=d7d8„Z>d9d:d;œd<d=„Z?ddœd>d?„Z@ddœd@dA„ZAdBdœdCdD„ZBd%ddEœdFdG„ZCdHdI„ZDdJdK„ZEdLdM„ZFedNdNdOœdPdQ„ƒZGedRdSdOœdTdQ„ƒZGdŠdUdVœdWdQ„ZGdXdYdZœd[d\„ZHd]d^„ZId_d`daœdbdc„ZJejKdddedfœdgdh„ƒZLdidjdkœdldm„ZMejNejNejOejPjQejRejSjQiZTejNdnejOdoejRdpejUdqejVdrejNdnejWdnejXdsejYdsejZdtej[dtej\duej]duej^dvej_dvej`dwejadwejOdoejbdoejRdpejcdpejddxejedxejfdyejgdyiZhdzd{d|œd}d~„Zidd€„Zjdd‚dƒœd„d…„ZkdS)‹zh
Misc tools for implementing data structures
 
Note: pandas.core.common is *not* part of the public API.
é)Ú annotationsN)ÚabcÚ defaultdict)Úpartial)
Ú TYPE_CHECKINGÚAnyÚCallableÚ
CollectionÚ    GeneratorÚHashableÚIterableÚSequenceÚcastÚoverload)Úlib)Ú AnyArrayLikeÚ    ArrayLikeÚNpDtypeÚ RandomStateÚT)Ú'construct_1d_object_array_from_listlike)Ú is_array_likeÚ is_bool_dtypeÚis_extension_array_dtypeÚ
is_integer)ÚABCExtensionArrayÚABCIndexÚ    ABCSeries)Úiterable_not_string)Úisna)ÚIndexccs,|D]"}t|ƒr t|ƒEdHq|VqdS)a
    Flatten an arbitrarily nested sequence.
 
    Parameters
    ----------
    line : sequence
        The non string sequence to flatten
 
    Notes
    -----
    This doesn't consider strings sequences.
 
    Returns
    -------
    flattened : generator
    N)rÚflatten)ÚlineÚelement©r$úId:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/core/common.pyr!<sr!c    CsL|dj}|dd…D]0}z|j|kr*d}Wqtk
rDd}YqXq|S)Nré)ÚnameÚ
ValueError)Úobjsr'Úobjr$r$r%Úconsensus_name_attrTs
 
 r+rÚbool)ÚkeyÚreturncCsºt|ttjtfƒs$t|ƒr‚t|jƒr‚|jtjkrrt     |¡}t
  |¡snd}t
  |¡dkrjt |ƒ ¡rjt|ƒ‚dSdSt|jƒr¶dSn4t|tƒr¶t|ƒdkr¶t|ƒtk    r¬t|ƒ}t
 |¡SdS)aË
    Check whether `key` is a valid boolean indexer.
 
    Parameters
    ----------
    key : Any
        Only list-likes may be considered boolean indexers.
        All other types are not considered a boolean indexer.
        For array-like input, boolean ndarrays or ExtensionArrays
        with ``_is_boolean`` set are considered boolean indexers.
 
    Returns
    -------
    bool
        Whether `key` is a valid boolean indexer.
 
    Raises
    ------
    ValueError
        When the array is an object-dtype ndarray or ExtensionArray
        and contains missing values.
 
    See Also
    --------
    check_array_indexer : Check that `key` is a valid array to index,
        and convert to an ndarray.
    z=Cannot mask with non-boolean array containing NA / NaN valuesÚbooleanFTr)Ú
isinstancerÚnpÚndarrayrrrÚdtypeÚobject_ÚasarrayrZ is_bool_arrayZ infer_dtyperÚanyr(rÚlistÚlenÚtypeZ is_bool_list)r-Z    key_arrayZna_msgr$r$r%Úis_bool_indexer_s*ÿÿ 
 
 
 
 
r:cCst |¡r| ¡rtdƒ‚|S)z®
    Disallow indexing with a float key, even if that key is a round number.
 
    Parameters
    ----------
    val : scalar
 
    Returns
    -------
    outval : scalar
    zYIndexing with a float is no longer supported. Manually convert to an integer key instead.)rZis_floatrÚ
IndexError)Úvalr$r$r%Úcast_scalar_indexer–s
þr=cGsdd„|DƒS)zL
    Returns a generator consisting of the arguments that are not None.
    css|]}|dk    r|VqdS©Nr$©Ú.0Úargr$r$r%Ú    <genexpr>°sznot_none.<locals>.<genexpr>r$©Úargsr$r$r%Únot_none¬srE)r.cGstdd„|DƒƒS)z?
    Returns a boolean indicating if any argument is None.
    css|]}|dkVqdSr>r$r?r$r$r%rB·szany_none.<locals>.<genexpr>©r6rCr$r$r%Úany_none³srGcGstdd„|DƒƒS)zA
    Returns a boolean indicating if all arguments are None.
    css|]}|dkVqdSr>r$r?r$r$r%rB¾szall_none.<locals>.<genexpr>©ÚallrCr$r$r%Úall_noneºsrJcGstdd„|DƒƒS)zC
    Returns a boolean indicating if any argument is not None.
    css|]}|dk    VqdSr>r$r?r$r$r%rBÅszany_not_none.<locals>.<genexpr>rFrCr$r$r%Ú any_not_noneÁsrKcGstdd„|DƒƒS)zE
    Returns a boolean indicating if all arguments are not None.
    css|]}|dk    VqdSr>r$r?r$r$r%rBÌszall_not_none.<locals>.<genexpr>rHrCr$r$r%Ú all_not_noneÈsrLÚintcGstdd„|DƒƒS)z;
    Returns the count of arguments that are not None.
    css|]}|dk    VqdSr>r$©r@Úxr$r$r%rBÓsz!count_not_none.<locals>.<genexpr>)ÚsumrCr$r$r%Úcount_not_noneÏsrQ.zArrayLike | list | tuple | zipzNpDtype | Nonez
np.ndarray)Úvaluesr3r.cCsdSr>r$©rRr3r$r$r%Úasarray_tuplesafeÖsrTr rcCsdSr>r$rSr$r$r%rTàsc    Csät|ttfƒs"t|dƒs"t|ƒ}nt|tƒr2|jSt|tƒrR|tjtfkrRt    |ƒSz4t
  ¡"t
  dtj ¡tj||d}W5QRXWntk
r¢t    |ƒYSXt|jjtƒrÀtj|td}|jdkràdd„|Dƒ}t    |ƒ}|S)NZ    __array__Úignore©r3écSsg|] }t|ƒ‘qSr$)ÚtuplerNr$r$r%Ú
<listcomp>ÿsz%asarray_tuplesafe.<locals>.<listcomp>)r0r7rXÚhasattrrZ_valuesr1r4ÚobjectrÚwarningsÚcatch_warningsÚ simplefilterZVisibleDeprecationWarningr5r(Ú
issubclassr3r9ÚstrÚndim)rRr3Úresultr$r$r%rTås$
 
 
 
znp.ndarray | Iterable)Úlabelsr3r.cCs\t|ttfƒr|g}t|ttjfƒsLz t|ƒ}Wntk
rJ|g}YnXt||d}|S)zë
    Transform label or iterable of labels to array, for use in Index.
 
    Parameters
    ----------
    dtype : dtype
        If specified, use as dtype of the resulting array, otherwise infer.
 
    Returns
    -------
    array
    rV)r0r`rXr7r1r2Ú    TypeErrorrT)rcr3r$r$r%Úindex_labels_to_arrays   recCs |dk    rt|ttfƒs|gS|Sr>)r0rXr7©r*r$r$r%Úmaybe_make_list"srgzIterable[T] | TzCollection[T] | T)r*r.cCs.t|tjƒr t|tjƒs t|ƒStt|ƒ}|S)zB
    If obj is Iterable but not list-like, consume into list.
    )r0rr ÚSizedr7rr    rfr$r$r%Úmaybe_iterable_to_list(s
ricCs(t|tƒo&|jdko&|jdko&|jdkS)z
    We have a null slice.
    N©r0ÚsliceÚstartÚstopÚsteprfr$r$r%Ú is_null_slice2s
ÿþürocCs*t|tƒo(|jdk    o(|jdk    o(|j|jkS)z>
    We have an empty slice, e.g. no values are selected.
    N)r0rkrlrmrfr$r$r%Úis_empty_slice>s
ÿþ
ürpz
list[bool]cCsdd„|DƒS)zX
    Find non-trivial slices in "line": return a list of booleans with same length.
    cSs g|]}t|tƒot|ƒ ‘qSr$)r0rkro)r@Úkr$r$r%rYNsz"is_true_slices.<locals>.<listcomp>r$)r"r$r$r%Úis_true_slicesJsrr)r"r.cCs(t|tƒo&|jdko&|j|ko&|jdkS)z&
    We have a full length slice.
    rNrj)r*r"r$r$r%Ú is_full_sliceRs
ÿþürscCs>t|dƒrt|dƒSt|tƒr(t|jƒSt|ƒr:t|ƒjSdS)NÚ__name__)    rZÚgetattrr0rÚget_callable_nameÚfuncÚcallabler9rtrfr$r$r%rv^s
 
 
 
 
rvcKst|ƒr||f|ŽS|S)zØ
    Evaluate possibly callable input using obj and kwargs if it is callable,
    otherwise return as it is.
 
    Parameters
    ----------
    maybe_callable : possibly a callable
    obj : NDFrame
    **kwargs
    )rx)Zmaybe_callabler*Úkwargsr$r$r%Úapply_if_callableos  rzcCsVt |¡s(t|tƒr tt|jƒSt|ƒ}t|tj    ƒsBt
d|›ƒ‚|tkrRt
dƒ‚|S)a
    Helper function to standardize a supplied mapping.
 
    Parameters
    ----------
    into : instance or subclass of collections.abc.Mapping
        Must be a class, an initialized collections.defaultdict,
        or an instance of a collections.abc.Mapping subclass.
 
    Returns
    -------
    mapping : a collections.abc.Mapping subclass or other constructor
        a callable object that can accept an iterator to create
        the desired Mapping.
 
    See Also
    --------
    DataFrame.to_dict
    Series.to_dict
    zunsupported type: z/to_dict() only accepts initialized defaultdicts) ÚinspectÚisclassr0rrÚdefault_factoryr9r_rÚMappingrd)Zintor$r$r%Ústandardize_mapping€s
 
  rznp.random.Generator)Ústater.cCsdSr>r$©r€r$r$r%Ú random_state sr‚zGint | ArrayLike | np.random.BitGenerator | np.random.RandomState | Noneznp.random.RandomStatecCsdSr>r$rr$r$r%r‚¥szRandomState | NonercCsht|ƒst|ƒst|tjjƒr*tj |¡St|tjjƒr<|St|tjjƒrN|S|dkr\tjStdƒ‚dS)aþ
    Helper function for processing random_state arguments.
 
    Parameters
    ----------
    state : int, array-like, BitGenerator, Generator, np.random.RandomState, None.
        If receives an int, array-like, or BitGenerator, passes to
        np.random.RandomState() as seed.
        If receives an np.random RandomState or Generator, just returns that unchanged.
        If receives `None`, returns np.random.
        If receives anything else, raises an informative ValueError.
 
        .. versionchanged:: 1.1.0
 
            array-like and BitGenerator object now passed to np.random.RandomState()
            as seed
 
        Default None.
 
    Returns
    -------
    np.random.RandomState or np.random.Generator. If state is None, returns np.random
 
    Nzdrandom_state must be an integer, array-like, a BitGenerator, Generator, a numpy RandomState, or None)    rrr0r1ÚrandomZ BitGeneratorrr
r(rr$r$r%r‚¬s ÿþ ý ÿz/Callable[..., T] | tuple[Callable[..., T], str]r)rwr.cOsRt|tƒr>|\}}||kr,|›d}t|ƒ‚|||<|||ŽS||f|ž|ŽSdS)as
    Apply a function ``func`` to object ``obj`` either by passing obj as the
    first argument to the function or, in the case that the func is a tuple,
    interpret the first element of the tuple as a function and pass the obj to
    that function as a keyword argument whose key is the value of the second
    element of the tuple.
 
    Parameters
    ----------
    func : callable or tuple of (callable, str)
        Function to apply to this object or, alternatively, a
        ``(callable, data_keyword)`` tuple where ``data_keyword`` is a
        string indicating the keyword of ``callable`` that expects the
        object.
    *args : iterable, optional
        Positional arguments passed into ``func``.
    **kwargs : dict, optional
        A dictionary of keyword arguments passed into ``func``.
 
    Returns
    -------
    object : the return type of ``func``.
    z/ is both the pipe target and a keyword argumentN)r0rXr()r*rwrDryÚtargetÚmsgr$r$r%Úpipeäs
 
 
r†cs$‡fdd„}tˆtjtfƒr |SˆS)zv
    Returns a function that will map names/labels, dependent if mapper
    is a dict, Series or just a function.
    cs|ˆkrˆ|S|SdSr>r$)rO©Úmapperr$r%Úfszget_rename_function.<locals>.f)r0rr~r)rˆr‰r$r‡r%Úget_rename_function    s rŠz"Hashable | Iterable | AnyArrayLikezlist | AnyArrayLike)rRr.cCs>t|ttjtttfƒr|St|tjƒr8t|t    ƒs8t|ƒS|gS)z¤
    Convert list-like or scalar input to list-like. List, numpy and pandas array-like
    inputs are returned unmodified whereas others are converted to list.
    )
r0r7r1r2rrrrr r`)rRr$r$r%Úconvert_to_list_likes
r‹r`zGenerator[(None, None, None)])Úattrr.c
cs4t||ƒ}t|||ƒz
|VW5t|||ƒXdS)zøTemporarily set attribute on an object.
 
    Args:
        obj: Object whose attribute will be modified.
        attr: Attribute to modify.
        value: Value to temporarily set attribute to.
 
    Yields:
        obj with modified attribute.
    N)ruÚsetattr)r*rŒÚvalueÚ    old_valuer$r$r%Ú temp_setattr's
 
 
rr ÚNone)Úindexr.cCs2t|ƒt|ƒkr.tdt|ƒ›dt|ƒ›dƒ‚dS)zC
    Check the length of data matches the length of the index.
    zLength of values (z") does not match length of index (ú)N)r8r()Údatar’r$r$r%Úrequire_length_match;sÿr•rPÚmaxÚminrIr6ÚmeanÚprodÚstdÚvarÚmedianÚcumprodÚcumsumrz
str | None)rAr.cCs
t |¡S)zH
    if we define an internal function for this argument, return it
    )Ú _cython_tableÚget©rAr$r$r%Úget_cython_funcnsr¢cCs t ||¡S)zd
    if we define a builtin function for this argument, return it,
    otherwise return the arg
    )Ú_builtin_tabler r¡r$r$r%Úis_builtin_funcusr¤zSequence[Hashable | None]zlist[Hashable])Únamesr.cCsdd„t|ƒDƒS)a,
    If a name is missing then replace it by level_n, where n is the count
 
    .. versionadded:: 1.4.0
 
    Parameters
    ----------
    names : list-like
        list of column names or None values.
 
    Returns
    -------
    list
        list of column names with the None values replaced.
    cSs&g|]\}}|dkrd|›n|‘qS)NZlevel_r$)r@Úir'r$r$r%rYsz&fill_missing_names.<locals>.<listcomp>)Ú    enumerate)r¥r$r$r%Úfill_missing_names}sr¨).).)N)N)N)lÚ__doc__Ú
__future__rÚbuiltinsÚ collectionsrrÚ
contextlibÚ    functoolsrr{Útypingrrrr    r
r r r rrr\Únumpyr1Z pandas._libsrZpandas._typingrrrrrZpandas.core.dtypes.castrZpandas.core.dtypes.commonrrrrZpandas.core.dtypes.genericrrrZpandas.core.dtypes.inferencerZpandas.core.dtypes.missingrZpandasr r!r+r:r=rErGrJrKrLrQrTrergrirorprrrsrvrzrr‚r†rŠr‹Úcontextmanagerrr•rPr–ÚmaximumÚreducer—Zminimumr£rIr6Znansumr˜Znanmeanr™ZnanprodršZnanstdr›ZnanvarrœZ    nanmedianZnanmaxZnanminrZ
nancumprodržZ    nancumsumrŸr¢r¤r¨r$r$r$r%Ú<module>sê  0       7ÿ    !ÿ
    8%ýç