|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>combine — Element-wise Combination — tsb playground</title> |
| 7 | + <style> |
| 8 | + body { font-family: system-ui, sans-serif; max-width: 900px; margin: 2rem auto; padding: 0 1rem; } |
| 9 | + h1 { color: #1a56db; } |
| 10 | + pre { background: #f4f4f4; padding: 1rem; border-radius: 6px; overflow-x: auto; } |
| 11 | + .output { background: #e8f5e9; padding: 1rem; border-radius: 6px; white-space: pre; font-family: monospace; min-height: 4rem; } |
| 12 | + button { background: #1a56db; color: white; border: none; padding: 0.5rem 1.2rem; border-radius: 4px; cursor: pointer; margin: 0.2rem; } |
| 13 | + button:hover { background: #1345b3; } |
| 14 | + </style> |
| 15 | +</head> |
| 16 | +<body> |
| 17 | + <h1>combine — Element-wise Combination</h1> |
| 18 | + <p> |
| 19 | + <code>combineSeries(a, b, func)</code> and <code>combineDataFrame(a, b, func)</code> |
| 20 | + combine two objects element-wise using a caller-supplied binary function. |
| 21 | + The result index is the <strong>union</strong> of both indices; a |
| 22 | + <code>fillValue</code> (default <code>null</code>) is used when only one |
| 23 | + side has a value for a given label. |
| 24 | + </p> |
| 25 | + |
| 26 | + <h2>Interactive Demo</h2> |
| 27 | + <button onclick="runSeriesMax()">Series: max</button> |
| 28 | + <button onclick="runSeriesUnion()">Series: union index</button> |
| 29 | + <button onclick="runDfShared()">DataFrame: shared columns</button> |
| 30 | + <button onclick="runDfUnion()">DataFrame: union columns</button> |
| 31 | + <div class="output" id="output">Click a button above to run an example.</div> |
| 32 | + |
| 33 | + <h2>Code Examples</h2> |
| 34 | + <pre><code>import { Series, DataFrame, combineSeries, combineDataFrame } from "tsb"; |
| 35 | + |
| 36 | +// ── Series ────────────────────────────────────────────────────────────────── |
| 37 | +const a = new Series({ data: [1, 5, 3], index: [0, 1, 2] }); |
| 38 | +const b = new Series({ data: [10, 2, 30], index: [0, 1, 2] }); |
| 39 | + |
| 40 | +// Element-wise max |
| 41 | +combineSeries(a, b, (x, y) => Math.max(x, y)).values; // [10, 5, 30] |
| 42 | + |
| 43 | +// Union index with fillValue=0 |
| 44 | +const c = new Series({ data: [1, 2], index: ["x", "y"] }); |
| 45 | +const d = new Series({ data: [10, 30], index: ["x", "z"] }); |
| 46 | +combineSeries(c, d, (x, y) => (x ?? 0) + (y ?? 0), 0).values; |
| 47 | +// x:11, y:2, z:30 |
| 48 | + |
| 49 | +// ── DataFrame ─────────────────────────────────────────────────────────────── |
| 50 | +const df1 = DataFrame.fromColumns({ a: [1, 5], b: [100, 200] }); |
| 51 | +const df2 = DataFrame.fromColumns({ a: [10, 2], c: [1000, 2000] }); |
| 52 | + |
| 53 | +// Shared column "a": element-wise min; unshared columns processed with fillValue |
| 54 | +combineDataFrame(df1, df2, (p, q) => Math.min(p ?? Infinity, q ?? Infinity)); |
| 55 | + |
| 56 | +// overwrite: false — unshared columns preserved as-is |
| 57 | +combineDataFrame(df1, df2, (p, q) => Math.min(p ?? Infinity, q ?? Infinity), |
| 58 | + { overwrite: false }); |
| 59 | +</code></pre> |
| 60 | + |
| 61 | + <script type="module"> |
| 62 | + const mod = await import("../src/index.ts").catch(() => null); |
| 63 | + |
| 64 | + function display(label, text) { |
| 65 | + document.getElementById("output").textContent = label + "\n\n" + text; |
| 66 | + } |
| 67 | + |
| 68 | + window.runSeriesMax = function () { |
| 69 | + if (!mod) { display("", "(Module unavailable — run via bun)"); return; } |
| 70 | + const { Series, combineSeries } = mod; |
| 71 | + const a = new Series({ data: [1, 5, 3], index: [0, 1, 2] }); |
| 72 | + const b = new Series({ data: [10, 2, 30], index: [0, 1, 2] }); |
| 73 | + const result = combineSeries(a, b, (x, y) => Math.max(x, y)); |
| 74 | + display("combineSeries(a, b, Math.max):", String(result)); |
| 75 | + }; |
| 76 | + |
| 77 | + window.runSeriesUnion = function () { |
| 78 | + if (!mod) { display("", "(Module unavailable — run via bun)"); return; } |
| 79 | + const { Series, combineSeries } = mod; |
| 80 | + const c = new Series({ data: [1, 2], index: ["x", "y"] }); |
| 81 | + const d = new Series({ data: [10, 30], index: ["x", "z"] }); |
| 82 | + const result = combineSeries(c, d, (x, y) => (x ?? 0) + (y ?? 0), 0); |
| 83 | + display("combineSeries with union index (fillValue=0):", String(result)); |
| 84 | + }; |
| 85 | + |
| 86 | + window.runDfShared = function () { |
| 87 | + if (!mod) { display("", "(Module unavailable — run via bun)"); return; } |
| 88 | + const { DataFrame, combineDataFrame } = mod; |
| 89 | + const df1 = DataFrame.fromColumns({ a: [1, 5, 3], b: [100, 200, 300] }); |
| 90 | + const df2 = DataFrame.fromColumns({ a: [10, 2, 30], b: [50, 250, 150] }); |
| 91 | + const result = combineDataFrame(df1, df2, (p, q) => Math.max(p, q)); |
| 92 | + display("combineDataFrame(df1, df2, Math.max) — shared columns:", String(result)); |
| 93 | + }; |
| 94 | + |
| 95 | + window.runDfUnion = function () { |
| 96 | + if (!mod) { display("", "(Module unavailable — run via bun)"); return; } |
| 97 | + const { DataFrame, combineDataFrame } = mod; |
| 98 | + const df1 = DataFrame.fromColumns({ a: [1, 2], b: [10, 20] }); |
| 99 | + const df2 = DataFrame.fromColumns({ a: [100, 200], c: [1000, 2000] }); |
| 100 | + const result = combineDataFrame(df1, df2, (p, q) => { |
| 101 | + if (p === null) return q; |
| 102 | + if (q === null) return p; |
| 103 | + return Math.min(p, q); |
| 104 | + }, { overwrite: false }); |
| 105 | + display("combineDataFrame — union columns, overwrite=false:", String(result)); |
| 106 | + }; |
| 107 | + </script> |
| 108 | +</body> |
| 109 | +</html> |
0 commit comments