o
    �¨Êh<'  ã                   @  sÜ   d Z ddlmZ ddlmZmZ ddlZddlmZ ddl	m
Z
 G dd„ dƒZG d	d
„ d
ƒZddd„ dfd+dd„ZG dd„ dƒZeddd�d,dd „ƒZeed!d"�d,d#d$„ƒZeed%d"�d,d&d'„ƒZeed(d"�d,d)d*„ƒZdS )-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_levelc                      sT   e Zd ZU eƒ Zded< eƒ Zded< eddd„ƒZ	ddd	„Z
d‡ fdd„Z‡  ZS )ÚDirNamesMixinúset[str]Ú
_accessorszfrozenset[str]Ú_hidden_attrsÚreturnc                 C  s   | j | jB S )z:
        Delete unwanted __dir__ for this object.
        )r	   r
   ©Úself© r   úF/var/www/html/env/lib/python3.10/site-packages/pandas/core/accessor.pyÚ_dir_deletions   s   zDirNamesMixin._dir_deletionsc                   s   ‡ fdd„ˆ j D ƒS )z9
        Add additional __dir__ for this object.
        c                   s   h | ]	}t ˆ |ƒr|’qS r   )Úhasattr)Ú.0Úaccessorr   r   r   Ú	<setcomp>"   s    z/DirNamesMixin._dir_additions.<locals>.<setcomp>)r	   r   r   r   r   Ú_dir_additions   s   zDirNamesMixin._dir_additionsú	list[str]c                   s*   t tƒ  ¡ ƒ}||  ¡  |  ¡ B }t|ƒS )z€
        Provide method name lookup and completion.

        Notes
        -----
        Only provide 'public' methods.
        )ÚsetÚsuperÚ__dir__r   r   Úsorted)r   Úrv©Ú	__class__r   r   r   $   s   zDirNamesMixin.__dir__)r   r   )r   r   )Ú__name__Ú
__module__Ú__qualname__r   r	   Ú__annotations__Ú	frozensetr
   r   r   r   r   Ú__classcell__r   r   r   r   r      s   
 
r   c                   @  sH   e Zd ZdZddd„Zddd„Zddd	„Zed
dd„ dfddd„ƒZdS )ÚPandasDelegatez@
    Abstract base class for delegating methods/properties.
    ÚnameÚstrc                 O  ó   t d|› �ƒ‚)NzYou cannot access the property ©Ú	TypeError©r   r%   ÚargsÚkwargsr   r   r   Ú_delegate_property_get6   ó   z%PandasDelegate._delegate_property_getc                 O  s   t d|› d�ƒ‚)NzThe property z cannot be setr(   )r   r%   Úvaluer+   r,   r   r   r   Ú_delegate_property_set9   s   z%PandasDelegate._delegate_property_setc                 O  r'   )NzYou cannot call method r(   r*   r   r   r   Ú_delegate_method<   r.   zPandasDelegate._delegate_methodFc                 C  ó   | S ©Nr   ©Úxr   r   r   Ú<lambda>F   ó    zPandasDelegate.<lambda>TÚ	accessorsr   ÚtypÚ	overwriteÚboolÚaccessor_mappingúCallable[[str], str]Úraise_on_missingr   ÚNonec                   s|   d	‡ ‡fdd„}d	‡ ‡fdd„}|D ])}	|s!t ˆˆ |	ƒdƒdu r!q|dkr*||	ƒ}
n||	ƒ}
|s5t| |	ƒs;t| |	|
ƒ qdS )
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.
        r%   r&   c                   s>   ‡ fdd„}‡ fdd„}ˆ |_ ˆ |_ t||tˆˆˆ ƒƒjd�S )Nc                   s
   |   ˆ ¡S r3   )r-   r   ©r%   r   r   Ú_getter_   s   
