DataFrame Patterns¶
Select, assign, sort¶
function patternSelectAssignSort(ASTX) {
const df = ASTX.DataFrame.fromRecords([
{ id: 1, amount: 10, status: 'open' },
{ id: 2, amount: 25, status: 'closed' }
]);
return df
.select(['id', 'amount'])
.assign({
amount_x2: frame => frame.amount.multiply(2)
})
.sort('amount', false);
}
Single-pass projection with selectExpr¶
function patternSelectExpr(ASTX) {
const df = ASTX.DataFrame.fromRecords([
{ id: 1, amount: 10, score: 81 },
{ id: 2, amount: 20, score: 63 }
]);
return df.selectExpr({
id: 'id',
amount_x2: row => row.amount * 2,
score_bucket: (columns, idx) => columns.score.array[idx] >= 80 ? 'high' : 'standard'
});
}
Use selectExpr when you want passthrough + computed columns in one projection step.
Fast construction with fromColumns¶
function patternFromColumns(ASTX) {
return ASTX.DataFrame.fromColumns({
id: [1, 2, 3, 4],
region: ['east', 'west', 'east', 'west'],
amount: [10, 15, 25, 30]
});
}
Use this when your source data is already column-oriented.
Dedupe with explicit subset¶
function patternDedupe(ASTX) {
const df = ASTX.DataFrame.fromRecords([
{ region: 'east', amount: 10 },
{ region: 'east', amount: 20 },
{ region: 'west', amount: 30 }
]);
const byAllColumns = df.dropDuplicates();
const byRegionOnly = df.dropDuplicates(['region']);
return { byAllColumns, byRegionOnly };
}
Join with explicit key mapping¶
function patternMerge(ASTX) {
const users = ASTX.DataFrame.fromRecords([
{ user_id: 1, name: 'Alice' },
{ user_id: 2, name: 'Bob' }
]);
const scores = ASTX.DataFrame.fromRecords([
{ uid: 1, score: 88 },
{ uid: 2, score: 91 }
]);
return users.merge(scores, 'inner', {
leftOn: 'user_id',
rightOn: 'uid'
});
}
Grouped metrics¶
function patternGroupMetrics(ASTX) {
const df = ASTX.DataFrame.fromRecords([
{ region: 'east', amount: 10 },
{ region: 'east', amount: 20 },
{ region: 'west', amount: 30 }
]);
return df.groupBy(['region']).agg({
amount: ['sum', 'mean', values => Math.max(...values)]
});
}
Partitioned window metrics¶
function patternWindow(ASTX) {
const df = ASTX.DataFrame.fromRecords([
{ region: 'east', event_ts: 1, amount: 10 },
{ region: 'east', event_ts: 2, amount: 15 },
{ region: 'west', event_ts: 1, amount: 5 }
]);
return df
.window({
partitionBy: ['region'],
orderBy: [{ column: 'event_ts', ascending: true }]
})
.assign({
row_number: w => w.rowNumber(),
amount_lag_1: w => w.col('amount').lag(1),
amount_running_sum: w => w.col('amount').running('sum')
});
}
JSON and markdown output¶
function patternOutput(ASTX) {
const df = ASTX.DataFrame.fromRecords([
{ id: 1, amount: 10 },
{ id: 2, amount: 20 }
]);
return {
markdown: df.toMarkdown(),
prettyJson: df.toJson({ indent: 2 }),
newlineJson: df.toJson({ multiline: true })
};
}
Large dataset checklist¶
For large workloads:
- prefer
fromColumnsfor construction. - prefer
toColumns/toArrayswhen row objects are not required. - call
dropDuplicateswith minimal subset keys. - avoid repeated
toRecords()materialization inside loops.