Skip to content

Conversion

Recast data between resolutions, attach labels, and declare rules.

recast_scale

recast_scale(
    data: Any,
    x: NestedScale,
    source: str | None = None,
    target: str | NestedScale | None = None,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    weight: str | Mapping[str, str] | None = None,
    na_action: NaAction = "drop",
    parent: str | None = None,
    collect: bool | None = None,
    diagnostics: Diagnostics = "auto",
    missing_sources: MissingSources = "na",
    batch: int | None = None,
    batch_by: str | None = None,
    **kwargs: Any,
) -> Any

Recast values from one resolution to another.

The central conversion verb. Takes a table keyed by member code at frame source and returns one keyed at target, aggregating (fine to coarse) or disaggregating (coarse to fine) with one rule per value column. Frames that cross-cut work too: the route always goes through the leaves. A crosswalk registered with register_scale_map() short-circuits it.

Columns that are neither the key nor a value column are identifiers (a year, a technology) and are preserved as groups, so panel data recasts in one call. Columns named like the scale's frames are dropped.

Parameters

data The table: pyarrow, pandas, polars, a dict of columns, or a lazy pyarrow Dataset / polars LazyFrame. Returned in the same type. x The scale the codes in data belong to. source Frame of the codes. None infers it: key when that is a frame, else the single frame name among the data's columns. target Target frame of x, or another named scale (its leaf layer is the target, matched on shared leaf codes). key The code column of data. Defaults to source when that column exists, else the scale's leaf column. values Value columns. Default: every numeric column other than the key and the frame columns; numeric identifiers (year) must be excluded by naming the values. A named column must be numeric: a column of strings is an error, never parsed. rule One of SCALE_RULES for every value column, or a dict per column. None looks each column up with get_scale_rule(); a column with no rule anywhere is an error. weight Weight column for sum splits and weighted_mean; one name or a dict per column. None uses the registered weight, else the scale's default; with no declared weights every leaf weighs 1. na_action Leaves with no code at source or target: "drop" (with a warning), "error", or "keep" (an explicit null row, so totals conserve). parent Rule "share" only: the frame the shares are taken within. Default: target when it differs from source, else the frame above. collect Lazy inputs: True materialises and completes the result; otherwise only the observed target members are returned. diagnostics The checks that scan the data (unknown codes, dropped shares, missing sources): "auto" runs them on eager inputs only; "on"/"off" force. missing_sources "na": a target whose sources are only partly present is null. "ignore": aggregate whatever is there (faster, opt-in). batch, batch_by Process in chunks of at most batch values of the identifier column batch_by (default: the first identifier). Chunking by an identifier leaves the numbers unchanged. **kwargs Passed to scale_map().

Returns

The recast table in the input's type, columns target, identifiers, values. Rule "share" keeps the source key and returns each value as a share of its parent-group total. Rows: identifier groups in their order of first appearance in the data, and within each the target codes in member order, a missing-target row last; the rows of a lazy input that is not collected are the observed ones, in no particular order.

Examples

import pyarrow as pa import nestedscales as ns s = ns.scale_example2() d = pa.table({"period": ["p1", "p2", "p3", "p4"], ... "v": [1.0, 2.0, 3.0, 4.0]}) ns.recast_scale(d, s, source="period", target="era", ... rule="sum").to_pylist() [{'era': 'E1', 'v': 3.0}, {'era': 'E2', 'v': 7.0}] y = pa.table({"era": ["E1", "E2"], "v": [8.0, 6.0]}) ns.recast_scale(y, s, source="era", target="period", ... rule="sum").to_pylist()[:2][{'period': 'p1', 'v': 2.0}, {'period': 'p2', 'v': 6.0}]

recast

recast_scale() -- the central conversion verb (port of R/recast.R).

Aggregation and disaggregation are one operation. Every conversion routes through the leaf layer; the route is collapsed into a small crosswalk by scale_map(), so the converter is one join plus one grouped aggregation.

The split factor is fixed for every dimension:

f = w / w_from        when the crosswalk carries weights
f = n_overlap/n_from  otherwise (the equal split)

NA semantics follow R exactly: aggregates do NOT skip nulls (a null member makes its group null), and a target whose sources are only partly present in the data is null (the partial-source poisoning law).

recast

recast(
    x: Any, scale: Any, *args: Any, **kwargs: Any
) -> Any

Recast data x through scale, dispatching on the scale's class.

For a NestedScale the arguments are those of recast_scale(): target is the target frame (or scale) and source the source frame, inferred when omitted.

Dimension packages register methods with recast.register(TheirClass). A method is called with the SCALE first and the data second, the two leading arguments of recast() swapped, followed by the remaining arguments unchanged: method(scale, x, *args, **kwargs).

Examples

