Skip to content

deepbox/dataframe

IO, Styling & Plotting

Read and write CSV, JSON, Parquet and Excel data, style tables as HTML or ANSI text, and plot columns through df.plot.

  • IO
  • Styling

Overview

  • Parquet and Excel work on bytes, not paths. df.toParquet() and df.toXlsx() return a Uint8Array, and DataFrame.fromParquet(bytes) and DataFrame.fromXlsx(bytes) read one. The standalone writeParquet(columns, rows), readParquet(bytes), writeXlsx and readXlsx do the same with plain row objects. Write the bytes to disk with node:fs.
  • CSV and JSON are DataFrame methods: fromCsvString, readCsv, toCsvString, toCsv, fromJsonString, readJson, toJsonString and toJson. The file variants are asynchronous and need Node.js.
  • readParquet reads flat schemas with plain and dictionary encodings, in uncompressed, Snappy or gzip (Node.js only) form, which covers the defaults of pyarrow and pandas. Nested columns, DECIMAL, INT96, delta encodings, ZSTD, LZ4 and Brotli throw instead of returning wrong data. Invalid buffers throw DataValidationError.
  • Excel date cells come back as serial numbers, and writeXlsx writes Date values as ISO text. Neither format stores the row index, so reading gives a default 0 to n-1 index.
  • df.style builds conditional formatting and renders it with toHTML() or toANSI(). The df.plot methods (line, bar, hist, scatter, box, pie, area) return a Figure that you render or save.

Types

10

ParquetReadOptions

type

Options for reading a Parquet file.

  • readonly columns?: readonly string[]

    Columns to read, returned in the order given.

ParquetReadResult

type

Result of readParquet: the column names and one record per row.

  • columns: string[]
  • data: Record<string, unknown>[]

ParquetWriteOptions

type

Options for writing a Parquet file.

  • readonly createdBy?: string

    Created-by string.

XlsxCell

type

A cell value as returned by readXlsx.

type XlsxCell = string | number | boolean | null;

XlsxReadOptions

type

Options for reading an XLSX file.

  • readonly sheet?: string | number

    Sheet to read: a sheet name, or a 0-based position in workbook order.

  • readonly header?: boolean

    Whether the first row contains headers.

  • readonly maxEntryBytes?: number

    Maximum decompressed size per zip entry in bytes (guards against decompression bombs in untrusted files).

  • readonly maxCells?: number

    Maximum number of cells (rows times columns of the used range) the result may hold.

XlsxReadResult

type

Result of readXlsx: the header names and one record per row.

  • columns: string[]
  • data: Record<string, XlsxCell>[]

XlsxWriteOptions

type

Options for writing an XLSX file.

  • readonly sheetName?: string

    Sheet name.

PlotBaseOptions

type

Options shared by every plot method.

  • readonly figsize?: readonly [ number, number ]

    Figure size as [width, height] in pixels (default: [640, 480]).

  • readonly title?: string

    Title drawn above the axes.

CellStyle

type

A single cell style rule.

  • readonly color?: string
  • readonly backgroundColor?: string
  • readonly fontWeight?: string
  • readonly fontStyle?: string
  • readonly textAlign?: string

StyleFunction

type

A formatting function that receives a cell value and returns a CSS-like style object.

type StyleFunction = (value: unknown, row: number, col: number) => CellStyle;

Classes

2

PlotAccessor

Plotting accessor for DataFrame.

Access via df.plot.line(), df.plot.bar(), etc. Every method returns the Figure, which can be rendered with renderSVG() or saved. Columns that are named in the options must exist; otherwise an InvalidParameterError lists the available columns. When no columns are named, only numeric columns are used. Missing values (null, undefined, NaN) are skipped by the underlying plots.

