Skip to main content

load_dataset()

Load a bundled dataset by name.

Usage

Source

load_dataset(
    name,
    *,
    backend=None,
)

Greenwood ships several classic survival-analysis datasets from R’s survival package, stored as gzipped CSVs. This function decompresses them on the fly and returns a DataFrame in your preferred backend.

Parameters

name: str

One of available_datasets() (e.g., "lung", "veteran", "ovarian", "pbc", "pbcseq", "colon", "mgus2", "e1684").

backend: str | None = None
"pandas", "polars", or "pyarrow". When left as None (the default), Greenwood picks a backend for you: it prefers Polars if it is installed, otherwise uses Pandas, and raises if neither is available.

Returns

DataFrame
A Polars DataFrame, a Pandas DataFrame, or a PyArrow Table with the dataset contents. Columns that are factors in R (veteran celltype, colon rx, and sex in pbc, pbcseq, and mgus2) are categoricals with R’s levels in R’s order: a Polars Enum, a Pandas Categorical, or an Arrow dictionary column. Models then take the first level as the reference. Curves and tests come in level order for the Polars and Pandas frames. An Arrow dictionary sorts by value, as Arrow itself does, so its groups come in sorted order.

Examples

Load the NCCTG lung cancer dataset as a Polars DataFrame:

import greenwood as gw

# Load the NCCTG lung cancer dataset as a Polars DataFrame
lung = gw.load_dataset("lung", backend="polars")
lung.head()
shape: (5, 10)
insttimestatusagesexph.ecogph.karnopat.karnomeal.calwt.loss
i64i64i64i64i64i64i64i64i64i64
330627411901001175null
3455268109090122515
31010156109090null15
5210257119060115011
18832601010090null0

The cell type in the veteran dataset keeps R’s levels, with squamous first:

# The cell type is an Enum with R's levels
veteran = gw.load_dataset("veteran", backend="polars")
veteran["celltype"].dtype
Enum(categories=['squamous', 'smallcell', 'adeno', 'large'])

See all available dataset names:

# List all available dataset names
gw.available_datasets()
['colon', 'e1684', 'lung', 'mgus2', 'ovarian', 'pbc', 'pbcseq', 'veteran']