Skip to main content
`Ctrl`+`K`

# pandas.api.extensions.register_dataframe_accessor

pandas.api.extensions.register_dataframe_accessor(name)

  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