Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Workflow.transform to be run with a DataFrame type #1777

Merged
merged 6 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions nvtabular/workflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import time
import types
import warnings
from functools import singledispatchmethod
from typing import TYPE_CHECKING, Optional

import cloudpickle
Expand Down Expand Up @@ -78,7 +79,8 @@ def __init__(self, output_node: WorkflowNode, client: Optional["distributed.Clie
self.graph = Graph(output_node)
self.executor = DaskExecutor(client)

def transform(self, dataset: Dataset) -> Dataset:
@singledispatchmethod
def transform(self, data):
"""Transforms the dataset by applying the graph of operators to it. Requires the ``fit``
method to have already been called, or calculated statistics to be loaded from disk

Expand All @@ -88,16 +90,33 @@ def transform(self, dataset: Dataset) -> Dataset:

Parameters
-----------
dataset: Dataset
Input dataset to transform
data: Union[Dataset, DataFrameType]
Input dataset or dataframe to transform

Returns
-------
Dataset
Transformed Dataset with the workflow graph applied to it
Dataset or DataFrame
Transformed Dataset or DataFrame with the workflow graph applied to it
"""
raise NotImplementedError(
"Workflow.transform received an unsupported type: {type(dataset)} "
"Supported types are a `merlin.io.Dataset` or DataFrame (pandas or cudf)"
)

@transform.register
def _(self, dataset: Dataset) -> Dataset:
return self._transform_impl(dataset)

@transform.register
def _(self, dataframe: pd.DataFrame) -> pd.DataFrame:
return self._transform_df(dataframe)

if cudf:
karlhigley marked this conversation as resolved.
Show resolved Hide resolved

@transform.register
def _(self, dataframe: cudf.DataFrame) -> cudf.DataFrame:
return self._transform_df(dataframe)

def fit_schema(self, input_schema: Schema):
"""Computes input and output schemas for each node in the Workflow graph

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/workflow/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def test_workflow_double_fit():
workflow.transform(df_event).to_ddf().compute()


def test_workflow_transform_df():
df = make_df({"user_session": ["1", "2", "4", "4", "5"]})
ops = ["user_session"] >> nvt.ops.Categorify()
dataset = nvt.Dataset(df)
workflow = nvt.Workflow(ops)
workflow.fit(dataset)
assert isinstance(workflow.transform(df), type(df))


@pytest.mark.parametrize("engine", ["parquet"])
def test_workflow_fit_op_rename(tmpdir, dataset, engine):
# NVT
Expand Down