Example
import { DataFrame } from 'deepbox/dataframe'; const df = new DataFrame({ x: [1, 2, 3], y: [4, 5, 6] });const fig = df.plot.line({ x: 'x', y: 'y' });
Members (8)
  • line(options?: PlotBaseOptions & { readonly x?: string; readonly y?: string | readonly string[]; }): Figure

    Line plot.

  • bar(options?: PlotBaseOptions & { readonly x?: string; readonly y?: string | readonly string[]; }): Figure

    Bar plot.

  • barh(options?: PlotBaseOptions & { readonly x?: string; readonly y?: string; }): Figure

    Horizontal bar plot.

  • hist(options?: PlotBaseOptions & { readonly column?: string | readonly string[]; readonly bins?: number; }): Figure

    Histogram.

  • scatter(options: PlotBaseOptions & { readonly x: string; readonly y: string; }): Figure

    Scatter plot.

  • box(options?: PlotBaseOptions & { readonly column?: string | readonly string[]; }): Figure

    Box plot.

  • pie(options: PlotBaseOptions & { readonly y: string; readonly labels?: string; }): Figure

    Pie chart from a single column.

  • area(options?: PlotBaseOptions & { readonly x?: string; readonly y?: string | readonly string[]; }): Figure

    Area plot (filled line plot).

StyleAccessor

Style accessor for DataFrame.

Access via df.style. Allows chaining conditional formatting rules and rendering to HTML or plain-text tables. Rules that look at a whole column (highlightMax, backgroundGradient, ...) read the DataFrame when they are added. Rules apply in the order they were added, and a later rule overrides an earlier one for the same CSS property.

Example
import { DataFrame } from 'deepbox/dataframe'; const df = new DataFrame({ a: [1, 2, 3], b: [4, 5, 6] });const html = df.style  .highlightMax({ color: 'green' })  .highlightMin({ color: 'red' })  .toHTML();
Members (15)
  • setCaption(caption: string): this

    Set a caption for the styled table.

  • applymap(fn: StyleFunction): this

    Apply a generic style function to all cells.

  • map(fn: StyleFunction): this

    Alias of StyleAccessor.applymap, the name pandas uses since 2.1.

  • highlightMax(style?: CellStyle): this

    Highlight the maximum value in each column.

  • highlight_max(style?: CellStyle): thisdeprecated

    Same as StyleAccessor.highlightMax.

  • highlightMin(style?: CellStyle): this

    Highlight the minimum value in each column.

  • highlight_min(style?: CellStyle): thisdeprecated

    Same as StyleAccessor.highlightMin.

  • highlightNull(style?: CellStyle): this

    Highlight missing values: null, undefined and NaN.

  • highlight_null(style?: CellStyle): thisdeprecated

    Same as StyleAccessor.highlightNull.

  • backgroundGradient(low?: string, high?: string): this

    Apply a color gradient (background) based on numeric values.

  • background_gradient(low?: string, high?: string): thisdeprecated

    Same as StyleAccessor.backgroundGradient.

  • bar(color?: string): this

    Draw an inline bar in each numeric cell.

  • format(column: string, formatter: (value: unknown) => string): this

    Set a formatter for a specific column.

  • toHTML(): string

    Render the styled DataFrame as an HTML table string.

  • toANSI(): string

    Render a plain-text table with ANSI escape codes.

Functions

4

readParquet

function readParquet(buffer: Uint8Array, options?: ParquetReadOptions): ParquetReadResult;

Read column data from a Parquet buffer.

Details

Supports flat schemas with any number of row groups and data pages (v1 and v2), PLAIN / dictionary / RLE-boolean encodings and UNCOMPRESSED, SNAPPY or GZIP (Node.js only) compression, which covers the defaults of pyarrow and pandas. Anything outside that subset (nested columns, DECIMAL, INT96, delta encodings, ZSTD/LZ4/Brotli) throws a descriptive error instead of returning wrong data.

Value mapping: BOOLEAN to boolean, INT32/FLOAT/DOUBLE to number, INT64 to number (or bigint when the value is beyond +/-2^53), strings to string, unannotated BYTE_ARRAY to Uint8Array, DATE and TIMESTAMP columns to Date (nanoseconds are truncated to milliseconds), unsigned integers to non-negative values, nulls to null.

Parameters

buffer
Uint8Array
The raw .parquet file as a Uint8Array
optionsoptional
ParquetReadOptions
Read options (columns selects and orders a subset)

Returns

ParquetReadResultParsed data with columns and rows

