Initializing playground…
← Back to roadmap

📦 hashPandasObject — Interactive Playground

hashPandasObject(obj) computes FNV-1a 64-bit hash values for each element of a Series or each row of a DataFrame — mirroring pandas.util.hash_pandas_object.
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · Series hashing

Hash each element of a Series. Identical values produce identical hashes; pass { index: false } to ignore the index label when computing the hash.

TypeScript
import { Series, hashPandasObject } from "tsb";

const s = new Series({ data: ["apple", "banana", "apple"], index: [0, 1, 2] });
const h = hashPandasObject(s, { index: false });

// Same value → same hash
console.log("apple===apple:", h.iat(0) === h.iat(2)); // true
console.log("apple===banana:", h.iat(0) === h.iat(1)); // false
console.log("hashes:", [...h.values]);
Click ▶ Run to execute
Ctrl+Enter to run

2 · DataFrame row hashing

Hash each row of a DataFrame. Rows with identical values across all columns produce the same hash, making this useful for deduplication and change detection.

TypeScript
import { DataFrame, hashPandasObject } from "tsb";

const df = DataFrame.fromColumns({
  id:   [1, 2, 3],
  name: ["Alice", "Bob", "Alice"],
  age:  [30, 25, 30],
});

const rowHashes = hashPandasObject(df, { index: false });
// Rows 0 and 2 are identical → same hash
console.log("row0===row2:", rowHashes.iat(0) === rowHashes.iat(2)); // true
console.log("row0===row1:", rowHashes.iat(0) === rowHashes.iat(1)); // false
Click ▶ Run to execute
Ctrl+Enter to run

3 · Deduplication with hashes

Use row hashes to find unique rows efficiently — a common pattern when duplicated() is too slow on large DataFrames.

TypeScript
import { DataFrame, hashPandasObject } from "tsb";

const df = DataFrame.fromColumns({
  a: [1, 2, 1, 3],
  b: ["x", "y", "x", "z"],
});

const hashes = hashPandasObject(df, { index: false });
const seen = new Set();
const uniqueRows: number[] = [];

for (let i = 0; i < df.shape[0]; i++) {
  const h = hashes.iat(i);
  if (!seen.has(h)) {
    seen.add(h);
    uniqueRows.push(i);
  }
}
// uniqueRows = [0, 1, 3]  — row 2 is a duplicate of row 0
console.log("unique row indices:", uniqueRows);
Click ▶ Run to execute
Ctrl+Enter to run

4 · Controlling index inclusion

By default (index: true), the index label is mixed into the hash. Set index: false to hash only the values.

TypeScript
import { Series, hashPandasObject } from "tsb";

const s = new Series({ data: [42, 42], index: ["a", "b"] });

// index=true (default): different index → different hash
const withIdx = hashPandasObject(s, { index: true });
console.log("index=true, iat(0)===iat(1):", withIdx.iat(0) === withIdx.iat(1)); // false

// index=false: only values matter
const noIdx = hashPandasObject(s, { index: false });
console.log("index=false, iat(0)===iat(1):", noIdx.iat(0) === noIdx.iat(1)); // true
Click ▶ Run to execute
Ctrl+Enter to run

🧪 Scratch Pad

Write your own hashPandasObject code below. All exports from tsb are available.

TypeScript — Scratch Pad
import { Series, DataFrame, hashPandasObject } from "tsb";

// Try it! Hash a Series of numbers.
const nums = new Series({ data: [10, 20, 10, 30] });
const hashes = hashPandasObject(nums, { index: false });

console.log("10===10:", hashes.iat(0) === hashes.iat(2));
console.log("10===20:", hashes.iat(0) === hashes.iat(1));
console.log("all hashes:", [...hashes.values]);
Click ▶ Run to execute
Ctrl+Enter to run