Skip to content

Pandas accessors#

Continuous timeseries also provides a pandas accessor. For details of the implementation of this pattern, see pandas' docs.

The accessors must be registered before they can be used (we do this to avoid imports of any of our modules having side effects, which is a pattern we have had bad experiences with in the past). This is done with register_pandas_accessor,

By default, the accessors are provided under the "ct" namespace and this is how the accessors are documented below. However, the namespace can be customised when using register_pandas_accessor, should you wish to use a different namespace for the accessor.

For the avoidance of doubt, in order to register/activate the accessors, you will need to run something like:

from continuous_timeseries.pandas_accessors import register_pandas_accessor

# The 'pd.Series.ct' namespace will not be available at this point.

# Register the accessors
register_pandas_accessor()

# The 'pd.Series.ct' namespace
# (or whatever other custom namespace you chose to register)
# will now be available.

The full accessor API is documented below.

pd.Series.ct #

pd.Series accessors

For details, see pandas' docs.

Attributes:

Name Type Description
pd.Series.ct.metadata DataFrame

Get the metadata as a pd.DataFrame

Source code in src/continuous_timeseries/pandas_accessors.py
class SeriesCTAccessor:
    """
    [`pd.Series`][pandas.Series] accessors

    For details, see
    [pandas' docs](https://pandas.pydata.org/docs/development/extending.html#registering-custom-accessors).
    """

    def __init__(self, pandas_obj: pd.Series[Timeseries]):  # type: ignore # pandas-stubs doesn't allow object even though it's fine
        """
        Initialise

        Parameters
        ----------
        pandas_obj
            Pandas object to use via the accessor
        """
        # TODO: consider adding validation
        # validate_series(pandas_obj)
        self._series = pandas_obj

    @property
    def metadata(self) -> pd.DataFrame:
        """
        Get the metadata as a [`pd.DataFrame`][pandas.DataFrame]
        """
        return self._series.index.to_frame(index=False)

pd.Series.ct.metadata property #

pd.Series.ct.metadata: DataFrame

Get the metadata as a pd.DataFrame