SQL over frames in Python
Polars will run SQL against registered frames, planner and all.
import polars as pl
runs = pl.DataFrame({
"lang": ["python", "rust", "python"],
"tpm": [104, 98, 91],
})
with pl.SQLContext(runs=runs) as ctx:
answer = ctx.execute(
"SELECT lang, COUNT(*) AS n, AVG(tpm) AS mean_tpm "
"FROM runs GROUP BY lang ORDER BY mean_tpm DESC",
eager=True)
print(answer)
print(runs.sql("SELECT * FROM self WHERE tpm > 95"))
How it works
SQLContextregisters frames under names.executereturns a LazyFrame unless you ask to collect.DataFrame.sqlis the shorthand for one frame.
Keywords and builtins used here
asprintwith
The run, in numbers
- Lines
- 15
- Characters to type
- 348
- Tokens
- 87
- Three-star pace
- 115 tpm
At the three-star pace of 115 tokens a minute, this run takes about 45 seconds.
Step 3 of 4 in Lazy & SQL, step 27 of 32 in Polars.