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: |
required |
table
|
str
|
The table to read: |
required |
columns
|
sequence of str
|
The columns to read, in this order. |
None
|
start
|
(str, datetime or Timestamp)
|
Keep the rows with |
None
|
end
|
(str, datetime or Timestamp)
|
Keep the rows with |
None
|
filter
|
Expression
|
A further row filter, for example
|
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: |
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: |
required |
table
|
str | Path
|
As for :func: |
required |
columns
|
str | Path
|
As for :func: |
required |
start
|
str | Path
|
As for :func: |
required |
end
|
str | Path
|
As for :func: |
required |
filter
|
str | Path
|
As for :func: |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The rows in the file's order, with a fresh index, the dtypes
:func: |
Raises:
| Type | Description |
|---|---|
ConfigError
|
As for :func: |
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: |
required |
table
|
str | Path
|
As for :func: |
required |
every
|
str or Timedelta
|
The bucket length, a fixed duration such as |
required |
how
|
str or mapping of str to str
|
The reduction, one of :data: |
'last'
|
columns
|
sequence of str
|
The columns to reduce when how is a string. |
None
|
start
|
Any
|
As for :func: |
None
|
end
|
Any
|
As for :func: |
None
|
filter
|
Any
|
As for :func: |
None
|
batch_rows
|
Any
|
As for :func: |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per bucket that holds a row, in time order: |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If every is not a fixed duration, a reduction is not one of
:data: |
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: |
required |
dest
|
str or Path
|
The folder to write |
required |
depth_bps
|
int
|
The width of each depth bin in basis points, and the number of bins.
|
None
|
depth_bins
|
int
|
The width of each depth bin in basis points, and the number of bins.
|
None
|
batch_rows
|
int
|
The most depth rows replayed at a time. |
BATCH_ROWS
|
Returns:
| Type | Description |
|---|---|
Path
|
The path of the |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If the folder has no readable |