Skip to content

Commit

Permalink
chore: use polars instead of pandas in examples (#3289)
Browse files Browse the repository at this point in the history
* Migrate the SQL examples to use Polars.
* Can merge when polars in pyodide is supported
  • Loading branch information
akshayka authored Dec 29, 2024
1 parent e6218aa commit 7ae245d
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 59 deletions.
3 changes: 2 additions & 1 deletion examples/sql/connect_to_motherduck.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# dependencies = [
# "altair==5.4.1",
# "duckdb==1.1.0",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "marimo",
# "pandas==2.2.3",
# ]
# ///

Expand Down
3 changes: 2 additions & 1 deletion examples/sql/connect_to_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "requests==2.32.3",
# ]
# ///
Expand Down
19 changes: 10 additions & 9 deletions examples/sql/connect_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "requests==2.32.3",
# ]
# ///

import marimo

__generated_with = "0.9.1"
__generated_with = "0.10.7"
app = marimo.App(width="medium")


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(
r"""
# Connect to SQLite
Expand All @@ -31,7 +32,7 @@ def __(mo):


@app.cell(hide_code=True)
def __():
def _():
import marimo as mo


Expand All @@ -53,7 +54,7 @@ def download_sample_data():


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.accordion(
{
"Tip: Creating SQL Cells": mo.md(
Expand All @@ -73,7 +74,7 @@ def __(mo):


@app.cell
def __(INFORMATION_SCHEMA, mo):
def _(INFORMATION_SCHEMA, TABLES, mo):
_df = mo.sql(
f"""
-- Boilerplate: detach the database so this cell works when you re-run it
Expand All @@ -90,13 +91,13 @@ def __(INFORMATION_SCHEMA, mo):


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(r"""Once the database is attached, you can query it with SQL. For example, the next cell computes the average track length of each composer in the chinook database.""")
return


@app.cell
def __(chinook, mo, track):
def _(chinook, mo, track):
_df = mo.sql(
f"""
SELECT composer, MEAN(Milliseconds) as avg_track_ms from chinook.track GROUP BY composer ORDER BY avg_track_ms DESC;
Expand All @@ -106,7 +107,7 @@ def __(chinook, mo, track):


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(
f"""
You can explore the schemas of all your tables at a glance in the **data sources panel**: click
Expand Down
3 changes: 2 additions & 1 deletion examples/sql/misc/database_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# ]
# ///

Expand Down
3 changes: 3 additions & 0 deletions examples/sql/misc/electric_vehicles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# requires-python = ">=3.9"
# dependencies = [
# "altair==5.4.1",
# "duckdb==1.1.3",
# "marimo",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# ]
# ///

Expand Down
6 changes: 3 additions & 3 deletions examples/sql/misc/sql_cars.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# "altair==5.4.1",
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "vega-datasets==0.9.0",
# ]
# ///
Expand Down Expand Up @@ -233,11 +234,10 @@ def __(cars, mo, selected_cars):
@app.cell
def __():
import marimo as mo
import pandas as pd
import duckdb
import altair as alt
from vega_datasets import data
return alt, data, duckdb, mo, pd
return alt, data, duckdb, mo


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion examples/sql/parametrizing_sql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "vega-datasets==0.9.0",
# ]
# ///
Expand Down
3 changes: 2 additions & 1 deletion examples/sql/querying_dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "vega-datasets==0.9.0",
# ]
# ///
Expand Down
38 changes: 20 additions & 18 deletions examples/sql/read_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
# dependencies = [
# "duckdb==1.1.1",
# "marimo",
# "pandas==2.2.3",
# "polars==1.18.0",
# "pyarrow==18.1.0",
# "vega-datasets==0.9.0",
# ]
# ///

import marimo

__generated_with = "0.9.1"
__generated_with = "0.10.7"
app = marimo.App(width="medium")


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(
"""
# Read CSV
Expand All @@ -27,22 +28,22 @@ def __(mo):


@app.cell(hide_code=True)
def __():
def _():
import marimo as mo
import pandas as pd
import polars as pl

pd.DataFrame({"A": [1, 2, 3], "B": ["a", "b", "c"]}).to_csv("data.csv")
return mo, pd
pl.DataFrame({"A": [1, 2, 3], "B": ["a", "b", "c"]}).write_csv("data.csv")
return mo, pl


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md("""Reading from a local CSV is as easy as `SELECT * from "data.csv"`, where `data.csv` is the path to your local file (or a URL to a CSV file).""")
return


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.accordion(
{
"Tip: Creating SQL Cells": mo.md(
Expand All @@ -62,19 +63,20 @@ def __(mo):


@app.cell
def __(data, mo):
def _(mo):
result = mo.sql(
f"""
-- Tip: you can also specify the data files using a glob, such as '/path/to/*.csv'
-- or '/path/**/to/*.csv'
SELECT * FROM "data.csv"
""", output=False
""",
output=False,
)
return (result,)


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.accordion(
{
"Tip: Query output": mo.md(
Expand All @@ -92,13 +94,13 @@ def __(mo):


@app.cell
def __(result):
def _(result):
result
return


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(
r"""
## Create an in-memory table from a CSV file
Expand All @@ -110,7 +112,7 @@ def __(mo):


@app.cell
def __(data, mo):
def _(mo):
_df = mo.sql(
f"""
CREATE TABLE myTable AS SELECT * FROM "data.csv"
Expand All @@ -120,7 +122,7 @@ def __(data, mo):


@app.cell
def __(mo, myTable):
def _(mo, myTable):
_df = mo.sql(
f"""
SELECT * FROM myTable
Expand All @@ -130,13 +132,13 @@ def __(mo, myTable):


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(r"""## Advanced usage""")
return


@app.cell(hide_code=True)
def __(mo):
def _(mo):
mo.md(r"""To customize how your CSV is read, including specifying the delimiter type, use [duckdb's `read_csv` function](https://duckdb.org/docs/data/csv/overview.html).""")
return

Expand Down
Loading

0 comments on commit 7ae245d

Please sign in to comment.