(sorted_df: pl.DataFrame, rows_removed_by_deduplication: int)
| 113 | pl.col("high").cast(pl.Float64), |
| 114 | pl.col("low").cast(pl.Float64), |
| 115 | pl.col("close").cast(pl.Float64), |
| 116 | pl.col("volume").cast(pl.Float64), |
| 117 | ) |
| 118 | if "adj_close" in frame.columns: |
| 119 | lf = lf.with_columns(pl.col("adj_close").cast(pl.Float64)) |
| 120 | else: |
| 121 | lf = lf.with_columns(pl.col("close").alias("adj_close")) |
| 122 | return lf.select(CANONICAL_OHLCV_COLUMNS) |
| 123 | |
| 124 | |
| 125 | def _prepare_ohlcv_lf(df: pl.DataFrame) -> pl.LazyFrame: |
| 126 | return _cast_ohlcv_lf(df).drop_nulls(CANONICAL_OHLCV_COLUMNS) |
| 127 | |
| 128 | |
| 129 | def _null_counts(cast_lf: pl.LazyFrame) -> dict[str, int]: |
| 130 | """Nulls per canonical column of `_cast_ohlcv_lf`'s frame, i.e. before they are dropped. |
| 131 | |
| 132 | A timestamp that does not parse is a null `ts`; a missing `adj_close` is filled from |
| 133 | `close`, so it has `close`'s nulls. |
| 134 | """ |
| 135 | row = cast_lf.select(pl.col(CANONICAL_OHLCV_COLUMNS).null_count()).collect().row(0) |
| 136 | return {name: int(n) for name, n in zip(CANONICAL_OHLCV_COLUMNS, row, strict=True)} |
| 137 | |
| 138 | |
| 139 | def _format_ts(v: Any) -> str | None: |
| 140 | if v is None: |
| 141 | return None |
| 142 | if hasattr(v, "strftime"): |
| 143 | # Same form as str(datetime) and the Rust bindings: no fraction on a whole second. |
| 144 | out = v.strftime("%Y-%m-%d %H:%M:%S") |
| 145 | if getattr(v, "microsecond", 0): |
| 146 | out += f".{v.microsecond:06d}" |
| 147 | return out |
| 148 | return str(v) |
| 149 | |
| 150 | |
| 151 | _DAY_US = 86_400 * 1_000_000 |
| 152 | _HOUR_US = 3_600 * 1_000_000 |
| 153 | |
| 154 | |
| 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). |
no test coverage detected