heartandsole.api.extensions.register_field¶
- heartandsole.api.extensions.register_field(name)[source]¶
Register a custom accessor on Activity objects.
Based on
pandas.api.extensions.register_dataframe_accessor().- Parameters
name (str) – Name under which the accessor should be registered. A warning is issued if this name conflicts with a preexisting attribute.
- Returns
A class decorator.
- Return type
callable
See also
pandas.api.extensions.register_dataframe_accessor()Register a custom accessor on DataFrame objects.
Notes
When accessed, your accessor will be initialized with the Activity object the user is interacting with. So the signature must be
def __init__(self, activity_obj): # noqa: E999 ...
Examples
In your library code:
import heartandsole as hns @hns.api.extensions.register_field('running_smoothness') class SmoothnessAccessor: def __init__(self, activity_obj): self._obj = activity_obj @property def avg(self): # return the average of the records return self._obj.records['running_smoothness'].mean()
Back in an interactive IPython session:
In [1]: act = hns.Activity(pd.DataFrame({{'running_smoothness': np.linspace(0, 10)}}) In [2]: act.running_smoothness.avg Out[2]: 5.0