MCPcopy Create free account
hub / github.com/Open-Quant/openquant / to_polars_indicator_matrix

Function to_polars_indicator_matrix

python/openquant/adapters.py:52–73  ·  view source on GitHub ↗
(
    ind_mat: Sequence[Sequence[int]],
    bar_index: Sequence[int] | None = None,
    label_names: Sequence[str] | None = None,
)

Source from the content-addressed store, hash-verified

50
51
52def to_polars_indicator_matrix(
53 ind_mat: Sequence[Sequence[int]],
54 bar_index: Sequence[int] | None = None,
55 label_names: Sequence[str] | None = None,
56) -> pl.DataFrame:
57 if not ind_mat:
58 return pl.DataFrame({"bar_index": []})
59 width = len(ind_mat[0])
60 if any(len(row) != width for row in ind_mat):
61 raise ValueError("ind_mat must be rectangular")
62 if label_names is None:
63 label_names = [f"label_{i}" for i in range(width)]
64 if len(label_names) != width:
65 raise ValueError(f"label_names length mismatch: expected {width}, got {len(label_names)}")
66 if bar_index is None:
67 bar_index = list(range(len(ind_mat)))
68 _validate_equal_length("bar_index", bar_index, "ind_mat_rows", ind_mat)
69
70 data: dict[str, object] = {"bar_index": list(bar_index)}
71 for j, name in enumerate(label_names):
72 data[name] = [int(row[j]) for row in ind_mat]
73 return pl.DataFrame(data)
74
75
76def to_polars_weights_frame(

Callers

nothing calls this directly

Calls 1

_validate_equal_lengthFunction · 0.85

Tested by

no test coverage detected