Skip to content

nanosdk.frames

Turn decoded responses into DataFrames.

Use the converters as a route map, so the route returns a DataFrame, or call them on anything iterable of models or dicts, including a paginated route's .iter():

class Api(nanosdk.Client):
    posts = nanosdk.get("/posts", response=list[Post]).map(nanosdk.frames.polars)

df = api.posts()                                    # pl.DataFrame
df = nanosdk.frames.pandas(api.list_posts.iter())   # every page, one pd.DataFrame

polars and pandas are optional: install nanosdk[polars] or nanosdk[pandas].

Functions:

Name Description
records

Convert models and mappings to plain-dict rows.

polars

Build a polars DataFrame, one row per model or mapping.

pandas

Build a pandas DataFrame, one row per model or mapping.

Rows

What the converters accept: a model, a mapping, or an iterable of either.

A RootModel (say RootModel[list[Post]]) is unwrapped to its root value.

records

records(data: Rows) -> list[dict[str, Any]]

Convert models and mappings to plain-dict rows.

Models are dumped with their Python field names (not wire aliases), and nested models become nested dicts. Useful for CSV, SQLite or any other row consumer.

Parameters:

Name Type Description Default
data Rows

A model, a mapping, or an iterable of them.

required

Returns:

Type Description
list[dict[str, Any]]

One dict per row; a single model or mapping gives one row.

Raises:

Type Description
TypeError

If an item is neither a pydantic model nor a mapping.

polars

polars(data: Rows) -> DataFrame

Build a polars DataFrame, one row per model or mapping.

Nested models become struct columns. Needs the nanosdk[polars] extra.

Parameters:

Name Type Description Default
data Rows

A model, a mapping, or an iterable of them.

required

Returns:

Type Description
DataFrame

The DataFrame; empty (no columns) when there are no rows.

pandas

pandas(data: Rows) -> DataFrame

Build a pandas DataFrame, one row per model or mapping.

Nested models become dict-valued columns. Needs the nanosdk[pandas] extra.

Parameters:

Name Type Description Default
data Rows

A model, a mapping, or an iterable of them.

required

Returns:

Type Description
DataFrame

The DataFrame.