Skip to content

DataFrame

Defined in: data-frame.ts:100

A distributed collection of rows with a named schema, obtained from a SparkSession (for example via spark.read.parquet(path) or spark.sql(...)).

DataFrame is lazy. Transformation methods (select, filter, join, withColumn, etc.) return a new DataFrame that wraps an extended logical plan; no work is performed on the server until an action (collect, count, show, write.save, etc.) is called.

Read, transform, collect

const df = await spark.read.parquet("s3://bucket/events");
const recent = df
.filter(col("ts").gte(lit("2026-01-01")))
.groupBy("country")
.count();
const rows = await recent.collect();

Spark source: Dataset.scala

Type Parameter Default type
R extends Row Row
get stat(): DataFrameStat;

Defined in: data-frame.ts:767

Access statistical functions (corr, cov, crosstab, etc.).

DataFrameStat


get write(): DataFrameWriter;

Defined in: data-frame.ts:774

Returns a DataFrameWriter for persisting the contents of this DataFrame.

DataFrameWriter


get writeStream(): DataStreamWriter;

Defined in: data-frame.ts:794

Returns a DataStreamWriter for launching a streaming query against the data in this (streaming) DataFrame.

Only valid on streaming DataFrames (those whose source plan is built via spark.readStream). The server will reject the write if the input plan isn’t streaming.

DataStreamWriter

agg(...exprs): DataFrame;

Defined in: data-frame.ts:197

Aggregate over the whole DataFrame. Shorthand for groupBy().agg(...), matching PySpark and Scala.

Parameter Type
exprs Column[]

DataFrame

df.agg(count("*").alias("rows"), avg("salary").alias("mean"))

alias(name): DataFrame<R>;

Defined in: data-frame.ts:525

Assign an alias to this DataFrame, useful for self-joins.

Parameter Type
name string

DataFrame<R>


as<NewR>(): DataFrame<NewR>;

Defined in: data-frame.ts:132

Narrow this DataFrame’s row type for typed collect(), first(), etc. The cast is compile-time only. Matches Scala’s Dataset.as[T] precedent.

Shape-preserving transformations (filter, where, limit, sort, orderBy, etc.) keep the narrowed type. Shape-changing transformations (select, withColumn, drop, groupBy().agg(), etc.) reset to Row. Re-narrow with another .as<NewR>() after them if needed.

Type Parameter
NewR extends Row

DataFrame<NewR>

const rows = await df.as<{ count: bigint; mean: number }>().collect();
// rows is typed Array<{ count: bigint; mean: number }>

cache(): Promise<DataFrame<R>>;

Defined in: data-frame.ts:804

Persist this DataFrame with the default storage level (MEMORY_AND_DISK). Returns this DataFrame for method chaining.

Promise<DataFrame<R>>


coalesce(numPartitions): DataFrame<R>;

Defined in: data-frame.ts:645

Return a new DataFrame that is reduced to the given number of partitions. Unlike repartition(), coalesce avoids a full shuffle and tries to combine existing partitions.

Parameter Type Description
numPartitions number Target number of partitions

DataFrame<R>


col(name): Column;

Defined in: data-frame.ts:146

Return a Column bound to this DataFrame. Use to disambiguate columns in self-joins and same-schema joins where the unqualified column name would be ambiguous on the server side.

Parameter Type
name string

Column

const a = df.alias("a");
const b = df.alias("b");
a.join(b, a.col("id").eq(b.col("id")));

collect(): Promise<R[]>;

Defined in: data-frame.ts:924

Execute the plan and collect all result rows into a JS array.

For large datasets, prefer toLocalIterator() or forEach() to avoid loading everything into memory.

Promise<R[]>


columns(): Promise<string[]>;

Defined in: data-frame.ts:1055

Return the column names as a string array. Uses the AnalyzePlan.Schema RPC to resolve the schema without executing.

Promise<string[]>


count(): Promise<bigint>;

Defined in: data-frame.ts:942

Return the number of rows.

Uses an aggregate count plan. The full dataset is not collected. Returns a bigint because Spark’s count(*) is LongType. Wrap in Number(...) when you know the row count fits in a JS safe integer.

Promise<bigint>


createGlobalTempView(viewName): Promise<void>;

Defined in: data-frame.ts:887

Register as a global temporary view. Throws if the view already exists.

Parameter Type
viewName string

Promise<void>


createOrReplaceGlobalTempView(viewName): Promise<void>;

Defined in: data-frame.ts:876

Register as a global temporary view, replacing if it already exists.

Parameter Type
viewName string

Promise<void>


createOrReplaceTempView(viewName): Promise<void>;

Defined in: data-frame.ts:854

Register this DataFrame as a temporary view with the given name. The view is session-scoped and will be dropped when the session ends.

Parameter Type
viewName string

