insertColumn / popColumn
Column insertion and removal for DataFrames — mirrors
pandas.DataFrame.insert() and
pandas.DataFrame.pop().
Because tsb DataFrames are immutable, both functions return a new DataFrame
rather than mutating the original. popColumn returns both the extracted
Series and the resulting DataFrame.
| Function | Pandas equivalent | Description |
|---|---|---|
insertColumn(df, loc, col, values) |
df.insert(loc, col, value) |
Insert a new column at integer position loc |
popColumn(df, col) |
df.pop(col) |
Remove a column; returns { series, df } |
reorderColumns(df, order) |
df[order] |
Reorder (and optionally subset) columns |
moveColumn(df, col, newLoc) |
— | Move an existing column to a new integer position |
insertColumnimport { DataFrame, insertColumn } from "tsb";
const df = DataFrame.fromColumns({
name: ["Alice", "Bob", "Carol"],
age: [30, 25, 35],
});
// columns: ["name", "age"]
// Insert "city" between "name" and "age"
const df2 = insertColumn(df, 1, "city", ["NY", "LA", "SF"]);
// df2.columns.values → ["name", "city", "age"]
// df2.col("city").values → ["NY", "LA", "SF"]
// Original is unchanged
// df.columns.values → ["name", "age"]
import { DataFrame, Series, insertColumn } from "tsb";
const df = DataFrame.fromColumns({ a: [1, 2, 3], b: [4, 5, 6] });
const salary = new Series({ data: [100_000, 90_000, 120_000], name: "salary" });
const df2 = insertColumn(df, 0, "salary", salary);
// df2.columns.values → ["salary", "a", "b"]
popColumnimport { DataFrame, popColumn } from "tsb";
const df = DataFrame.fromColumns({
id: [1, 2, 3],
name: ["Alice", "Bob", "Carol"],
age: [30, 25, 35],
});
// Remove "age" and keep the Series
const { series: ageSeries, df: df2 } = popColumn(df, "age");
// ageSeries.values → [30, 25, 35]
// df2.columns.values → ["id", "name"]
// df.columns.values → ["id", "name", "age"] ← original unchanged
reorderColumnsimport { DataFrame, reorderColumns } from "tsb";
const df = DataFrame.fromColumns({ a: [1], b: [2], c: [3], d: [4] });
// Reverse the column order
const df2 = reorderColumns(df, ["d", "c", "b", "a"]);
// df2.columns.values → ["d", "c", "b", "a"]
// Select a subset (drops columns not listed)
const df3 = reorderColumns(df, ["a", "c"]);
// df3.columns.values → ["a", "c"] (b and d are dropped)
moveColumnimport { DataFrame, moveColumn } from "tsb";
const df = DataFrame.fromColumns({
year: [2020, 2021, 2022],
value: [10, 20, 30],
label: ["a", "b", "c"],
});
// columns: ["year", "value", "label"]
// Move "label" to the front
const df2 = moveColumn(df, "label", 0);
// df2.columns.values → ["label", "year", "value"]
// Duplicate column name (default: not allowed)
insertColumn(df, 1, "a", [1, 2, 3]);
// → RangeError: Column "a" already exists. Use allowDuplicates=true to permit...
// Out-of-range loc
insertColumn(df, 99, "x", [1, 2, 3]);
// → RangeError: loc=99 is out of range [0, 2].
// Wrong number of values
insertColumn(df, 0, "x", [1]); // df has 3 rows
// → RangeError: values length 1 does not match DataFrame row count 3.
// Column not found
popColumn(df, "missing");
// → RangeError: Column "missing" not found in DataFrame.
| pandas | tsb |
|---|---|
df.insert(1, "x", [1,2,3]) *(mutates)* | insertColumn(df, 1, "x", [1,2,3]) |
series = df.pop("col") *(mutates)* | const { series, df: df2 } = popColumn(df, "col") |
df[["c","a","b"]] | reorderColumns(df, ["c","a","b"]) |