For the complete documentation index, see llms.txt. This page is also available as Markdown.

Transforms

TransformProcess — data transformations, column operations, type conversions, and sequences

A TransformProcess is an ordered list of operations applied sequentially to every record in a dataset. Each operation sees the record as it was left by the previous operation, allowing you to chain complex multi-step pipelines from simple building blocks.

The TransformProcess.Builder API is fluent and validates each operation against the current schema state at build time. If you reference a column that does not exist, apply a numeric operation to a string column, or produce a result that breaks the schema, you get an exception when calling .build() rather than at runtime.

Building a TransformProcess

import org.datavec.api.transform.TransformProcess;
import org.datavec.api.transform.condition.ConditionOp;
import org.datavec.api.transform.condition.column.*;

TransformProcess tp = new TransformProcess.Builder(inputDataSchema)
    // Remove columns that aren't features
    .removeColumns("CustomerID", "MerchantID")
    // Filter: keep only USA and CAN transactions
    .filter(new ConditionFilter(
        new CategoricalColumnCondition("MerchantCountryCode",
            ConditionOp.NotInSet, new HashSet<>(Arrays.asList("USA","CAN")))
    ))
    // Replace negative amounts with 0
    .conditionalReplaceValueTransform(
        "TransactionAmountUSD",
        new DoubleWritable(0.0),
        new DoubleColumnCondition("TransactionAmountUSD", ConditionOp.LessThan, 0.0)
    )
    // Parse a date string into epoch milliseconds
    .stringToTimeTransform("DateTimeString", "YYYY-MM-DD HH:mm:ss.SSS", DateTimeZone.UTC)
    .renameColumn("DateTimeString", "DateTime")
    // Extract hour-of-day as a new integer column
    .transform(new DeriveColumnsFromTimeTransform.Builder("DateTime")
        .addIntegerDerivedColumn("HourOfDay", DateTimeFieldType.hourOfDay())
        .build())
    .removeColumns("DateTime")
    .build();

Column Management

Removing Columns

Renaming Columns

Reordering Columns

Duplicating Columns

Adding Constant Columns

Useful when downstream systems require a fixed column that is the same for all records.

Type Conversions

String to Numeric

Categorical Encoding

One-Hot Encoding: Replaces a categorical column with N binary columns (one per state).

Integer Encoding: Replaces the categorical column with a single integer (0 to numStates-1).

Integer to Categorical: The reverse — assign state names to integer values.

Integer to One-Hot: Directly expand an integer column to one-hot, given known min/max.

String to Categorical: Attach known state names to an existing string column.

Mathematical Operations

Scalar Operations on a Single Column

Math Functions on a Column

Derived Column from Multiple Columns

Time Arithmetic

String Operations

Remove Whitespace

Append to String

Map Replacements

Replace specific string values with new values:

Time Transforms

Parse String to Time

Derive Integer Columns from Time

Extract date/time components from a Time column as new Integer columns:

Conditional Transforms

Replace a Value When Condition Is True

Replace with One of Two Values Based on Condition

Copy a Value from Another Column Conditionally

Filtering

Filters are applied inline during TransformProcess execution. A record is removed if the condition returns true.

Normalization

DataVec includes a built-in normalize operation that applies standard statistical normalization inline in the transform, given a pre-computed DataAnalysis object. The type parameter controls which normalization strategy to apply.

For full control over normalizer fitting and serialization, use NormalizerStandardize or NormalizerMinMaxScaler as a pre-processor on the DataSetIterator instead. See Normalization.

Sorting and Ranking

Calculate Sorted Rank

Adds a new Long column containing the rank (0 to N-1) of each record when sorted by a given column:

Sequence Transforms

Convert Records to Sequences

Group flat records into sequences by a key column:

Convert Sequences Back to Records

Flatten sequences into individual records:

Split Sequences

Break large sequences into smaller ones:

Trim Sequences

Remove time steps from the start or end:

Offset Sequence Columns

Shift column values forward or backward in time (for creating prediction targets):

Moving Window Reduction

Compute a rolling statistic over the last N time steps:

Reduce Sequences

Aggregate a whole sequence into a single record:

Applying Custom Transforms

If none of the built-in transforms meet your needs, implement the Transform interface directly and add it via .transform(yourCustomTransform).

Executing the Transform

After building a TransformProcess, execute it with LocalTransformExecutor or SparkTransformExecutor:

See Executors for full details.

Debugging Transforms

Print the schema state after each step to diagnose unexpected results:

This is particularly helpful when a later transform fails because the schema no longer matches expectations after earlier steps.

Last updated

Was this helpful?