Promise<void>


createTempView(viewName): Promise<void>;

Defined in: data-frame.ts:865

Register as a temporary view. Throws if the view already exists.

Parameter Type
viewName string

Promise<void>


crossJoin(other): DataFrame;

Defined in: data-frame.ts:290

Alias for join with joinType=“cross”.

Parameter Type
other DataFrame

DataFrame


cube(...columns): GroupedData;

Defined in: data-frame.ts:202

Multi-dimensional cube aggregation (all grouping-column combinations).

Parameter Type
columns (string | Column)[]

GroupedData


describe(...cols): DataFrame;

Defined in: data-frame.ts:684

Compute summary statistics (count, mean, stddev, min, max) for columns.

Parameter Type
cols string[]

DataFrame


distinct(): DataFrame<R>;

Defined in: data-frame.ts:404

Alias for dropDuplicates() with no arguments.

DataFrame<R>


drop(...columnNames): DataFrame;

Defined in: data-frame.ts:295

Drop one or more columns by name.

Parameter Type
columnNames string[]

DataFrame


dropDuplicates(...columnNames): DataFrame<R>;

Defined in: data-frame.ts:394

Remove duplicate rows, optionally considering only a subset of columns.

Parameter Type
columnNames string[]

DataFrame<R>


dropna(how?, cols?): DataFrame<R>;

Defined in: data-frame.ts:500

Drop rows with null values.

Parameter Type Default value
how "any" | "all" "any"
cols string[] []

DataFrame<R>


dtypes(): Promise<[string, string][]>;

Defined in: data-frame.ts:1065

Return column names and their data types as [name, type] pairs. Uses the AnalyzePlan.Schema RPC.

Promise<[string, string][]>


except(other): DataFrame;

Defined in: data-frame.ts:445

Return rows in this but not in other (distinct).

Parameter Type
other DataFrame

DataFrame


exceptAll(other): DataFrame;

Defined in: data-frame.ts:450

Return rows in this but not in other (duplicates kept).

Parameter Type
other DataFrame

DataFrame


explain(mode?): Promise<string>;

Defined in: data-frame.ts:1098

Return the query execution plan as a string.

Parameter Type Default value Description
mode "extended" | "simple" | "codegen" | "cost" | "formatted" "simple" Explain mode: “simple”, “extended”, “codegen”, “cost”, “formatted”

Promise<string>


fillna(value, cols?): DataFrame<R>;

Defined in: data-frame.ts:490

Replace null values. If cols is empty, applies to all columns.

Parameter Type Default value
value string | number | boolean undefined
cols string[] []

DataFrame<R>


filter(condition): DataFrame<R>;

Defined in: data-frame.ts:160

Filter rows by a boolean Column expression or a SQL predicate string. A string is parsed server-side as SQL via expr(...).

Parameter Type
condition string | Column

DataFrame<R>

df.filter(col("status").eq(lit("active")));
df.filter("status = 'active' AND region IN ('EU', 'US')");

first(): Promise<R | null>;

Defined in: data-frame.ts:1017

Return the first row as a Row object, or null if the DataFrame is empty.

Promise<R | null>


forEach(fn): Promise<void>;

Defined in: data-frame.ts:995

Process each row with a callback as it streams from the server.

Parameter Type
fn (row) => void

Promise<void>

await df.forEach((row) => console.log(row.name, row.salary));

getStorageLevel(): Promise<StorageLevel>;

Defined in: data-frame.ts:841

Get the storage level used for caching this DataFrame. Returns the StorageLevel if cached, or NONE if not cached.

Promise<StorageLevel>


groupBy(...columns): GroupedData;

Defined in: data-frame.ts:185

Group by one or more columns, returning a GroupedData handle for aggregation.

Parameter Type
columns (string | Column)[]

GroupedData


head(n?): Promise<R[]>;

Defined in: data-frame.ts:1025

Return the first n rows as an array (alias for limit + collect).

Parameter Type Default value
n number 1

Promise<R[]>


hint(name, ...parameters): DataFrame<R>;

Defined in: data-frame.ts:541

Attach an optimizer hint to this DataFrame.

Parameter Type
name string
parameters (string | number | boolean)[]

DataFrame<R>

df.hint("broadcast")
df.join(right.hint("broadcast"), ...)

intersect(other): DataFrame;

Defined in: data-frame.ts:435

Return rows present in both DataFrames (distinct).

Parameter Type
other DataFrame

DataFrame


intersectAll(other): DataFrame;

Defined in: data-frame.ts:440

Return rows present in both DataFrames (duplicates kept).

Parameter Type
other DataFrame

DataFrame


isEmpty(): Promise<boolean>;

Defined in: data-frame.ts:1075

Returns true if the DataFrame has no rows. Uses head(1) to check and stops after the first row.