import pyarrow as pa import nestedscales as ns d = pa.table({"period": ["p1", "p2", "p3", "p4"], "v": [1.0, 2, 3, 4]}) ns.recast(d, ns.scale_example2(), "era", rule="sum").to_pylist() [{'era': 'E1', 'v': 3.0}, {'era': 'E2', 'v': 7.0}]

recast_crosswalk

recast_crosswalk(
    data: Any,
    map: Any,
    source: str,
    target: str,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    ids: Sequence[str] | None = None,
    by: Sequence[str] = (),
    targets: Sequence[str | None] | None = None,
    missing_sources: MissingSources = "na",
    member: str = "member",
    collect: bool | None = None,
) -> Any

Recast values through a given crosswalk.

map has one row per (source, target) pair: the codes in columns source and target, n_from, n_overlap, optionally w (weighted-mean weight; default n_overlap) and w_from (with both, sum splits by w / w_from; otherwise by n_overlap / n_from), and any by columns.

With targets, the result is completed to those codes and ordered as recast_scale() orders its result. Without, it holds the observed targets, sorted by the identifier columns and then by the target code.

recast_from_leaves

recast_from_leaves(
    data: Any,
    x: NestedScale,
    target: str,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    weight: str | None = None,
    na_rm: bool = False,
    na_action: NaAction = "drop",
    collect: bool | None = None,
) -> Any

Aggregate leaf-keyed data UP into the members of frame target.

weight names a column of the data to weight by (so it may vary by identifier); None uses a weight column if present, else the scale's declared weight. With na_rm=True a null member says nothing rather than making its group null. Unlike recast_scale(), a missing leaf simply does not contribute (no partial-source poisoning).

recast_scale

recast_scale(
    data: Any,
    x: NestedScale,
    source: str | None = None,
    target: str | NestedScale | None = None,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    weight: str | Mapping[str, str] | None = None,
    na_action: NaAction = "drop",
    parent: str | None = None,
    collect: bool | None = None,
    diagnostics: Diagnostics = "auto",
    missing_sources: MissingSources = "na",
    batch: int | None = None,
    batch_by: str | None = None,
    **kwargs: Any,
) -> Any

Recast values from one resolution to another.

The central conversion verb. Takes a table keyed by member code at frame source and returns one keyed at target, aggregating (fine to coarse) or disaggregating (coarse to fine) with one rule per value column. Frames that cross-cut work too: the route always goes through the leaves. A crosswalk registered with register_scale_map() short-circuits it.

Columns that are neither the key nor a value column are identifiers (a year, a technology) and are preserved as groups, so panel data recasts in one call. Columns named like the scale's frames are dropped.

Parameters

data The table: pyarrow, pandas, polars, a dict of columns, or a lazy pyarrow Dataset / polars LazyFrame. Returned in the same type. x The scale the codes in data belong to. source Frame of the codes. None infers it: key when that is a frame, else the single frame name among the data's columns. target Target frame of x, or another named scale (its leaf layer is the target, matched on shared leaf codes). key The code column of data. Defaults to source when that column exists, else the scale's leaf column. values Value columns. Default: every numeric column other than the key and the frame columns; numeric identifiers (year) must be excluded by naming the values. A named column must be numeric: a column of strings is an error, never parsed. rule One of SCALE_RULES for every value column, or a dict per column. None looks each column up with get_scale_rule(); a column with no rule anywhere is an error. weight Weight column for sum splits and weighted_mean; one name or a dict per column. None uses the registered weight, else the scale's default; with no declared weights every leaf weighs 1. na_action Leaves with no code at source or target: "drop" (with a warning), "error", or "keep" (an explicit null row, so totals conserve). parent Rule "share" only: the frame the shares are taken within. Default: target when it differs from source, else the frame above. collect Lazy inputs: True materialises and completes the result; otherwise only the observed target members are returned. diagnostics The checks that scan the data (unknown codes, dropped shares, missing sources): "auto" runs them on eager inputs only; "on"/"off" force. missing_sources "na": a target whose sources are only partly present is null. "ignore": aggregate whatever is there (faster, opt-in). batch, batch_by Process in chunks of at most batch values of the identifier column batch_by (default: the first identifier). Chunking by an identifier leaves the numbers unchanged. **kwargs Passed to scale_map().

Returns

The recast table in the input's type, columns target, identifiers, values. Rule "share" keeps the source key and returns each value as a share of its parent-group total. Rows: identifier groups in their order of first appearance in the data, and within each the target codes in member order, a missing-target row last; the rows of a lazy input that is not collected are the observed ones, in no particular order.

Examples

