Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(python): Fix LazyFrame fetch method references #18033

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/source/src/python/user-guide/lazy/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
# --8<-- [start:partial]
q9 = (
pl.scan_csv(f"docs/assets/data/reddit.csv")
.head(10)
.with_columns(pl.col("name").str.to_uppercase())
.filter(pl.col("comment_karma") > 0)
.fetch(n_rows=int(100))
.collect()
)
# --8<-- [end:partial]
"""
31 changes: 11 additions & 20 deletions docs/source/user-guide/lazy/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,18 @@ We look at [streaming in more detail here](streaming.md).

While you're writing, optimizing or checking your query on a large dataset, querying all available data may lead to a slow development process.

You can instead execute the query with the `.fetch` method. The `.fetch` method takes a parameter `n_rows` and tries to 'fetch' that number of rows at the data source. The number of rows cannot be guaranteed, however, as the lazy API does not count how many rows there are at each stage of the query.
Instead, you can scan a subset of your partitions or use `.head`/`.collect` at the beginning and end of your query, respectively.
Keep in mind that the results of aggregations and filters on subsets of your data may not be representative of the result you would get on the full data.
Copy link
Member

@ritchie46 ritchie46 Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heads that are after an aggregation don't influence an aggregation. I don't think this sentence makes much sense. Of course head followed by aggregation influences a result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion is to have head at the beginning of your query so in front of the aggregations.

Copy link
Contributor Author

@edwinvehmaanpera edwinvehmaanpera Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E.g. in the given example

q9 = (
    pl.scan_csv(f"docs/assets/data/reddit.csv")
    .head(10)
    .with_columns(pl.col("name").str.to_uppercase())
    .filter(pl.col("comment_karma") > 0)
    .fetch(n_rows=int(100))
    .collect()
)

Copy link
Contributor Author

@edwinvehmaanpera edwinvehmaanpera Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it too obvious? Maybe a little, but I have had these types of operations fail in ways I didn't expect. Calling head(10000) and returning 0 rows might confuse someone. It is just single sentence so I feel like it is justified.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, with the accompanied query it makes sense. 👍


Here we "fetch" 100 rows from the source file and apply the predicates.

{{code_block('user-guide/lazy/execution','partial',['scan_csv','collect','fetch'])}}
{{code_block('user-guide/lazy/execution','partial',['scan_csv','collect','head'])}}

```text
shape: (27, 6)
┌───────┬───────────────────────────┬─────────────┬────────────┬───────────────┬────────────┐
│ id ┆ name ┆ created_utc ┆ updated_on ┆ comment_karma ┆ link_karma │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪═══════════════════════════╪═════════════╪════════════╪═══════════════╪════════════╡
│ 6 ┆ TAOJIANLONG_JASONBROKEN ┆ 1397113510 ┆ 1536527864 ┆ 4 ┆ 0 │
│ 17 ┆ SSAIG_JASONBROKEN ┆ 1397113544 ┆ 1536527864 ┆ 1 ┆ 0 │
│ 19 ┆ FDBVFDSSDGFDS_JASONBROKEN ┆ 1397113552 ┆ 1536527864 ┆ 3 ┆ 0 │
│ 37 ┆ IHATEWHOWEARE_JASONBROKEN ┆ 1397113636 ┆ 1536527864 ┆ 61 ┆ 0 │
│ … ┆ … ┆ … ┆ … ┆ … ┆ … │
│ 77763 ┆ LUNCHY ┆ 1137599510 ┆ 1536528275 ┆ 65 ┆ 0 │
│ 77765 ┆ COMPOSTELLAS ┆ 1137474000 ┆ 1536528276 ┆ 6 ┆ 0 │
│ 77766 ┆ GENERICBOB ┆ 1137474000 ┆ 1536528276 ┆ 291 ┆ 14 │
│ 77768 ┆ TINHEADNED ┆ 1139665457 ┆ 1536497404 ┆ 4434 ┆ 103 │
└───────┴───────────────────────────┴─────────────┴────────────┴───────────────┴────────────┘
shape: (1, 6)
┌─────┬─────────────────────────┬─────────────┬────────────┬───────────────┬────────────┐
│ id ┆ name ┆ created_utc ┆ updated_on ┆ comment_karma ┆ link_karma │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════════════════════════╪═════════════╪════════════╪═══════════════╪════════════╡
│ 6 ┆ TAOJIANLONG_JASONBROKEN ┆ 1397113510 ┆ 1536527864 ┆ 4 ┆ 0 │
└─────┴─────────────────────────┴─────────────┴────────────┴───────────────┴────────────┘
```
13 changes: 0 additions & 13 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,6 @@ def collect(

See Also
--------
fetch: Run the query on the first `n` rows only for debugging purposes.
explain : Print the query plan that is evaluated with collect.
profile : Collect the LazyFrame and time each node in the computation graph.
polars.collect_all : Collect multiple LazyFrames at the same time.
Expand Down Expand Up @@ -5188,12 +5187,6 @@ def limit(self, n: int = 5) -> LazyFrame:
n
Number of rows to return.

Notes
-----
Consider using the :func:`fetch` operation if you only want to test your
query. The :func:`fetch` operation will load the first `n` rows at the scan
level, whereas the :func:`head`/:func:`limit` are applied at the end.

Examples
--------
>>> lf = pl.LazyFrame(
Expand Down Expand Up @@ -5237,12 +5230,6 @@ def head(self, n: int = 5) -> LazyFrame:
n
Number of rows to return.

Notes
-----
Consider using the :func:`fetch` operation if you only want to test your
query. The :func:`fetch` operation will load the first `n` rows at the scan
level, whereas the :func:`head`/:func:`limit` are applied at the end.

Examples
--------
>>> lf = pl.LazyFrame(
Expand Down
Loading