Promise<boolean>


join(
other,
condition?,
joinType?): DataFrame;

Defined in: data-frame.ts:262

Join with another DataFrame.

Parameter Type Default value Description
other DataFrame undefined The right side DataFrame
condition? Column undefined Join condition (a boolean Column expression)
joinType? | "inner" | "full_outer" | "left_outer" | "right_outer" | "left_semi" | "left_anti" | "cross" "inner" Type of join (default: “inner”)

DataFrame


limit(n): DataFrame<R>;

Defined in: data-frame.ts:214

Limit the number of rows.

Parameter Type
n number

DataFrame<R>


melt(
ids,
values,
variableColumnName,
valueColumnName): DataFrame;

Defined in: data-frame.ts:757

Alias for unpivot().

Parameter Type
ids (string | Column)[]
values | (string | Column)[] | undefined
variableColumnName string
valueColumnName string

DataFrame


offset(n): DataFrame<R>;

Defined in: data-frame.ts:409

Skip the first N rows.

Parameter Type
n number

DataFrame<R>


orderBy(...columns): DataFrame<R>;

Defined in: data-frame.ts:251

Alias for sort().

Parameter Type
columns (string | Column)[]

DataFrame<R>


persist(storageLevel?): Promise<DataFrame<R>>;

Defined in: data-frame.ts:814

Persist this DataFrame with the given storage level. Returns this DataFrame for method chaining.

Parameter Type Default value Description
storageLevel StorageLevel MEMORY_AND_DISK How to store the cached data

Promise<DataFrame<R>>


printSchema(): Promise<void>;

Defined in: data-frame.ts:1113

Print the schema to the console in a tree format. Convenience method that calls schema() and formats the output.

Promise<void>


randomSplit(weights, seed?): DataFrame<Row>[];

Defined in: data-frame.ts:716

Randomly split this DataFrame into multiple DataFrames by weight.

Parameter Type
weights number[]
seed? number

DataFrame<Row>[]


repartition(numPartitions, ...columns): DataFrame<R>;

Defined in: data-frame.ts:620

Return a new DataFrame partitioned by the given number of partitions. This results in a full shuffle of the data.

Parameter Type Description
numPartitions number Target number of partitions
columns (string | Column)[] Optional partitioning columns

DataFrame<R>


repartitionByRange(numPartitions, ...columns): DataFrame<R>;

Defined in: data-frame.ts:660

Return a new DataFrame partitioned by the given columns using range partitioning.

Parameter Type Description
numPartitions number Target number of partitions
columns (string | Column)[] Partitioning columns

DataFrame<R>


replace(to, subset?): DataFrame;

Defined in: data-frame.ts:702

Replace values matching old with new, optionally restricted to a column subset.

Parameter Type Default value
to Record<string, string | number | boolean | null> undefined
subset string[] []

DataFrame


rollup(...columns): GroupedData;

Defined in: data-frame.ts:208

Multi-dimensional rollup aggregation (hierarchical subtotals).

Parameter Type
columns (string | Column)[]

GroupedData


sameSemantics(other): Promise<boolean>;

Defined in: data-frame.ts:898

Returns true if both DataFrames have the same logical plan.

Parameter Type
other DataFrame

Promise<boolean>


sample(
fraction,
withReplacement?,
seed?): DataFrame<R>;

Defined in: data-frame.ts:476

Return a random sample of rows.

Parameter Type Default value
fraction number undefined
withReplacement boolean false
seed? number undefined

DataFrame<R>


schema(): Promise<Record<string, unknown>>;

Defined in: data-frame.ts:1085

Return the schema of the DataFrame as a plain object. Uses the AnalyzePlan.Schema RPC to resolve column names and types without executing the query.

Promise<Record<string, unknown>>


select(...columns): DataFrame;

Defined in: data-frame.ts:175

Project (select) a subset of columns.

Parameter Type
columns (string | Column)[]

DataFrame


selectExpr(...exprs): DataFrame;

Defined in: data-frame.ts:559

Select columns using SQL expression strings. Each string is parsed by the server as an expression.

Parameter Type
exprs string[]

DataFrame

df.selectExpr("age * 2 as doubled_age", "name")

semanticHash(): Promise<number>;

Defined in: data-frame.ts:908

Returns a hash code of the logical plan.

Promise<number>


show(numRows?, truncate?): Promise<void>;

Defined in: data-frame.ts:1125

Pretty-print the first numRows rows to the console as an ASCII table.

Mirrors PySpark’s df.show() behaviour. If truncate is true, strings longer than 20 characters are truncated with ....

Parameter Type Default value
numRows number 20
truncate boolean true

Promise<void>


sort(...columns): DataFrame<R>;

Defined in: data-frame.ts:226