import pyarrow as pa import nestedscales as ns s = ns.scale_example2() d = pa.table({"period": ["p1", "p2", "p3", "p4"], ... "v": [1.0, 2.0, 3.0, 4.0]}) ns.recast_scale(d, s, source="period", target="era", ... rule="sum").to_pylist() [{'era': 'E1', 'v': 3.0}, {'era': 'E2', 'v': 7.0}] y = pa.table({"era": ["E1", "E2"], "v": [8.0, 6.0]}) ns.recast_scale(y, s, source="era", target="period", ... rule="sum").to_pylist()[:2][{'period': 'p1', 'v': 2.0}, {'period': 'p2', 'v': 6.0}]

recast_to_leaves

recast_to_leaves(
    data: Any,
    x: NestedScale,
    source: str | None = None,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    weight: str | Mapping[str, str] | None = None,
    attach_weight: bool = True,
    collect: bool | None = None,
) -> Any

Project member-keyed data DOWN to the leaf layer (one row per leaf).

Extensive columns (rule sum) are split across a member's leaves by weight so totals conserve; intensive columns are repeated. A weight column is attached so the return trip's weighted_mean reproduces the weighting.

The rows follow the data: one block per data row, in data order, and within a block the member's leaves in leaftable order.

recast_to_leaves

recast_to_leaves(
    data: Any,
    x: NestedScale,
    source: str | None = None,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    weight: str | Mapping[str, str] | None = None,
    attach_weight: bool = True,
    collect: bool | None = None,
) -> Any

Project member-keyed data DOWN to the leaf layer (one row per leaf).

Extensive columns (rule sum) are split across a member's leaves by weight so totals conserve; intensive columns are repeated. A weight column is attached so the return trip's weighted_mean reproduces the weighting.

The rows follow the data: one block per data row, in data order, and within a block the member's leaves in leaftable order.

recast_from_leaves

recast_from_leaves(
    data: Any,
    x: NestedScale,
    target: str,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    weight: str | None = None,
    na_rm: bool = False,
    na_action: NaAction = "drop",
    collect: bool | None = None,
) -> Any

Aggregate leaf-keyed data UP into the members of frame target.

weight names a column of the data to weight by (so it may vary by identifier); None uses a weight column if present, else the scale's declared weight. With na_rm=True a null member says nothing rather than making its group null. Unlike recast_scale(), a missing leaf simply does not contribute (no partial-source poisoning).

recast_crosswalk

recast_crosswalk(
    data: Any,
    map: Any,
    source: str,
    target: str,
    key: str | None = None,
    values: str | Sequence[str] | None = None,
    rule: str | Mapping[str, str] | None = None,
    ids: Sequence[str] | None = None,
    by: Sequence[str] = (),
    targets: Sequence[str | None] | None = None,
    missing_sources: MissingSources = "na",
    member: str = "member",
    collect: bool | None = None,
) -> Any

Recast values through a given crosswalk.

map has one row per (source, target) pair: the codes in columns source and target, n_from, n_overlap, optionally w (weighted-mean weight; default n_overlap) and w_from (with both, sum splits by w / w_from; otherwise by n_overlap / n_from), and any by columns.

With targets, the result is completed to those codes and ordered as recast_scale() orders its result. Without, it holds the observed targets, sorted by the identifier columns and then by the target code.

join_scale

join_scale(
    data: Any,
    x: NestedScale,
    key: str | None = None,
    frame: str | None = None,
    attach: bool | str | Sequence[str] | None = None,
    meta: bool = False,
    weight: str | None = None,
    as_factor: bool = True,
    collect: bool | None = None,
    diagnostics: Diagnostics = "auto",
    labels: bool | str | None = None,
) -> Any

Attach a code column named after the scale, optionally the codes of coarser frames ("<name>.<frame>"), the display names of the keyed members ("<name>.label"; labels=True for the default variant or the name of one) and the member's share and weight ("<name>.share", "<name>.weight"). Existing columns are never overwritten. With as_factor, memberships are dictionary columns over the frame's full vocabulary.

SCALE_RULES module-attribute

SCALE_RULES = (
    "sum",
    "weighted_mean",
    "mean",
    "copy",
    "sd",
    "share",
    "logshare",
)

register_scale_rule

register_scale_rule(
    param: str,
    rule: str,
    weight: str | None = None,
    scope: str | None = None,
) -> dict

Record the rule (and optionally the weight) for a value column.

get_scale_rule

get_scale_rule(
    param: str, scope: str | None = None
) -> dict | None

The registered entry (rule, weight, scope), or None.

A scoped lookup prefers its own scope and falls back to unscoped.

list_scale_rules

list_scale_rules(scope: str | None = None) -> pa.Table

Registered rules: columns param, rule, weight, scope.

clear_scale_rules

clear_scale_rules(
    name: str | Sequence[str] | None = None,
    scope: str | None = None,
) -> None

Remove rules: the named value columns (within scope), every rule of one scope, or, with neither, everything.