Polars#
If you have installed polars, you can use a result set’s .PolarsDataFrame() method.
Load sample data#
Let’s create some sample data:
%load_ext sql
%%sql sqlite://
CREATE TABLE writer (first_name, last_name, year_of_death);
INSERT INTO writer VALUES ('William', 'Shakespeare', 1616);
INSERT INTO writer VALUES ('Bertold', 'Brecht', 1956);
Connecting to 'sqlite://'
1 rows affected.
1 rows affected.
Convert to polars.DataFrame#
Query the sample data and convert to polars.DataFrame:
result = %sql SELECT * FROM writer WHERE year_of_death > 1900
Running query in 'sqlite://'
df = result.PolarsDataFrame()
type(df)
polars.dataframe.frame.DataFrame
df
shape: (1, 3)
| first_name | last_name | year_of_death |
|---|---|---|
| str | str | i64 |
| "Bertold" | "Brecht" | 1956 |
Or using the cell magic:
%%sql result <<
SELECT * FROM writer WHERE year_of_death > 1900
Running query in 'sqlite://'
result.PolarsDataFrame()
shape: (1, 3)
| first_name | last_name | year_of_death |
|---|---|---|
| str | str | i64 |
| "Bertold" | "Brecht" | 1956 |
Convert automatically#
%config SqlMagic.autopolars = True
df = %sql SELECT * FROM writer
type(df)
Running query in 'sqlite://'
polars.dataframe.frame.DataFrame
df
shape: (2, 3)
| first_name | last_name | year_of_death |
|---|---|---|
| str | str | i64 |
| "William" | "Shakespeare" | 1616 |
| "Bertold" | "Brecht" | 1956 |