|
| 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>tsb — toDictOriented / fromDictOriented</title> |
| 7 | + <style> |
| 8 | + body { font-family: system-ui, sans-serif; max-width: 860px; margin: 2rem auto; padding: 0 1rem; line-height: 1.6; color: #1a1a1a; } |
| 9 | + h1 { color: #0066cc; } |
| 10 | + h2 { color: #333; border-bottom: 1px solid #eee; padding-bottom: 0.3em; } |
| 11 | + pre { background: #f6f8fa; border-radius: 6px; padding: 1rem; overflow-x: auto; font-size: 0.9em; } |
| 12 | + code { font-family: 'Fira Code', 'Cascadia Code', monospace; } |
| 13 | + .note { background: #fffbea; border-left: 4px solid #f5a623; padding: 0.7rem 1rem; border-radius: 0 6px 6px 0; margin: 1rem 0; } |
| 14 | + table { border-collapse: collapse; width: 100%; margin: 1rem 0; } |
| 15 | + th, td { border: 1px solid #ddd; padding: 0.5rem 0.75rem; text-align: left; } |
| 16 | + th { background: #f0f4f8; } |
| 17 | + a { color: #0066cc; } |
| 18 | + </style> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | + <p><a href="index.html">← tsb playground</a></p> |
| 22 | + |
| 23 | + <h1><code>toDictOriented</code> / <code>fromDictOriented</code></h1> |
| 24 | + <p> |
| 25 | + Convert a DataFrame to and from dictionary structures with flexible orientation — mirrors |
| 26 | + <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_dict.html" target="_blank"> |
| 27 | + <code>pandas.DataFrame.to_dict(orient=...)</code></a> and |
| 28 | + <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from_dict.html" target="_blank"> |
| 29 | + <code>pandas.DataFrame.from_dict(orient=...)</code></a>. |
| 30 | + </p> |
| 31 | + |
| 32 | + <h2>Supported orientations — <code>toDictOriented</code></h2> |
| 33 | + <table> |
| 34 | + <thead><tr><th>Orient</th><th>Return type</th><th>Description</th></tr></thead> |
| 35 | + <tbody> |
| 36 | + <tr><td><code>"dict"</code> / <code>"columns"</code></td><td><code>Record<col, Record<rowLabel, value>></code></td><td>Nested column → row-label → value map</td></tr> |
| 37 | + <tr><td><code>"list"</code></td><td><code>Record<col, value[]></code></td><td>Column name → array of values</td></tr> |
| 38 | + <tr><td><code>"series"</code></td><td><code>Record<col, Series></code></td><td>Column name → Series object</td></tr> |
| 39 | + <tr><td><code>"split"</code></td><td><code>{ index, columns, data }</code></td><td>Serialisable split structure</td></tr> |
| 40 | + <tr><td><code>"tight"</code></td><td><code>{ index, columns, data, index_names, column_names }</code></td><td>Split plus axis-name metadata</td></tr> |
| 41 | + <tr><td><code>"records"</code></td><td><code>Record<col, value>[]</code></td><td>Array of row objects</td></tr> |
| 42 | + <tr><td><code>"index"</code></td><td><code>Record<rowLabel, Record<col, value>></code></td><td>Row-label → column → value</td></tr> |
| 43 | + </tbody> |
| 44 | + </table> |
| 45 | + |
| 46 | + <h2>Supported orientations — <code>fromDictOriented</code></h2> |
| 47 | + <table> |
| 48 | + <thead><tr><th>Orient</th><th>Input shape</th></tr></thead> |
| 49 | + <tbody> |
| 50 | + <tr><td><code>"columns"</code> (default)</td><td><code>{ col: value[] }</code></td></tr> |
| 51 | + <tr><td><code>"index"</code></td><td><code>{ rowLabel: { col: value } }</code></td></tr> |
| 52 | + <tr><td><code>"split"</code></td><td><code>{ index?, columns, data }</code></td></tr> |
| 53 | + <tr><td><code>"tight"</code></td><td>Same as <code>"split"</code>, extra fields ignored</td></tr> |
| 54 | + </tbody> |
| 55 | + </table> |
| 56 | + |
| 57 | + <h2>Example — all orientations</h2> |
| 58 | + <pre><code>import { DataFrame } from "tsb"; |
| 59 | +import { toDictOriented, fromDictOriented } from "tsb"; |
| 60 | + |
| 61 | +const df = DataFrame.fromColumns( |
| 62 | + { name: ["Alice", "Bob"], score: [92, 85] }, |
| 63 | + { index: new Index(["r0", "r1"]) }, |
| 64 | +); |
| 65 | + |
| 66 | +// "dict" / "columns" |
| 67 | +toDictOriented(df, "dict"); |
| 68 | +// { name: { r0: "Alice", r1: "Bob" }, score: { r0: 92, r1: 85 } } |
| 69 | + |
| 70 | +// "list" |
| 71 | +toDictOriented(df, "list"); |
| 72 | +// { name: ["Alice", "Bob"], score: [92, 85] } |
| 73 | + |
| 74 | +// "records" |
| 75 | +toDictOriented(df, "records"); |
| 76 | +// [ { name: "Alice", score: 92 }, { name: "Bob", score: 85 } ] |
| 77 | + |
| 78 | +// "split" |
| 79 | +toDictOriented(df, "split"); |
| 80 | +// { index: ["r0", "r1"], columns: ["name", "score"], data: [["Alice", 92], ["Bob", 85]] } |
| 81 | + |
| 82 | +// "index" |
| 83 | +toDictOriented(df, "index"); |
| 84 | +// { r0: { name: "Alice", score: 92 }, r1: { name: "Bob", score: 85 } } |
| 85 | + |
| 86 | +// fromDictOriented — columns (default) |
| 87 | +fromDictOriented({ name: ["Alice", "Bob"], score: [92, 85] }); |
| 88 | + |
| 89 | +// fromDictOriented — index |
| 90 | +fromDictOriented( |
| 91 | + { r0: { name: "Alice", score: 92 }, r1: { name: "Bob", score: 85 } }, |
| 92 | + "index", |
| 93 | +); |
| 94 | + |
| 95 | +// fromDictOriented — split (round-trip) |
| 96 | +const split = toDictOriented(df, "split"); |
| 97 | +const df2 = fromDictOriented(split, "split"); |
| 98 | +// df2 is equivalent to df |
| 99 | +</code></pre> |
| 100 | + |
| 101 | + <h2>Missing values</h2> |
| 102 | + <div class="note"> |
| 103 | + Missing values (<code>null</code> / <code>undefined</code>) are preserved as <code>null</code> |
| 104 | + in all orientations. When using <code>fromDictOriented</code> with <code>"index"</code> |
| 105 | + orientation, any column that is absent from a given row object is filled with <code>null</code>. |
| 106 | + </div> |
| 107 | + |
| 108 | + <h2>Type signatures</h2> |
| 109 | + <pre><code>function toDictOriented(df: DataFrame, orient: "dict" | "columns"): Record<string, Record<string, Scalar>>; |
| 110 | +function toDictOriented(df: DataFrame, orient: "list"): Record<string, Scalar[]>; |
| 111 | +function toDictOriented(df: DataFrame, orient: "series"): Record<string, Series<Scalar>>; |
| 112 | +function toDictOriented(df: DataFrame, orient: "split"): DictSplit; |
| 113 | +function toDictOriented(df: DataFrame, orient: "tight"): DictTight; |
| 114 | +function toDictOriented(df: DataFrame, orient: "records"): Record<string, Scalar>[]; |
| 115 | +function toDictOriented(df: DataFrame, orient: "index"): Record<string, Record<string, Scalar>>; |
| 116 | + |
| 117 | +function fromDictOriented(data: Record<string, readonly Scalar[]>, orient?: "columns"): DataFrame; |
| 118 | +function fromDictOriented(data: Record<string, Record<string, Scalar>>, orient: "index"): DataFrame; |
| 119 | +function fromDictOriented(data: SplitInput, orient: "split" | "tight"): DataFrame; |
| 120 | +</code></pre> |
| 121 | +</body> |
| 122 | +</html> |
0 commit comments