zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
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
U
¬ý°d?(ã@sàdZddlmZddlmZmZddlZddlmZddl    m
Z
Gdd„dƒZ Gd    d
„d
ƒZ d d d „dfddddddœdd„Z Gdd„dƒZeddddd„ƒZeedddd„ƒZeed dd!d"„ƒZeed#dd$d%„ƒZdS)&z†
 
accessor.py contains base classes for implementing accessor properties
that can be mixed into or pinned onto other pandas classes.
 
é)Ú annotations)ÚCallableÚfinalN)Údoc)Úfind_stack_levelcs`eZdZUeƒZded<eƒZded<eddœdd„ƒZ    ddœdd    „Z
d
dœ‡fd d „ Z ‡Z S) Ú DirNamesMixinzset[str]Ú
_accessorszfrozenset[str]Ú _hidden_attrs)ÚreturncCs |j|jBS)z:
        Delete unwanted __dir__ for this object.
        )rr    ©Úself©r úKd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/core/accessor.pyÚ_dir_deletionsszDirNamesMixin._dir_deletionscs‡fdd„ˆjDƒS)z9
        Add additional __dir__ for this object.
        csh|]}tˆ|ƒr|’qSr )Úhasattr)Ú.0Úaccessorr r rÚ    <setcomp>"s
z/DirNamesMixin._dir_additions.<locals>.<setcomp>)rr r r rÚ_dir_additionsszDirNamesMixin._dir_additionsú    list[str]cs*ttƒ ¡ƒ}|| ¡| ¡B}t|ƒS)z€
        Provide method name lookup and completion.
 
        Notes
        -----
        Only provide 'public' methods.
        )ÚsetÚsuperÚ__dir__rrÚsorted)r Úrv©Ú    __class__r rr$szDirNamesMixin.__dir__) Ú__name__Ú
__module__Ú __qualname__rrÚ__annotations__Ú    frozensetr    rrrrÚ __classcell__r r rrrs
rc    @sPeZdZdZdd„Zdd„Zdd„Zedd    d
„d fd d dddddœdd„ƒZdS)ÚPandasDelegatez@
    Abstract base class for delegating methods/properties.
    cOstd|›ƒ‚dS)NzYou cannot access the property ©Ú    TypeError©r ÚnameÚargsÚkwargsr r rÚ_delegate_property_get6sz%PandasDelegate._delegate_property_getcOstd|›dƒ‚dS)Nz The property z cannot be setr$)r r'Úvaluer(r)r r rÚ_delegate_property_set9sz%PandasDelegate._delegate_property_setcOstd|›ƒ‚dS)NzYou cannot call method r$r&r r rÚ_delegate_method<szPandasDelegate._delegate_methodFcCs|S©Nr ©Úxr r rÚ<lambda>FózPandasDelegate.<lambda>TrÚstrÚboolúCallable[[str], str]ÚNone)Ú    accessorsÚtypÚ    overwriteÚaccessor_mappingÚraise_on_missingr
c sx‡‡fdd„}‡‡fdd„}|D]R}    |s>tˆˆ|    ƒdƒdkr>q |dkrP||    ƒ}
n||    ƒ}
|sft||    ƒs t||    |
ƒq dS)aí
        Add accessors to cls from the delegate class.
 
        Parameters
        ----------
        cls
            Class to add the methods/properties to.
        delegate
            Class to get methods/properties and doc-strings.
        accessors : list of str
            List of accessors to add.
        typ : {'property', 'method'}
        overwrite : bool, default False
            Overwrite the method/property in the target class if it exists.
        accessor_mapping: Callable, default lambda x: x
            Callable to map the delegate's function to the cls' function.
        raise_on_missing: bool, default True
            Raise if an accessor does not exist on delegate.
            False skips the missing accessor.
        cs>‡fdd„}‡fdd„}ˆ|_ˆ|_t||tˆˆˆƒƒjdS)Ncs
| ˆ¡Sr.)r*r ©r'r rÚ_getter_sz[PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._gettercs | ˆ|¡Sr.)r,)r Z
new_valuesr<r rÚ_setterbsz[PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._setter)ÚfgetÚfsetr)rÚpropertyÚgetattrÚ__doc__)r'r=r>©r:Údelegater<rÚ_create_delegator_property^s  ýzJPandasDelegate._add_delegate_accessors.<locals>._create_delegator_propertycs(‡fdd„}ˆ|_tˆˆˆƒƒj|_|S)Ncs|jˆf|ž|ŽSr.)r-)r r(r)r<r rÚfoszSPandasDelegate._add_delegate_accessors.<locals>._create_delegator_method.<locals>.f)rrBrC)r'rGrDr<rÚ_create_delegator_methodns zHPandasDelegate._add_delegate_accessors.<locals>._create_delegator_methodNrA)rBrÚsetattr) ÚclsrEr7r8r9r:r;rFrHr'rGr rDrÚ_add_delegate_accessors?s    ÿþ
z&PandasDelegate._add_delegate_accessorsN)    rrrrCr*r,r-Ú classmethodrKr r r rr#1sùr#FcCs|Sr.r r/r r rr1r2r1Trr3r4r5)r7r8r9r:r;cs‡‡‡‡‡‡fdd„}|S)a×
    Add delegated names to a class using a class decorator.  This provides
    an alternative usage to directly calling `_add_delegate_accessors`
    below a class definition.
 
    Parameters
    ----------
    delegate : object
        The class to get methods/properties & doc-strings.
    accessors : Sequence[str]
        List of accessor to add.
    typ : {'property', 'method'}
    overwrite : bool, default False
       Overwrite the method/property in the target class if it exists.
    accessor_mapping: Callable, default lambda x: x
        Callable to map the delegate's function to the cls' function.
    raise_on_missing: bool, default True
        Raise if an accessor does not exist on delegate.
        False skips the missing accessor.
 
    Returns
    -------
    callable
        A class decorator.
 
    Examples
    --------
    @delegate_names(Categorical, ["categories", "ordered"], "property")
    class CategoricalAccessor(PandasDelegate):
        [...]
    cs|jˆˆˆˆˆˆd|S)N)r9r:r;)rK)rJ©r:r7rEr9r;r8r rÚadd_delegate_accessors°súz.delegate_names.<locals>.add_delegate_accessorsr )rEr7r8r9r:r;rNr rMrÚdelegate_namesˆs( rOc@s(eZdZdZdddœdd„Zdd„Zd    S)
ÚCachedAccessora¡
    Custom property-like object.
 
    A descriptor for caching accessors.
 
    Parameters
    ----------
    name : str
        Namespace that will be accessed under, e.g. ``df.foo``.
    accessor : cls
        Class with the extension methods.
 
    Notes
    -----
    For accessor, The class's __init__ method assumes that one of
    ``Series``, ``DataFrame`` or ``Index`` as the
    single argument ``data``.
    r3r6)r'r
cCs||_||_dSr.)Ú_nameÚ    _accessor)r r'rr r rÚ__init__ØszCachedAccessor.__init__cCs,|dkr|jS| |¡}t ||j|¡|Sr.)rRÚobjectÚ __setattr__rQ)r ÚobjrJZ accessor_objr r rÚ__get__Üs
 
zCachedAccessor.__get__N)rrrrCrSrWr r r rrPÄsrPÚ)ÚklassZotherscs‡‡fdd„}|S)aG
    Register a custom accessor on {klass} objects.
 
    Parameters
    ----------
    name : str
        Name under which the accessor should be registered. A warning is issued
        if this name conflicts with a preexisting attribute.
 
    Returns
    -------
    callable
        A class decorator.
 
    See Also
    --------
    register_dataframe_accessor : Register a custom accessor on DataFrame objects.
    register_series_accessor : Register a custom accessor on Series objects.
    register_index_accessor : Register a custom accessor on Index objects.
 
    Notes
    -----
    When accessed, your accessor will be initialized with the pandas object
    the user is interacting with. So the signature must be
 
    .. code-block:: python
 
        def __init__(self, pandas_object):  # noqa: E999
            ...
 
    For consistency with pandas methods, you should raise an ``AttributeError``
    if the data passed to your accessor has an incorrect dtype.
 
    >>> pd.Series(['a', 'b']).dt
    Traceback (most recent call last):
    ...
    AttributeError: Can only use .dt accessor with datetimelike values
 
    Examples
    --------
    In your library code::
 
        import pandas as pd
 
        @pd.api.extensions.register_dataframe_accessor("geo")
        class GeoAccessor:
            def __init__(self, pandas_obj):
                self._obj = pandas_obj
 
            @property
            def center(self):
                # return the geographic center point of this DataFrame
                lat = self._obj.latitude
                lon = self._obj.longitude
                return (float(lon.mean()), float(lat.mean()))
 
            def plot(self):
                # plot this array's data on a map, e.g., using Cartopy
                pass
 
    Back in an interactive IPython session:
 
        .. code-block:: ipython
 
            In [1]: ds = pd.DataFrame({{"longitude": np.linspace(0, 10),
               ...:                    "latitude": np.linspace(0, 20)}})
            In [2]: ds.geo.center
            Out[2]: (5.0, 10.0)
            In [3]: ds.geo.plot()  # plots data on a map
    cs^tˆˆƒr<tjdt|ƒ›dtˆƒ›dtˆƒ›dttƒdtˆˆtˆ|ƒƒˆj     ˆ¡|S)Nzregistration of accessor z  under name z
 for type z: is overriding a preexisting attribute with the same name.)Ú
stacklevel)
rÚwarningsÚwarnÚreprÚ UserWarningrrIrPrÚadd)r©rJr'r rÚ    decorator2s
"û z%_register_accessor.<locals>.decoratorr )r'rJrar r`rÚ_register_accessorésI rbÚ    DataFrame)rYcCsddlm}t||ƒS)Nr)rc)Úpandasrcrb)r'rcr r rÚregister_dataframe_accessorBs reÚSeriescCsddlm}t||ƒS)Nr)rf)rdrfrb)r'rfr r rÚregister_series_accessorIs rgÚIndexcCsddlm}t||ƒS)Nr)rh)rdrhrb)r'rhr r rÚregister_index_accessorPs ri)rCÚ
__future__rÚtypingrrr[Zpandas.util._decoratorsrZpandas.util._exceptionsrrr#rOrPrbrergrir r r rÚ<module>s(   [ú<%
 
X