
    iC                    ^   d Z ddlmZ ddlZddlmZmZ ddlZddlm	Z	 ddl
mZ erddlmZ ddlmZ dd	lmZ dd
lmZ  G d d      Z G d d      Zdd df	 	 	 	 	 	 	 	 	 ddZ G d d      ZeZ	 	 	 	 	 	 ddZdZ e	d      dd       ZdZ e	d      dd       ZdZ e	d      dd       Zy) z

accessor.py contains base classes for implementing accessor properties
that can be mixed into or pinned onto other pandas classes.

    )annotationsN)TYPE_CHECKINGfinal)
set_module)find_stack_level)Callable)TypeTIndex)NDFramec                  l     e Zd ZU  e       Zded<    e       Zded<   edd       Z	ddZ
d	 fdZ xZS )
DirNamesMixinset[str]
_accessorszfrozenset[str]_hidden_attrsc                4    | j                   | j                  z  S )z:
        Delete unwanted __dir__ for this object.
        )r   r   )selfs    Q/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pandas/core/accessor.py_dir_deletionszDirNamesMixin._dir_deletions#   s    
 !3!333    c                Z    | j                   D ch c]  }t        | |      s| c}S c c}w )z9
        Add additional __dir__ for this object.
        )r   hasattr)r   accessors     r   _dir_additionszDirNamesMixin._dir_additions*   s%     *.TXGD(<STTTs   ((c                    t        t        | 	               }|| j                         z
  | j	                         z  }t        |      S )z
        Provide method name lookup and completion.

        Notes
        -----
        Only provide 'public' methods.
        )setsuper__dir__r   r   sorted)r   rv	__class__s     r   r   zDirNamesMixin.__dir__0   sA     "#4&&((D,?,?,AAbzr   )returnr   )r"   	list[str])__name__
__module____qualname__r   r   __annotations__	frozensetr   r   r   r   r   __classcell__)r!   s   @r   r   r      s=    5J $-KM>/
4 4U
 
r   r   c                  Z    e Zd ZdZd
dZddZd
dZedd df	 	 	 	 	 	 	 	 	 	 	 dd       Zy	)PandasDelegatez@
    Abstract base class for delegating methods/properties.
    c                    t        d|       )NzYou cannot access the property 	TypeErrorr   nameargskwargss       r   _delegate_property_getz%PandasDelegate._delegate_property_getB   s    9$@AAr   c                     t        d| d      )NzThe property z cannot be setr-   )r   r0   valuer1   r2   s        r   _delegate_property_setz%PandasDelegate._delegate_property_setE   s    -v^<==r   c                    t        d|       )NzYou cannot call method r-   r/   s       r   _delegate_methodzPandasDelegate._delegate_methodH   s    1$899r   Fc                    | S N xs    r   <lambda>zPandasDelegate.<lambda>R   s    1 r   Tc                    dfd}dfd}|D ]J  }	|st         |	      d      |dk(  r	 ||	      }
n ||	      }
|st        | |	      r>t        | |	|
       L y)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 docstrings.
        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.
        c           
           fd} fd} |_          |_         t        ||t                      j                        S )Nc                &    | j                        S r:   )r3   )r   r0   s    r   _getterz[PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._getterk   s    22488r   c                (    | j                  |      S r:   )r6   )r   
