Skip to content

continuous_timeseries.pandas_accessors#

API for pandas accessors.

Classes:

Name Description
SeriesCTAccessor

pd.Series accessors

Functions:

Name Description
register_pandas_accessor

Register the pandas accessors

SeriesCTAccessor #

pd.Series accessors

For details, see pandas' docs.

Methods:

Name Description
__init__

Initialise

Attributes:

Name Type Description
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)

metadata property #

metadata: DataFrame

Get the metadata as a pd.DataFrame

__init__ #

__init__(pandas_obj: Series[Timeseries])

Initialise

Parameters:

Name Type Description Default
pandas_obj Series[Timeseries]

Pandas object to use via the accessor

required
Source code in src/continuous_timeseries/pandas_accessors.py
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

register_pandas_accessor #

register_pandas_accessor(namespace: str = 'ct') -> None

Register the pandas accessors

For details, see pandas' docs.

We provide this as a separate function because we have had really bad experiences with imports having side effects and don't want to pass those on to our users.

Parameters:

Name Type Description Default
namespace str

Namespace to use for the accessor

'ct'
Source code in src/continuous_timeseries/pandas_accessors.py
def register_pandas_accessor(namespace: str = "ct") -> None:
    """
    Register the pandas accessors

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

    We provide this as a separate function
    because we have had really bad experiences with imports having side effects
    and don't want to pass those on to our users.

    Parameters
    ----------
    namespace
        Namespace to use for the accessor
    """
    try:
        import pandas as pd
    except ImportError as exc:
        raise MissingOptionalDependencyError(
            "register_pandas_accessor", requirement="pandas"
        ) from exc

    pd.api.extensions.register_series_accessor(namespace)(SeriesCTAccessor)