Throws

  • DataValidationErrorIf the buffer is not a valid or supported Parquet file
  • InvalidParameterErrorIf options.columns names a column the file does not have

readXlsx

function readXlsx(buffer: Uint8Array, options?: XlsxReadOptions): XlsxReadResult;

Read cell data from an XLSX buffer.

Details

Returns an object with columns (header names) and data (array of objects mapping column names to values).

Empty cells are null, formula cells give their cached result (formula errors become null), and numeric cells are numbers (dates are Excel serial numbers). Rows keep their sheet positions, so blank rows inside the data stay as rows of null; blank rows before the data are dropped. Blank rows after the data are dropped too, except plain empty rows inside the sheet's declared dimension, which is how writeXlsx stores all-null rows. Trailing empty columns are dropped. Blank or missing header cells are named Column<i> and repeated headers are suffixed .1, .2, ... like pandas.

Parameters

buffer
Uint8Array
The raw .xlsx file as a Uint8Array
optionsoptional
XlsxReadOptions
Read options

Returns

XlsxReadResultParsed spreadsheet data

Throws

  • DataValidationErrorIf the buffer is not a valid .xlsx file, the sheet does not exist, an XML element is not closed, or the used range exceeds maxCells
  • InvalidParameterErrorIf maxEntryBytes or maxCells is not a positive number

writeParquet

function writeParquet(columns: readonly string[], data: readonly Record<string, unknown>[], options?: ParquetWriteOptions): Uint8Array;

Write DataFrame-like data to a Parquet buffer.

Details

Column types are inferred from every value of the column: booleans become BOOLEAN, Dates become TIMESTAMP_MILLIS, integers become INT32 (or INT64 when any value exceeds the 32-bit range or is a bigint), other numbers become DOUBLE, and everything else (strings, mixed columns) is stored as UTF-8 text. Invalid dates are written as nulls.

Columns containing null/undefined are written as OPTIONAL with RLE definition levels, so nulls survive the round-trip (also when read by external tools such as pyarrow/pandas). NaN is a regular DOUBLE value, not a null.

Parameters

columns
readonly string[]
Column names (must be unique)
data
readonly Record<string, unknown>[]
Array of row objects
optionsoptional
ParquetWriteOptions
Write options

Returns

Uint8ArrayUint8Array containing the .parquet file

Throws

  • DataValidationErrorIf column names repeat, a row is not an object, a bigint exceeds the INT64 range, or a column chunk is larger than 2 GiB

writeXlsx

function writeXlsx(columns: readonly string[], data: readonly Record<string, unknown>[], options?: XlsxWriteOptions): Uint8Array;

Write DataFrame-like data to an XLSX buffer.

Value mapping: numbers become numeric cells (NaN and null/undefined become empty cells, Infinity/-Infinity are written as the text "inf"/"-inf" since Excel has no such numbers), booleans become boolean cells, safe-integer bigints become numbers, Dates become ISO 8601 text (invalid dates are empty), and everything else is written as text.

Parameters

columns
readonly string[]
Column names (header row); must be unique
data
readonly Record<string, unknown>[]
Array of row objects (column name to value)
optionsoptional
XlsxWriteOptions
Write options

Returns

Uint8ArrayUint8Array containing the .xlsx file

Throws

  • DataValidationErrorIf column names repeat, exceed Excel's sheet limits, or a text cell is longer than 32767 characters
  • InvalidParameterErrorIf sheetName is not a valid Excel sheet name
dataframe-io-styling.ts
import { DataFrame } from "deepbox/dataframe"; const df = new DataFrame({  name: ["A", "B", "C"],  score: [88, 74, 95],}); const xlsx = df.toXlsx({ sheetName: "Scores" });const parquet = df.toParquet();console.log(DataFrame.fromXlsx(xlsx).toString());console.log(DataFrame.fromParquet(parquet, { columns: ["score"] }).toString());console.log(df.toCsvString()); const html = df.style.highlightMax().toHTML();const figure = df.plot.bar({ x: "name", y: "score" });console.log(html.length > 0, figure.renderSVG().svg.length > 0);