new_valuesr0   s     r   _setterz[PandasDelegate._add_delegate_accessors.<locals>._create_delegator_property.<locals>._settern   s    224DDr   )fgetfsetdoc)r$   propertygetattr__doc__)r0   rB   rE   accessor_mappingdelegates   `  r   _create_delegator_propertyzJPandasDelegate._add_delegate_accessors.<locals>._create_delegator_propertyj   sI    9E  $G#GH&6t&<=EE r   c                h     t                      }t        j                  |       fd       }|S )Nc                0     | j                   g|i |S r:   )r8   )r   r1   r2   r0   s      r   fzSPandasDelegate._add_delegate_accessors.<locals>._create_delegator_method.<locals>.f}   s     ,t,,TCDCFCCr   )rJ   	functoolswraps)r0   methodrQ   rL   rM   s   `  r   _create_delegator_methodzHPandasDelegate._add_delegate_accessors.<locals>._create_delegator_methodz   s9    X'7'=>F__V$D %D Hr   NrI   r0   str)rJ   r   setattr)clsrM   	accessorstyp	overwriterL   raise_on_missingrN   rU   r0   rQ   s    `   `     r   _add_delegate_accessorsz&PandasDelegate._add_delegate_accessorsK   so    >	 	  	&D$H&6t&<dCKj .t4,T2 T 2T1%	&r   NrV   r0   rW   r"   None)rZ   r#   r[   rW   r\   boolrL   Callable[[str], str]r]   ra   r"   r`   )	r$   r%   r&   rK   r3   r6   r8   classmethodr^   r;   r   r   r+   r+   =   sz    B>:   1<!%E& E& 	E&
 E& /E& E& 
E& E&r   r+   Fc                    | S r:   r;   r<   s    r   r>   r>      s    q r   Tc                &      f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 & docstrings.
    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                6    | j                         | S )N)r\   rL   r]   )r^   )rY   rL   rZ   rM   r\   r]   r[   s    r   add_delegate_accessorsz.delegate_names.<locals>.add_delegate_accessors   s/    ##-- 	$ 	
 
r   r;   )rM   rZ   r[   r\   rL   r]   rg   s   `````` r   delegate_namesrh      s    P	 	 "!r   c                      e Zd ZdZddZd Zy)Accessora  
    Custom property-like object.

    A descriptor for 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``.
    c                     || _         || _        y r:   )_name	_accessor)r   r0   r   s      r   __init__zAccessor.__init__   s    
!r   c                @    || j                   S | j                  |      S r:   )rm   )r   objrY   s      r   __get__zAccessor.__get__   s     ;>>!~~c""r   Nr_   )r$   r%   r&   rK   rn   rq   r;   r   r   rj   rj      s    &"#r   rj   c                     d fd}|S )a  
    Register a custom accessor on 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
    -----
    This function allows you to register a custom-defined accessor class
    for pandas objects (DataFrame, Series, or Index).
    The requirements for the accessor class are as follows:

    * Must contain an init method that:

      * accepts a single object

      * raises an AttributeError if the object does not have correctly
        matching inputs for the accessor

    * Must contain a method for each access pattern.

      * The methods should be able to take any argument signature.

      * Accessible using the @property decorator if no additional arguments are
        needed.

    c           	         t              r.t        j                  d| dddt        t	                      t        t        |              j                  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UserWarningr   rX   rj   r   add)r   rY   r0   s    r   	decoratorz%_register_accessor.<locals>.decorator  sn    3MM+H<|(*SG ,01 +- 	T8D(344 r   )r   r	   r"   r	   r;   )r0   rY   ry   s   `` r   _register_accessorrz      s    Z r   a  
An accessor that only accepts integers could
have a class defined like this:

>>> @pd.api.extensions.register_dataframe_accessor("int_accessor")
... class IntAccessor:
...     def __init__(self, pandas_obj):
...         if not all(pandas_obj[col].dtype == 'int64' for col in pandas_obj.columns):
...             raise AttributeError("All columns must contain integer values only")
...         self._obj = pandas_obj
...
...     def sum(self):
...         return self._obj.sum()
...
>>> df = pd.DataFrame([[1, 2], ['x', 'y']])
>>> df.int_accessor
Traceback (most recent call last):
...
AttributeError: All columns must contain integer values only.
>>> df = pd.DataFrame([[1, 2], [3, 4]])
>>> df.int_accessor.sum()
0    4
1    6
dtype: int64zpandas.api.extensionsc                &    ddl m} t        | |      S )a  
    Register a custom accessor on DataFrame 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
    -----
    This function allows you to register a custom-defined accessor class for DataFrame.
    The requirements for the accessor class are as follows:

    * Must contain an init method that:

      * accepts a single DataFrame object

      * raises an AttributeError if the DataFrame object does not have correctly
        matching inputs for the accessor

    * Must contain a method for each access pattern.

      * The methods should be able to take any argument signature.

      * Accessible using the @property decorator if no additional arguments are
        needed.

    Examples
    --------
    An accessor that only accepts integers could
    have a class defined like this:

    >>> @pd.api.extensions.register_dataframe_accessor("int_accessor")
    ... class IntAccessor:
    ...     def __init__(self, pandas_obj):
    ...         if not all(
    ...             pandas_obj[col].dtype == "int64" for col in pandas_obj.columns
    ...         ):
    ...             raise AttributeError("All columns must contain integer values only")
    ...         self._obj = pandas_obj
    ...
    ...     def sum(self):
    ...         return self._obj.sum()
    >>> df = pd.DataFrame([[1, 2], ["x", "y"]])
    >>> df.int_accessor
    Traceback (most recent call last):
    ...
    AttributeError: All columns must contain integer values only.
    >>> df = pd.DataFrame([[1, 2], [3, 4]])
    >>> df.int_accessor.sum()
    0    4
    1    6
    dtype: int64
    r   )	DataFrame)pandasr|   rz   )r0   r|   s     r   register_dataframe_accessorr~   E  s    H !dI..r   a  
An accessor that only accepts integers could
have a class defined like this:

>>> @pd.api.extensions.register_series_accessor("int_accessor")
... class IntAccessor:
...     def __init__(self, pandas_obj):
...         if not pandas_obj.dtype == 'int64':
...             raise AttributeError("The series must contain integer data only")
...         self._obj = pandas_obj
...
...     def sum(self):
...         return self._obj.sum()
...
>>> df = pd.Series([1, 2, 'x'])
>>> df.int_accessor
Traceback (most recent call last):
...
AttributeError: The series must contain integer data only.
>>> df = pd.Series([1, 2, 3])
>>> df.int_accessor.sum()
6c                &    ddl m} t        | |      S )aj  
    Register a custom accessor on Series 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
    -----
    This function allows you to register a custom-defined accessor class for Series.
    The requirements for the accessor class are as follows:

    * Must contain an init method that:

      * accepts a single Series object

      * raises an AttributeError if the Series object does not have correctly
        matching inputs for the accessor

    * Must contain a method for each access pattern.

      * The methods should be able to take any argument signature.

      * Accessible using the @property decorator if no additional arguments are
        needed.

    Examples
    --------
    An accessor that only accepts integers could
    have a class defined like this:

    >>> @pd.api.extensions.register_series_accessor("int_accessor")
    ... class IntAccessor:
    ...     def __init__(self, pandas_obj):
    ...         if not pandas_obj.dtype == "int64":
    ...             raise AttributeError("The series must contain integer data only")
    ...         self._obj = pandas_obj
    ...
    ...     def sum(self):
    ...         return self._obj.sum()
    >>> df = pd.Series([1, 2, "x"])
    >>> df.int_accessor
    Traceback (most recent call last):
    ...
    AttributeError: The series must contain integer data only.
    >>> df = pd.Series([1, 2, 3])
    >>> df.int_accessor.sum()
    6
    r   )Series)r}   r   rz   )r0   r   s     r   register_series_accessorr     s    @ dF++r   a[  
An accessor that only accepts integers could
have a class defined like this:

>>> @pd.api.extensions.register_index_accessor("int_accessor")
... class IntAccessor:
...     def __init__(self, pandas_obj):
...         if not all(isinstance(x, int) for x in pandas_obj):
...             raise AttributeError("The index must only be an integer value")
...         self._obj = pandas_obj
...
...     def even(self):
...         return [x for x in self._obj if x % 2 == 0]
>>> df = pd.DataFrame.from_dict(
...     {"row1": {"1": 1, "2": "a"}, "row2": {"1": 2, "2": "b"}}, orient="index"
... )
>>> df.index.int_accessor
Traceback (most recent call last):
...
AttributeError: The index must only be an integer value.
>>> df = pd.DataFrame(
...     {"col1": [1, 2, 3, 4], "col2": ["a", "b", "c", "d"]}, index=[1, 2, 5, 8]
... )
>>> df.index.int_accessor.even()
[2, 8]c                &    ddl m} t        | |      S )aQ  
    Register a custom accessor on Index 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
    -----
    This function allows you to register a custom-defined accessor class for Index.
    The requirements for the accessor class are as follows:

    * Must contain an init method that:

      * accepts a single Index object

      * raises an AttributeError if the Index object does not have correctly
        matching inputs for the accessor

    * Must contain a method for each access pattern.

      * The methods should be able to take any argument signature.

      * Accessible using the @property decorator if no additional arguments are
        needed.

    Examples
    --------
    An accessor that only accepts integers could
    have a class defined like this:

    >>> @pd.api.extensions.register_index_accessor("int_accessor")
    ... class IntAccessor:
    ...     def __init__(self, pandas_obj):
    ...         if not all(isinstance(x, int) for x in pandas_obj):
    ...             raise AttributeError("The index must only be an integer value")
    ...         self._obj = pandas_obj
    ...
    ...     def even(self):
    ...         return [x for x in self._obj if x % 2 == 0]
    >>> df = pd.DataFrame.from_dict(
    ...     {"row1": {"1": 1, "2": "a"}, "row2": {"1": 2, "2": "b"}}, orient="index"
    ... )
    >>> df.index.int_accessor
    Traceback (most recent call last):
    ...
    AttributeError: The index must only be an integer value.
    >>> df = pd.DataFrame(
    ...     {"col1": [1, 2, 3, 4], "col2": ["a", "b", "c", "d"]}, index=[1, 2, 5, 8]
    ... )
    >>> df.index.int_accessor.even()
    [2, 8]
    r   r
   )r}   r   rz   )r0   r   s     r   register_index_accessorr     s    H dE**r   )
rZ   r#   r[   rW   r\   ra   rL   rb   r]   ra   )r0   rW   rY   ztype[NDFrame | Index]r"   Callable[[TypeT], TypeT])r0   rW   r"   r   ) rK   
__future__r   rR   typingr   r   ru   pandas.util._decoratorsr   pandas.util._exceptionsr   collections.abcr   pandas._typingr	   r}   r   pandas.core.genericr   r   r+   rh   rj   CachedAccessorrz   _register_df_examplesr~   _register_series_examplesr   _register_index_examplesr   r;   r   r   <module>r      s+   #   5($+ <T& T&v -8!3"3" 
3" 	3"
 +3" 3"l# #B :
:)::z 4 #$E/ %E/P 0 #$A, %A,H
 6 #$E+ %E+r   