Skip to content

Reading saved runs in parts

load_data reads every table of a saved run into memory. The functions on this page read the same folder one batch of rows at a time, so their memory is set by the batch size and the size of the result, not by the size of the file. Use them on a run too large to load. Scale and chunking shows a worked example and the memory measured.

Function What it gives
scan_table the rows of one table as pandas frames, one batch at a time
read_table the selected rows and columns of one table as one frame
aggregate_by_time one row per fixed time bucket: first, last, min, max, sum, count or mean
summarize_depth_file a new depth_summary from a saved depth table, for example with other bins

The columns and the time range you give go to the Parquet reader, so it reads only those columns and skips each row group whose times are all outside the range. Each frame has the dtypes load_data gives and the tick size and lot size on attrs.

scan_table

scan_table(
    path: str | Path,
    table: str,
    *,
    columns: Sequence[str] | None = None,
    start: Any = None,
    end: Any = None,
    filter: Expression | None = None,
    batch_rows: int = BATCH_ROWS,
) -> Iterator[pd.DataFrame]

Read one table of a saved run as pandas frames, one batch at a time.

Only the named columns are read, and only the row groups that can hold a row in the time range. The batches come in the file's row order, and together they hold the same rows :func:read_table gives.

Parameters:

Name Type Description Default
path str or Path

A folder written by :func:~ob_analytics.data.save_data or :meth:~ob_analytics.pipeline.Pipeline.run_windows.

required
table str

The table to read: "events", "trades", "depth" or "depth_summary", or any other <table>.parquet in the folder.

required
columns sequence of str

The columns to read, in this order. None reads every column.

None
start (str, datetime or Timestamp)

Keep the rows with start <= timestamp < end. A time with no zone is read as UTC. None leaves that side open.

None
end (str, datetime or Timestamp)

Keep the rows with start <= timestamp < end. A time with no zone is read as UTC. None leaves that side open.

None
filter Expression

A further row filter, for example pyarrow.dataset.field("direction") == "bid". It is passed to the reader together with the time range.

None
batch_rows int

The most rows in one frame. The memory the read uses grows with it.

BATCH_ROWS

Returns:

Type Description
iterator of pandas.DataFrame

Each frame has the dtypes :func:~ob_analytics.data.load_data gives and the tick size and lot size on attrs. A batch with no rows left after the filter is not given.

Raises:

Type Description
ConfigError

If path is not a folder or has no such table, the file is not Parquet this build can read or has an unsupported schema version, a named column is missing, or start or end is not a time. These are raised by the call, before the first frame is read.

read_table

read_table(
    path: str | Path,
    table: str,
    *,
    columns: Sequence[str] | None = None,
    start: Any = None,
    end: Any = None,
    filter: Expression | None = None,
) -> pd.DataFrame

Read the selected rows and columns of one table of a saved run.

The selection is passed to the Parquet reader, as in :func:scan_table, so a time range or a few columns of a large file read only that part of it. The result is held in memory, so it must fit.

Parameters:

Name Type Description Default
path str | Path

As for :func:scan_table.

required
table str | Path

As for :func:scan_table.

required
columns str | Path

As for :func:scan_table.

required
start str | Path

As for :func:scan_table.

required
end str | Path

As for :func:scan_table.

required
filter str | Path

As for :func:scan_table.

required

Returns:

Type Description
DataFrame

The rows in the file's order, with a fresh index, the dtypes :func:~ob_analytics.data.load_data gives and the tick size and lot size on attrs.

Raises:

Type Description
ConfigError

As for :func:scan_table.

aggregate_by_time

aggregate_by_time(
    path: str | Path,
    table: str,
    *,
    every: str | Timedelta,
    how: str | Mapping[str, str] = "last",
    columns: Sequence[str] | None = None,
    start: Any = None,
    end: Any = None,
    filter: Expression | None = None,
    batch_rows: int = BATCH_ROWS,
) -> pd.DataFrame

Reduce one table of a saved run to one row per time bucket, in parts.

The table is read one batch at a time. Each batch is reduced per bucket, and the partial results of a bucket that two batches share are combined, so the result is the same as reducing the whole table at once: frame.groupby(bucket).agg(how). The memory used is set by batch_rows and the number of buckets, not by the size of the file.

Buckets are fixed spans of time counted from the Unix epoch, as everywhere else in the package, so a "1min" bucket starts on the minute.

Parameters:

Name Type Description Default
path str | Path

As for :func:scan_table.

required
table str | Path

As for :func:scan_table.

required
every str or Timedelta

The bucket length, a fixed duration such as "1s", "1min" or "1h".

required
how str or mapping of str to str

The reduction, one of :data:AGGREGATIONS. A string applies to every column in columns. A mapping gives one reduction per column, and then columns must be None. "first" and "last" follow the file's row order. Default "last": the value at the end of each bucket.

'last'
columns sequence of str

The columns to reduce when how is a string. None takes every numeric column except event_id.

None
start Any

As for :func:scan_table.

None
end Any

As for :func:scan_table.

None
filter Any

As for :func:scan_table.

None
batch_rows Any

As for :func:scan_table.

None

Returns:

Type Description
DataFrame

One row per bucket that holds a row, in time order: timestamp (the start of the bucket) and one column per reduced column, with the same name. A bucket with no rows is not in the result. Prices and sizes stay in the file's units (integer ticks and lots in a current file); the tick size and lot size are on attrs.

Raises:

Type Description
ConfigError

If every is not a fixed duration, a reduction is not one of :data:AGGREGATIONS, both a mapping and columns are given, a column is missing, or as for :func:scan_table.

summarize_depth_file

summarize_depth_file(
    path: str | Path,
    dest: str | Path,
    *,
    depth_bps: int | None = None,
    depth_bins: int | None = None,
    batch_rows: int = BATCH_ROWS,
) -> Path

Build the depth summary from a saved depth table, in parts.

The depth table is read one batch at a time and replayed through one :class:~ob_analytics.depth.DepthSummaryEngine, which keeps the book from one batch to the next. Each batch's summary rows are written as they are made. The result is the same as :func:~ob_analytics.depth.summarize_depth gives on the whole table, row for row, and the memory used is set by batch_rows and the size of the book, not by the size of the file.

Parameters:

Name Type Description Default
path str or Path

A folder written by :func:~ob_analytics.data.save_data or :meth:~ob_analytics.pipeline.Pipeline.run_windows that holds a depth table.

required
dest str or Path

The folder to write depth_summary.parquet to; it is created when missing. It may be path, which replaces the run's own summary. The file is moved into place only once the whole table is done, so a run that stops part-way leaves dest as it was.

required
depth_bps int

The width of each depth bin in basis points, and the number of bins. None keeps the value the run recorded, or the :class:~ob_analytics.config.PipelineConfig default when it recorded none.

None
depth_bins int

The width of each depth bin in basis points, and the number of bins. None keeps the value the run recorded, or the :class:~ob_analytics.config.PipelineConfig default when it recorded none.

None
batch_rows int

The most depth rows replayed at a time.

BATCH_ROWS

Returns:

Type Description
Path

The path of the depth_summary.parquet written.

Raises:

Type Description
ConfigError

If the folder has no readable depth table, the table has no rows, its rows are not in time order, or as for :func:scan_table.

AGGREGATIONS module-attribute

AGGREGATIONS = (
    "first",
    "last",
    "min",
    "max",
    "sum",
    "count",
    "mean",
)

BATCH_ROWS module-attribute

BATCH_ROWS = 131072