Skip to content

extensions

Extensions for pandas DataFrames and Series. Access with the boot attribute of a DataFrame or Series.

Available after importing the package:

import bootstrap

df = pd.DataFrame(...)
df.boot.get_samples(...)

ser = pd.Series(...)
ser.boot.get_samples(...)

AccessorMixin

Common functionality for DataFrame and Series accessors.

Source code in bootstrap/extensions.py
class AccessorMixin:
    """Common functionality for DataFrame and Series accessors."""

    def __init__(self, obj):
        self._obj = obj

    def get_samples(
        self,
        bfunc: BFUNC,
        B: int = 100,
        sample_kwargs: Dict[str, Any] = None,
        parallel: Optional[Parallel] = None,
        **kwargs: P.kwargs,
    ) -> Union[pd.Series, pd.DataFrame]:
        """Get bootstrap samples of the object.

        Args:
            bfunc: Bootstrap function.
            B: Number of bootstrap samples.
            sample_kwargs: Keyword arguments to pass to the sample method of the object.
            parallel: Joblib Parallel object.
            kwargs: Keyword arguments to pass to the bootstrap function.

        Returns:
            Bootstrap samples in a Series or DataFrame. Depends on the return type of the bootstrap function.

        """
        return bootstrap(
            self._obj,
            bfunc=bfunc,
            B=B,
            sample_kwargs=sample_kwargs,
            parallel=parallel,
            **kwargs,
        )

get_samples(bfunc, B=100, sample_kwargs=None, parallel=None, **kwargs)

Get bootstrap samples of the object.

Parameters:

Name Type Description Default
bfunc BFUNC

Bootstrap function.

required
B int

Number of bootstrap samples.

100
sample_kwargs Dict[str, Any]

Keyword arguments to pass to the sample method of the object.

None
parallel Optional[Parallel]

Joblib Parallel object.

None
kwargs kwargs

Keyword arguments to pass to the bootstrap function.

{}

Returns:

Type Description
Union[Series, DataFrame]

Bootstrap samples in a Series or DataFrame. Depends on the return type of the bootstrap function.

Source code in bootstrap/extensions.py
def get_samples(
    self,
    bfunc: BFUNC,
    B: int = 100,
    sample_kwargs: Dict[str, Any] = None,
    parallel: Optional[Parallel] = None,
    **kwargs: P.kwargs,
) -> Union[pd.Series, pd.DataFrame]:
    """Get bootstrap samples of the object.

    Args:
        bfunc: Bootstrap function.
        B: Number of bootstrap samples.
        sample_kwargs: Keyword arguments to pass to the sample method of the object.
        parallel: Joblib Parallel object.
        kwargs: Keyword arguments to pass to the bootstrap function.

    Returns:
        Bootstrap samples in a Series or DataFrame. Depends on the return type of the bootstrap function.

    """
    return bootstrap(
        self._obj,
        bfunc=bfunc,
        B=B,
        sample_kwargs=sample_kwargs,
        parallel=parallel,
        **kwargs,
    )

DataFrameBootstrapAccessor

Bases: AccessorMixin

Bootstrap accessor for pandas DataFrames.

Examples:

Bootstrap the mean of each column in a DataFrame.

import pandas as pd
import bootstrap

df = pd.DataFrame({
    'a': [1, 2, 3, 4, 5],
    'b': [6, 7, 8, 9, 10],
})

def mean_of_columns(df):
    return df.mean(numeric_only=True)

df_bootstrap: pd.DataFrame = df.boot.get_samples(bfunc=mean_of_columns, B=100)
Source code in bootstrap/extensions.py
@pd.api.extensions.register_dataframe_accessor("boot")
class DataFrameBootstrapAccessor(AccessorMixin):
    """Bootstrap accessor for pandas DataFrames.

    Examples:
        Bootstrap the mean of each column in a DataFrame.

        ```python
        import pandas as pd
        import bootstrap

        df = pd.DataFrame({
            'a': [1, 2, 3, 4, 5],
            'b': [6, 7, 8, 9, 10],
        })

        def mean_of_columns(df):
            return df.mean(numeric_only=True)

        df_bootstrap: pd.DataFrame = df.boot.get_samples(bfunc=mean_of_columns, B=100)
        ```

    """

SeriesBootstrapAccessor

Bases: AccessorMixin

Bootstrap accessor for pandas Series.

Examples:

Bootstrap the mean of a Series.

import pandas as pd
import bootstrap

ser = pd.Series([1, 2, 3, 4, 5])

def mean_of_series(ser):
    return ser.mean()

ser_bootstrap: pd.Series = ser.boot.get_samples(bfunc=mean_of_series, B=100)
Source code in bootstrap/extensions.py
@pd.api.extensions.register_series_accessor("boot")
class SeriesBootstrapAccessor(AccessorMixin):
    """Bootstrap accessor for pandas Series.

    Examples:
        Bootstrap the mean of a Series.

        ```python
        import pandas as pd
        import bootstrap

        ser = pd.Series([1, 2, 3, 4, 5])

        def mean_of_series(ser):
            return ser.mean()

        ser_bootstrap: pd.Series = ser.boot.get_samples(bfunc=mean_of_series, B=100)
        ```

    """

Comments