1 2
import numpy as np
2 2
import pandas as pd
3

4 2
from snorkel.slicing import PandasSFApplier
5 2
from snorkel.slicing.sf import SlicingFunction
6

7

8 2
def slice_dataframe(
9
    df: pd.DataFrame, slicing_function: SlicingFunction
10
) -> pd.DataFrame:
11
    """Return a dataframe with examples corresponding to specified ``SlicingFunction``.
12

13
    Parameters
14
    ----------
15
    df
16
        A pandas DataFrame that will be sliced
17
    slicing_function
18
        SlicingFunction which will operate over df to return a subset of examples;
19
        function returns a subset of data for which ``slicing_function`` output is True
20

21
    Returns
22
    -------
23
    pd.DataFrame
24
        A DataFrame including only examples belonging to slice_name
25
    """
26

27 2
    S = PandasSFApplier([slicing_function]).apply(df)
28

29
    # Index into the SF labels by name
30 2
    df_idx = np.where(S[slicing_function.name])[0]  # type: ignore
31 2
    return df.iloc[df_idx]

Read our documentation on viewing source code .

Loading