From 3891e72a45016dcb128bc02155d0f4ab5a47e6c1 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 4 Nov 2025 03:02:46 +0100 Subject: [PATCH] Integrations: Harmonize pages about data frames (pandas, Dask, Polars) --- docs/integrate/dask/index.md | 88 ++++++++++++++--- docs/integrate/pandas/efficient-ingest.md | 2 + docs/integrate/pandas/index.md | 105 +++++++++++++++------ docs/integrate/pandas/makeTimeDataFrame.py | 8 ++ docs/integrate/polars/index.md | 91 +++++++++++++++--- docs/integrate/status.md | 2 + 6 files changed, 242 insertions(+), 54 deletions(-) create mode 100644 docs/integrate/pandas/makeTimeDataFrame.py diff --git a/docs/integrate/dask/index.md b/docs/integrate/dask/index.md index aff5001c..f353cb96 100644 --- a/docs/integrate/dask/index.md +++ b/docs/integrate/dask/index.md @@ -1,17 +1,21 @@ (dask)= # Dask +:::{div} .float-right .text-right +[![Dask logo](https://github.com/crate/crate-clients-tools/assets/453543/99bd2234-c501-479b-ade7-bcc2bfc1f288){height=60px loading=lazy}][Dask] +
+[![Dask CI](https://img.shields.io/github/actions/workflow/status/crate/cratedb-examples/dataframe-dask.yml?branch=main)](https://github.com/crate/cratedb-examples/actions/workflows/dataframe-dask.yml) +::: +:::{div} .clearfix +::: + :::{rubric} About ::: + [Dask] is a parallel computing library for analytics with task scheduling. It is built on top of the Python programming language, making it easy to scale the Python libraries that you know and love, like NumPy, pandas, and scikit-learn. -```{div} -:style: "float: right" -[![](https://github.com/crate/crate-clients-tools/assets/453543/99bd2234-c501-479b-ade7-bcc2bfc1f288){w=180px}](https://www.dask.org/) -``` - - [Dask DataFrames] help you process large tabular data by parallelizing pandas, either on your laptop for larger-than-memory computing, or on a distributed cluster of computers. @@ -20,16 +24,78 @@ the Python libraries that you know and love, like NumPy, pandas, and scikit-lear generic Python workflows across a Dask cluster with minimal code changes, by extending Python's `concurrent.futures` interface. -```{div} -:style: "clear: both" +:::{rubric} Install +::: + +```shell +pip install 'dask[dataframe]' 'sqlalchemy-cratedb' ``` -:::{rubric} Learn +:::{rubric} Synopsis ::: + +Write Dask dataframe to CrateDB. + +`example.py` +```python +import dask.dataframe as dd +from sqlalchemy_cratedb import insert_bulk + +CRATEDB_URI = "crate://crate:crate@localhost:4200" +TABLE_NAME = "example" + +df = makeTimeDataFrame(rows=500_000, freq="s") +ddf = dd.from_pandas(df, npartitions=4) +ddf.to_sql( + TABLE_NAME, + uri=CRATEDB_URI, + index=False, + if_exists="replace", + chunksize=20_000, + parallel=True, + method=insert_bulk, +) +``` + +:::{rubric} Quickstart example +::: + +Create the file `example.py` including the synopsis code shared above. +Complete the example by using the `makeTimeDataFrame()` function. + +:::{literalinclude} ../pandas/makeTimeDataFrame.py +::: + +:::{include} /connect/_cratedb.md +::: +```shell +pip install 'dask[dataframe]' 'sqlalchemy-cratedb' +python example.py +``` + +:::{rubric} Full example +::: + +:::{card} +:link: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/dask +:link-type: url +{material-regular}`play_arrow;2em` +Connect to CrateDB and CrateDB Cloud using Dask. ++++ +Includes basic examples of how to use Dask with CrateDB. +::: + +:::{rubric} Guides +::: + - {ref}`dask-usage` -- [Efficient batch/bulk INSERT operations with pandas, Dask, and SQLAlchemy] + +:::{rubric} Related sections +::: + +- {ref}`Efficient batch/bulk INSERT operations for pandas, Dask, and Polars ` +- {ref}`arrow-import-parquet` - [Import weather data using Dask] -- [Dask code examples] :::{toctree} @@ -40,8 +106,6 @@ Usage [Dask]: https://www.dask.org/ -[Dask code examples]: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/dask [Dask DataFrames]: https://docs.dask.org/en/latest/dataframe.html [Dask Futures]: https://docs.dask.org/en/latest/futures.html -[Efficient batch/bulk INSERT operations with pandas, Dask, and SQLAlchemy]: https://cratedb.com/docs/python/en/latest/by-example/sqlalchemy/dataframe.html [Import weather data using Dask]: https://github.com/crate/cratedb-examples/blob/main/topic/timeseries/dask-weather-data-import.ipynb diff --git a/docs/integrate/pandas/efficient-ingest.md b/docs/integrate/pandas/efficient-ingest.md index b73fbf12..38ccd72f 100644 --- a/docs/integrate/pandas/efficient-ingest.md +++ b/docs/integrate/pandas/efficient-ingest.md @@ -1,4 +1,6 @@ (pandas-bulk-import)= +(pandas-efficient-import)= + # Efficient bulk imports with pandas ## Introduction diff --git a/docs/integrate/pandas/index.md b/docs/integrate/pandas/index.md index b3c28ab5..0f85edae 100644 --- a/docs/integrate/pandas/index.md +++ b/docs/integrate/pandas/index.md @@ -1,12 +1,13 @@ (pandas)= # pandas -```{div} -:style: "float: right" -[![](https://pandas.pydata.org/static/img/pandas.svg){w=180px}](https://pandas.pydata.org/) -``` -```{div} .clearfix -``` +:::{div} .float-right .text-right +[![pandas logo](https://pandas.pydata.org/static/img/pandas.svg){height=60px loading=lazy}][pandas] +
+[![pandas CI](https://img.shields.io/github/actions/workflow/status/crate/cratedb-examples/dataframe-pandas.yml?branch=main)](https://github.com/crate/cratedb-examples/actions/workflows/dataframe-pandas.yml) +::: +:::{div} .clearfix +::: :::{rubric} About ::: @@ -15,35 +16,83 @@ and manipulation tool, built on top of the Python programming language. It offers data structures and operations for manipulating numerical tables and time series. -:::{rubric} Data Model +Pandas is built around data structures called Series and DataFrames. Data for these +collections can be imported from various file formats such as comma-separated values, +JSON, Parquet, SQL database tables or queries, and Microsoft Excel. +A Series is a 1-dimensional data structure built on top of NumPy's array. + +:::{rubric} Install ::: -- Pandas is built around data structures called Series and DataFrames. Data for these - collections can be imported from various file formats such as comma-separated values, - JSON, Parquet, SQL database tables or queries, and Microsoft Excel. -- A Series is a 1-dimensional data structure built on top of NumPy's array. -- Pandas includes support for time series, such as the ability to interpolate values - and filter using a range of timestamps. -- By default, a Pandas index is a series of integers ascending from 0, similar to the - indices of Python arrays. However, indices can use any NumPy data type, including - floating point, timestamps, or strings. -- Pandas supports hierarchical indices with multiple values per data point. An index - with this structure, called a "MultiIndex", allows a single DataFrame to represent - multiple dimensions, similar to a pivot table in Microsoft Excel. Each level of a - MultiIndex can be given a unique name. +```shell +pip install pandas sqlalchemy-cratedb +``` -:::{rubric} Learn +:::{rubric} Synopsis ::: + +Write pandas dataframe to CrateDB. + +`example.py` +```python +import sqlalchemy as sa +from sqlalchemy_cratedb import insert_bulk + +CRATEDB_URI = "crate://crate:crate@localhost:4200" +TABLE_NAME = "example" + +df = makeTimeDataFrame(rows=500_000, freq="s") +engine = sa.create_engine(CRATEDB_URI) +df.to_sql( + name=TABLE_NAME, + con=engine, + if_exists="replace", + index=False, + chunksize=20_000, + method=insert_bulk, +) +``` + +:::{rubric} Quickstart example +::: + +Create the file `example.py` including the synopsis code shared above. +Complete the example by using the `makeTimeDataFrame()` function. + +:::{literalinclude} ../pandas/makeTimeDataFrame.py +::: + +:::{include} /connect/_cratedb.md +::: +```shell +pip install pandas sqlalchemy-cratedb +python example.py +``` + +:::{rubric} Full example +::: + +:::{card} +:link: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/pandas +:link-type: url +{material-regular}`play_arrow;2em` +Connect to CrateDB and CrateDB Cloud using pandas. ++++ +Includes basic examples of how to use pandas with CrateDB. +::: + +:::{rubric} Guides +::: + +- {ref}`pandas-efficient-import` - {ref}`pandas-tutorial-start` - {ref}`pandas-tutorial-jupyter` -- {ref}`arrow-import-parquet` -- {ref}`pandas-bulk-import` -- See also: {ref}`dask-bulk-import` -- See also: [Efficient batch/bulk INSERT operations with pandas, Dask, and SQLAlchemy] -:::{rubric} Code examples +:::{rubric} Related sections ::: -- [pandas code examples] + +- {ref}`Efficient batch/bulk INSERT operations for pandas, Dask, and Polars ` +- {ref}`arrow-import-parquet` :::{toctree} @@ -55,6 +104,4 @@ Efficient ingest ::: -[Efficient batch/bulk INSERT operations with pandas, Dask, and SQLAlchemy]: https://cratedb.com/docs/python/en/latest/by-example/sqlalchemy/dataframe.html [pandas]: https://pandas.pydata.org/ -[pandas code examples]: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/pandas diff --git a/docs/integrate/pandas/makeTimeDataFrame.py b/docs/integrate/pandas/makeTimeDataFrame.py new file mode 100644 index 00000000..6c992514 --- /dev/null +++ b/docs/integrate/pandas/makeTimeDataFrame.py @@ -0,0 +1,8 @@ +def makeTimeDataFrame(rows=5_000, freq = "B"): + import numpy as np + import pandas as pd + return pd.DataFrame( + np.random.default_rng(2).standard_normal((rows, 4)), + columns=pd.Index(list("ABCD"), dtype=object), + index=pd.date_range("2000-01-01", periods=rows, freq=freq), + ) diff --git a/docs/integrate/polars/index.md b/docs/integrate/polars/index.md index a992cd35..ac4a7fe3 100644 --- a/docs/integrate/polars/index.md +++ b/docs/integrate/polars/index.md @@ -1,12 +1,13 @@ (polars)= # Polars -```{div} -:style: "float: right; margin-left: 0.5em" -[![Polars logo](https://github.com/pola-rs/polars-static/raw/master/logos/polars-logo-dark.svg){w=180px}][Polars] -``` -```{div} .clearfix -``` +:::{div} .float-right .text-right +[![Polars logo](https://github.com/pola-rs/polars-static/raw/master/logos/polars-logo-dark.svg){height=60px loading=lazy}][Polars] +
+[![Polars CI](https://img.shields.io/github/actions/workflow/status/crate/cratedb-examples/dataframe-polars.yml?branch=main)](https://github.com/crate/cratedb-examples/actions/workflows/dataframe-polars.yml) +::: +:::{div} .clearfix +::: :::{rubric} About ::: @@ -15,6 +16,13 @@ Rust, Python, Node.js, and R, plus a SQL context. It is powered by a multithreaded, vectorized query engine and written in Rust. +:::{dropdown} Features and data formats + +Polars is an open-source library for data manipulation, known for being +one of the fastest data processing solutions on a single machine. +It features a well-structured, typed API that is both expressive and +easy to use. + - **Fast:** Written from scratch in Rust and with performance in mind, designed close to the machine, and without external dependencies. @@ -29,7 +37,7 @@ multithreaded, vectorized query engine and written in Rust. - **Out of Core:** The streaming API allows you to process your results without requiring all your data to be in memory at the same time. -- **Parallel:** Polars' multi-threaded query engine utilizes the power of your +- **Parallel:** Polars' multithreaded query engine utilizes the power of your machine by dividing the workload among the available CPU cores without any additional configuration. @@ -41,9 +49,6 @@ multithreaded, vectorized query engine and written in Rust. community of developers. Everyone is encouraged to add new features and contribute. It is free to use under the MIT license. -:::{rubric} Data formats -::: - Polars supports reading and writing to many common data formats. This allows you to easily integrate Polars into your existing data stack. @@ -53,11 +58,71 @@ This allows you to easily integrate Polars into your existing data stack. - Databases: MySQL, PostgreSQL, SQLite, Redshift, SQL Server, etc. (via ConnectorX) - Cloud storage: Amazon S3, Azure Blob/ADLS (via fsspec‑compatible backends) -:::{rubric} Learn ::: -- [Polars code examples] + +:::{rubric} Install +::: + +```shell +pip install 'polars[pyarrow]' sqlalchemy-cratedb +``` + +:::{rubric} Synopsis +::: + +Write Polars dataframe to CrateDB. + +`example.py` +```python +import polars as pl +import sqlalchemy as sa +from sqlalchemy_cratedb import insert_bulk + +CRATEDB_URI = "crate://crate:crate@localhost:4200" +TABLE_NAME = "example" + +df = pl.from_pandas(makeTimeDataFrame(rows=500_000, freq="s")) +engine = sa.create_engine(CRATEDB_URI) +df.write_database( + engine="sqlalchemy", + connection=engine, + table_name=TABLE_NAME, + if_table_exists="replace", + engine_options={ + "method": insert_bulk, + "chunksize": 20_000, + }, +) +``` + +:::{rubric} Quickstart example +::: + +Create the file `example.py` including the synopsis code shared above. +Complete the example by using the `makeTimeDataFrame()` function. + +:::{literalinclude} ../pandas/makeTimeDataFrame.py +::: + +:::{include} /connect/_cratedb.md +::: +```shell +pip install 'polars[pyarrow]' sqlalchemy-cratedb pandas +python example.py +``` + +:::{rubric} Full example +::: + +:::{card} +:link: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/polars +:link-type: url +{material-regular}`play_arrow;2em` +Connect to CrateDB and CrateDB Cloud using Polars. ++++ +Includes basic examples of how to use Polars with CrateDB. +::: [Apache Arrow]: https://arrow.apache.org/ [Polars]: https://pola.rs/ -[Polars code examples]: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/polars diff --git a/docs/integrate/status.md b/docs/integrate/status.md index 92340225..7d445937 100644 --- a/docs/integrate/status.md +++ b/docs/integrate/status.md @@ -60,6 +60,8 @@ on behalf of [cratedb-examples] and [academy-fundamentals-course]. + +