You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2024/02/08 03:08:33 UTC
(arrow-datafusion) 01/03: Minor: Improve DataFrame docs, add examples
This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch alamb/write_parquet_docs
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
commit 85e1ce4225f108331d878e50967a122859ea5ed0
Author: Andrew Lamb <an...@nerdnetworks.org>
AuthorDate: Wed Feb 7 21:42:38 2024 -0500
Minor: Improve DataFrame docs, add examples
---
datafusion/core/src/dataframe/mod.rs | 74 +++++++++++++++++++++++-----
datafusion/core/src/execution/context/mod.rs | 4 +-
datafusion/physical-plan/src/lib.rs | 4 +-
3 files changed, 67 insertions(+), 15 deletions(-)
diff --git a/datafusion/core/src/dataframe/mod.rs b/datafusion/core/src/dataframe/mod.rs
index f4023642ef..2d4cd25a0b 100644
--- a/datafusion/core/src/dataframe/mod.rs
+++ b/datafusion/core/src/dataframe/mod.rs
@@ -109,27 +109,50 @@ impl Default for DataFrameWriteOptions {
}
}
-/// DataFrame represents a logical set of rows with the same named columns.
-/// Similar to a [Pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) or
-/// [Spark DataFrame](https://spark.apache.org/docs/latest/sql-programming-guide.html)
+/// Represents a logical set of rows with the same named columns.
///
-/// DataFrames are typically created by the `read_csv` and `read_parquet` methods on the
-/// [SessionContext](../execution/context/struct.SessionContext.html) and can then be modified
-/// by calling the transformation methods, such as `filter`, `select`, `aggregate`, and `limit`
-/// to build up a query definition.
+/// Similar to a [Pandas DataFrame] or [Spark DataFrame], a DataFusion DataFrame
+/// represents a 2 dimensional table of rows and columns.
///
-/// The query can be executed by calling the `collect` method.
+/// The typical workflow using DataFrames looks like
///
+/// 1. Create a DataFrame via methods on [SessionContext], such as [`read_csv`]
+/// and [`read_parquet`].
+///
+/// 2. Build a desired calculation by calling methods such as [`filter`],
+/// [`select`], [`aggregate`], and [`limit`]
+///
+/// 3. Execute into [`RecordBatch`]es by calling [`collect`]
+///
+/// DataFrames are "lazy" in the sense that most methods do not actually compute
+/// anything, they just build up a plan. Calling [`collect`] executes the plan
+/// using the norma DataFusion planning and execution process.
+///
+/// [Pandas DataFrame]: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html
+/// [Spark DataFrame]: https://spark.apache.org/docs/latest/sql-programming-guide.html
+/// [`read_csv`]: SessionContext::read_csv
+/// [`read_parquet`]: SessionContext::read_parquet
+/// [`filter`]: DataFrame::filter
+/// [`select`]: DataFrame::select
+/// [`aggregate`]: DataFrame::aggregate
+/// [`limit`]: DataFrame::limit
+/// [`collect`]: DataFrame::collect
+///
+/// # Example
/// ```
/// # use datafusion::prelude::*;
/// # use datafusion::error::Result;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let ctx = SessionContext::new();
+/// // Read the data from a csv file
/// let df = ctx.read_csv("tests/data/example.csv", CsvReadOptions::new()).await?;
+/// // create a new dataframe that computes the equivalent of
+/// // `SELECT a, MIN(b) FROM df WHERE a <= b GROUP BY a LIMIT 100;`
/// let df = df.filter(col("a").lt_eq(col("b")))?
/// .aggregate(vec![col("a")], vec![min(col("b"))])?
/// .limit(0, Some(100))?;
+/// // Perform the actual computation
/// let results = df.collect();
/// # Ok(())
/// # }
@@ -739,9 +762,13 @@ impl DataFrame {
Ok(len)
}
- /// Convert the logical plan represented by this DataFrame into a physical plan and
- /// execute it, collecting all resulting batches into memory
- /// Executes this DataFrame and collects all results into a vector of RecordBatch.
+ /// Execute this DataFrame and buffer all resulting `RecordBatch`es into memory.
+ ///
+ /// Prior to calling `collect`, modifying a DataFrame simply updates a plan
+ /// (no actual computation is performed). `collect` triggers the computation.
+ ///
+ /// See [`Self::execute_stream`] to execute a DataFrame without buffering.
+ ///
/// ```
/// # use datafusion::prelude::*;
/// # use datafusion::error::Result;
@@ -802,6 +829,8 @@ impl DataFrame {
/// Executes this DataFrame and returns a stream over a single partition
///
+ /// See [Self::collect] to buffer the `RecordBatch`es in memory.
+ ///
/// # Example
/// ```
/// # use datafusion::prelude::*;
@@ -1025,6 +1054,7 @@ impl DataFrame {
}
/// Write this DataFrame to the referenced table by name.
+ ///
/// This method uses on the same underlying implementation
/// as the SQL Insert Into statement. Unlike most other DataFrame methods,
/// this method executes eagerly. Data is written to the table using an
@@ -1050,7 +1080,27 @@ impl DataFrame {
DataFrame::new(self.session_state, plan).collect().await
}
- /// Write a `DataFrame` to a CSV file.
+ /// Execute the `DataFrame` and write the results to a CSV file.
+ ///
+ /// # Example
+ /// ```
+ /// # use datafusion::prelude::*;
+ /// # use datafusion::error::Result;
+ /// # #[tokio::main]
+ /// # async fn main() -> Result<()> {
+ /// use datafusion::dataframe::DataFrameWriteOptions;
+ /// let ctx = SessionContext::new();
+ /// // Sort the data by column "b" and write it to a new location
+ /// ctx.read_csv("tests/data/example.csv", CsvReadOptions::new()).await?
+ /// .sort(vec![col("b").sort(true, true)])? // sort by b asc, nulls first
+ /// .write_csv(
+ /// "output.csv",
+ /// DataFrameWriteOptions::new(),
+ /// None, // can also specify CSV writing options here
+ /// ).await?;
+ /// # Ok(())
+ /// # }
+ /// ```
pub async fn write_csv(
self,
path: &str,
diff --git a/datafusion/core/src/execution/context/mod.rs b/datafusion/core/src/execution/context/mod.rs
index b9039c5c92..0bc75720e7 100644
--- a/datafusion/core/src/execution/context/mod.rs
+++ b/datafusion/core/src/execution/context/mod.rs
@@ -164,7 +164,7 @@ where
///
/// [`SessionContext`] provides the following functionality:
///
-/// * Create a DataFrame from a CSV or Parquet data source.
+/// * Create a [`DataFrame`] from a CSV or Parquet data source.
/// * Register a CSV or Parquet data source as a table that can be referenced from a SQL query.
/// * Register a custom data source that can be referenced from a SQL query.
/// * Execution a SQL query
@@ -172,7 +172,7 @@ where
/// # Example: DataFrame API
///
/// The following example demonstrates how to use the context to execute a query against a CSV
-/// data source using the DataFrame API:
+/// data source using the [`DataFrame`] API:
///
/// ```
/// use datafusion::prelude::*;
diff --git a/datafusion/physical-plan/src/lib.rs b/datafusion/physical-plan/src/lib.rs
index 9c8ad03bbe..1c4a6ac0ec 100644
--- a/datafusion/physical-plan/src/lib.rs
+++ b/datafusion/physical-plan/src/lib.rs
@@ -511,7 +511,9 @@ pub async fn collect(
common::collect(stream).await
}
-/// Execute the [ExecutionPlan] and return a single stream of results.
+/// Execute the [ExecutionPlan] and return a single stream of `RecordBatch`es.
+///
+/// See [collect] to buffer the `RecordBatch`es in memory.
///
/// # Aborting Execution
///