| 155 | def _inferred_interval_us(sorted_df: pl.DataFrame) -> int | None: |
| 156 | """The bar spacing of a frame sorted by (symbol, ts_us): the most common positive spacing |
| 157 | between consecutive bars of one symbol, pooled over symbols (the smallest on a tie). |
| 158 | |
| 159 | `None` when no symbol has two bars at different times. Mirrors |
| 160 | `openquant::data_processing` in Rust. |
| 161 | """ |
| 162 | spacings = ( |
| 163 | sorted_df.select( |
| 164 | pl.when(pl.col("symbol") == pl.col("symbol").shift(1)) |
| 165 | .then(pl.col("ts_us") - pl.col("ts_us").shift(1)) |
| 166 | .alias("spacing") |
| 167 | ) |
| 168 | .get_column("spacing") |
| 169 | .drop_nulls() |
| 170 | ) |