z[PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._getterc                   s   |   ˆ |¡S r3   )r0   )r   Ú
new_valuesr@   r   r   Ú_setterb   s   z[PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._setter)ÚfgetÚfsetr   )r   ÚpropertyÚgetattrÚ__doc__)r%   rA   rC   ©r<   Údelegater@   r   Ú_create_delegator_property^   s   ýzJPandasDelegate._add_delegate_accessors.<locals>._create_delegator_propertyc                   s(   ‡ fdd„}ˆ |_ tˆˆˆ ƒƒj|_|S )Nc                   s   | j ˆ g|¢R i |¤ŽS r3   )r1   )r   r+   r,   r@   r   r   Úfo   s   zSPandasDelegate._add_delegate_accessors.<locals>._create_delegator_method.<locals>.f)r   rG   rH   )r%   rL   rI   r@   r   Ú_create_delegator_methodn   s   zHPandasDelegate._add_delegate_accessors.<locals>._create_delegator_methodNrF   ©r%   r&   )rG   r   Úsetattr)ÚclsrJ   r8   r9   r:   r<   r>   rK   rM   r%   rL   r   rI   r   Ú_add_delegate_accessors?   s   	ÿ
€òz&PandasDelegate._add_delegate_accessorsNrN   )r8   r   r9   r&   r:   r;   r<   r=   r>   r;   r   r?   )	r   r   r    rH   r-   r0   r1   ÚclassmethodrQ   r   r   r   r   r$   1   s    


ùr$   Fc                 C  r2   r3   r   r4   r   r   r   r6   �   r7   r6   Tr8   r   r9   r&   r:   r;   r<   r=   r>   c                   s   ‡ ‡‡‡‡‡fdd„}|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):
        [...]
    c                   s   | j ˆˆˆˆˆ ˆd� | S )N)r:   r<   r>   )rQ   )rP   ©r<   r8   rJ   r:   r>   r9   r   r   Úadd_delegate_accessors°   s   úz.delegate_names.<locals>.add_delegate_accessorsr   )rJ   r8   r9   r:   r<   r>   rT   r   rS   r   Údelegate_namesˆ   s   (rU   c                   @  s"   e Zd ZdZddd„Zdd	„ Zd
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``.
    r%   r&   r   r?   c                 C  s   || _ || _d S r3   )Ú_nameÚ	_accessor)r   r%   r   r   r   r   Ú__init__Ø   s   
zCachedAccessor.__init__c                 C  s,   |d u r| j S |   |¡}t || j|¡ |S r3   )rX   ÚobjectÚ__setattr__rW   )r   ÚobjrP   Úaccessor_objr   r   r   Ú__get__Ü   s
   
zCachedAccessor.__get__N)r%   r&   r   r?   )r   r   r    rH   rY   r^   r   r   r   r   rV   Ä   s    
rV   Ú )ÚklassÚothersr%   c                   s   ‡ ‡fdd„}|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
    c                   s^   t ˆ ˆƒrtjdt| ƒ› dtˆƒ› dtˆ ƒ› d�ttƒ d� tˆ ˆtˆ| ƒƒ ˆ j 	ˆ¡ | S )Nzregistration of accessor z under name z
 for type z: is overriding a preexisting attribute with the same name.)Ú
stacklevel)
r   ÚwarningsÚwarnÚreprÚUserWarningr   rO   rV   r	   Úadd)r   ©rP   r%   r   r   Ú	decorator2  s   
ÿÿûz%_register_accessor.<locals>.decoratorr   )r%   rP   ri   r   rh   r   Ú_register_accessoré   s   Irj   Ú	DataFrame)r`   c                 C  ó   ddl m} t| |ƒS )Nr   )rk   )Úpandasrk   rj   )r%   rk   r   r   r   Úregister_dataframe_accessorB  ó   
rn   ÚSeriesc                 C  rl   )Nr   )rp   )rm   rp   rj   )r%   rp   r   r   r   Úregister_series_accessorI  ro   rq   ÚIndexc                 C  rl   )Nr   )rr   )rm   rr   rj   )r%   rr   r   r   r   Úregister_index_accessorP  ro   rs   )
r8   r   r9   r&   r:   r;   r<   r=   r>   r;   rN   )rH   Ú
__future__r   Útypingr   r   rc   Úpandas.util._decoratorsr   Úpandas.util._exceptionsr   r   r$   rU   rV   rj   rn   rq   rs   r   r   r   r   Ú<module>   s*    [ú<
%
X