Sort by one or more columns (ascending by default). Use col(“x”).desc() for descending order.

Parameter Type
columns (string | Column)[]

DataFrame<R>


sortWithinPartitions(...columns): DataFrame<R>;

Defined in: data-frame.ts:587

Sort within each partition (non-global sort).

Parameter Type
columns (string | Column)[]

DataFrame<R>


summary(...statistics): DataFrame;

Defined in: data-frame.ts:693

Compute specified statistics for numeric and string columns.

Parameter Type
statistics string[]

DataFrame


tail(n): Promise<Row[]>;

Defined in: data-frame.ts:1042

Return the last n rows as an array.

Maps to Spark Connect’s Relation.Tail.

Parameter Type
n number

Promise<Row[]>


take(n): Promise<R[]>;

Defined in: data-frame.ts:1033

Return the first n rows as an array. Alias for head(). Matches PySpark’s take() semantics.

Parameter Type
n number

Promise<R[]>


toDF(...columnNames): DataFrame;

Defined in: data-frame.ts:512

Return a new DataFrame with renamed columns (positional).

Parameter Type
columnNames string[]

DataFrame


toLocalIterator(): AsyncIterableIterator<R>;

Defined in: data-frame.ts:978

Async iterator that yields rows one at a time. Only one batch is in memory at a time.

AsyncIterableIterator<R>

for await (const row of df.toLocalIterator()) {
console.log(row);
}

transform<T>(fn): T;

Defined in: data-frame.ts:580

Apply a user-defined function to this DataFrame and return the result. This is purely client-side; it just calls fn(this).

Enables fluent pipeline composition:

Type Parameter
T extends DataFrame<Row>
Parameter Type
fn (df) => T

T

df.transform(withDoubledAge).transform(withSalaryBand)

union(other): DataFrame;

Defined in: data-frame.ts:420

Return a new DataFrame with rows from both this and other (duplicates kept).

Parameter Type
other DataFrame

DataFrame


unionAll(other): DataFrame;

Defined in: data-frame.ts:425

Alias for union().

Parameter Type
other DataFrame

DataFrame


unionByName(other, allowMissingColumns?): DataFrame;

Defined in: data-frame.ts:430

Union by column name (rather than position), keeping duplicates.

Parameter Type Default value
other DataFrame undefined
allowMissingColumns boolean false

DataFrame


unpersist(blocking?): Promise<DataFrame<R>>;

Defined in: data-frame.ts:828

Remove this DataFrame from the cache.

Parameter Type Default value Description
blocking boolean false Whether to block until the operation completes

Promise<DataFrame<R>>


unpivot(
ids,
values,
variableColumnName,
valueColumnName): DataFrame;

Defined in: data-frame.ts:738

Unpivot from wide format to long format.

Parameter Type
ids (string | Column)[]
values | (string | Column)[] | undefined
variableColumnName string
valueColumnName string

DataFrame


where(condition): DataFrame<R>;

Defined in: data-frame.ts:170

Alias for filter().

Parameter Type
condition string | Column

DataFrame<R>


withColumn(name, expression): DataFrame;

Defined in: data-frame.ts:308

Add or replace a column.

Parameter Type
name string
expression Column

DataFrame

df.withColumn("doubled", col("value").multiply(lit(2)))

withColumnRenamed(existing, newName): DataFrame;

Defined in: data-frame.ts:332

Rename a single column.

Parameter Type
existing string
newName string

DataFrame


withColumns(colMap): DataFrame;

Defined in: data-frame.ts:319

Add or replace multiple columns at once.

Parameter Type
colMap Record<string, Column>

DataFrame


withColumnsRenamed(colsMap): DataFrame;

Defined in: data-frame.ts:345

Rename multiple columns at once.

Parameter Type Description
colsMap Record<string, string> mapping of { existingName: newName }

DataFrame


withWatermark(eventTimeColumn, delayThreshold): DataFrame<R>;

Defined in: data-frame.ts:373

Attach an event-time watermark to a streaming DataFrame.

Bounds how late an event can arrive before Spark considers it dropped for stateful streaming operators (windowed aggregations, stream-stream joins, dropDuplicates). No effect on a batch DataFrame.

Parameter Type Description
eventTimeColumn string name of the event-time column
delayThreshold string Spark interval string, e.g. "10 minutes", "1 hour"

DataFrame<R>

spark.readStream.format("rate").load()
.withWatermark("timestamp", "10 minutes")
.groupBy(window(col("timestamp"), "5 minutes"))
.agg(count("*").alias("events"))

writeTo(tableName): DataFrameWriterV2;

Defined in: data-frame.ts:782

Returns a DataFrameWriterV2 for writing to the given table using the DataSource V2 API (catalog-aware, supports create/replace/append/overwrite).

Parameter Type
tableName string

DataFrameWriterV2