Skip to main content
Class representing a dataset result including the data and metadata. Extends DatasetMetadata. Instances of DatasetResult are created and returned by Squirrels dataset execution APIs (for example, in dataset routes or project methods); you should not construct this class directly. If DatasetResult is needed for type annotation, it can be imported from the squirrels.types or squirrels module.

Attributes

df
pl.DataFrame
The dataset as a Polars DataFrame

Methods

to_json()

Returns the dataset as a JSON-serializable dictionary with pagination support.
def to_json(
    self, orientation: Literal["records", "rows", "columns"], 
    limit: int, offset: int
) -> dict:
returns
dict
A dictionary containing the dataset with data and metadata. The structure includes:

Examples

Here are some common usage patterns for the DatasetResult class. It is assumed that the code is running in an async context (e.g. inside an async function or a Jupyter Notebook cell).

Query a dataset and access the dataframe

from squirrels.types import DatasetResult
from squirrels import SquirrelsProject

sqrl = SquirrelsProject()

# Query a dataset
result: DatasetResult = await sqrl.dataset(
    "sales_data",
    selections={"year": "2024", "region": "north-america"}
)

# Access the Polars DataFrame
print(result.df.head())

# Convert to pandas if needed
pandas_df = result.df.to_pandas()

Export dataset to JSON with different orientations

from squirrels.types import DatasetResult
from squirrels import SquirrelsProject

sqrl = SquirrelsProject()

result: DatasetResult = await sqrl.dataset("sales_data")

# Get first 10 rows as array of objects (records orientation)
json_data = result.to_json("records", limit=10, offset=0)
print(json_data['data'])
# Output: [{'date': '2024-01-01', 'amount': 100}, ...]

# Get as array of arrays (rows orientation)
json_data = result.to_json("rows", limit=10, offset=0)
print(json_data['data'])
# Output: [['2024-01-01', 100], ['2024-01-02', 150], ...]

# Get as column-oriented object (columns orientation)
json_data = result.to_json("columns", limit=10, offset=0)
print(json_data['data'])
# Output: {'date': ['2024-01-01', '2024-01-02', ...], 'amount': [100, 150, ...]}