Skip to content

Commit

Permalink
Simplify snippet hierarchy.
Browse files Browse the repository at this point in the history
Since there is now a coherent story throughout the whole 'getting started' page it no longer makes sense to have three different files with examples for each language.
  • Loading branch information
rodrigogiraoserrao committed Oct 1, 2024
1 parent 3879c69 commit 049a91e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 161 deletions.
29 changes: 0 additions & 29 deletions docs/source/src/python/user-guide/getting-started/joins.py

This file was deleted.

This file was deleted.

12 changes: 3 additions & 9 deletions docs/source/src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@ path = "home/example.rs"
required-features = ["polars/lazy", "polars/csv"]

[[bin]]
name = "user-guide-getting-started-expressions"
path = "user-guide/getting-started/expressions.rs"
name = "user-guide-getting-started"
path = "user-guide/getting-started.rs"
required-features = ["polars/lazy", "polars/temporal", "polars/round_series", "polars/strings"]
[[bin]]
name = "user-guide-getting-started-joins"
path = "user-guide/getting-started/joins.rs"
required-features = ["polars/polars-ops"]
[[bin]]
name = "user-guide-getting-started-reading-writing"
path = "user-guide/getting-started/reading-writing.rs"

[[bin]]
name = "user-guide-concepts-data-structures"
path = "user-guide/concepts/data-structures.rs"
Expand Down
29 changes: 0 additions & 29 deletions docs/source/src/rust/user-guide/getting-started/joins.rs

This file was deleted.

35 changes: 0 additions & 35 deletions docs/source/src/rust/user-guide/getting-started/reading-writing.rs

This file was deleted.

72 changes: 36 additions & 36 deletions docs/source/user-guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ This chapter is here to help you get started with Polars. It covers all the fund

Polars supports reading and writing for common file formats (e.g. csv, json, parquet), cloud storage (S3, Azure Blob, BigQuery) and databases (e.g. postgres, mysql). Below, we create a small dataframe and show how to write it to disk and read it back.

{{code_block('user-guide/getting-started/expressions','df',['DataFrame'])}}
{{code_block('user-guide/getting-started','df',['DataFrame'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:df"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:df"
```

In the example below we write the dataframe to a csv file called `output.csv`. After that, we read it back using `read_csv` and then print the result for inspection.

{{code_block('user-guide/getting-started/expressions','csv',['read_csv','write_csv'])}}
{{code_block('user-guide/getting-started','csv',['read_csv','write_csv'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:csv"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:csv"
```

For more examples on the CSV file format and other data formats, see the [IO section](io/index.md) of the user guide.
Expand Down Expand Up @@ -67,20 +67,20 @@ For a more detailed exploration of contexts and expressions see the respective u
The context `select` allows you to select and manipulate columns from a dataframe.
In the simplest case, each expression you provide will map to a column in the result dataframe:

{{code_block('user-guide/getting-started/expressions','select',['select','alias','Expr.dt'])}}
{{code_block('user-guide/getting-started','select',['select','alias','Expr.dt'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:select"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:select"
```

Polars also supports a feature called “expression expansion”, in which one expression acts as shorthand for multiple expressions.
In the example below, we use expression expansion to manipulate the columns “weight” and “height” with a single expression.
When using expression expansion you can use `.name.suffix` to add a suffix to the names of the original columns:

{{code_block('user-guide/getting-started/expressions','expression-expansion',['select','alias','Expr.name'])}}
{{code_block('user-guide/getting-started','expression-expansion',['select','alias','Expr.name'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:expression-expansion"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:expression-expansion"
```

You can check other sections of the user guide to learn more about [basic operations](expressions/operators.md) or [column selections](expressions/column-selections.md).
Expand All @@ -90,10 +90,10 @@ You can check other sections of the user guide to learn more about [basic operat
The context `with_columns` is very similar to the context `select` but `with_columns` adds columns to the dataframe instead of selecting them.
Notice how the resulting dataframe contains the four columns of the original dataframe plus the two new columns introduced by the expressions inside `with_columns`:

{{code_block('user-guide/getting-started/expressions','with_columns',['with_columns'])}}
{{code_block('user-guide/getting-started','with_columns',['with_columns'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:with_columns"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:with_columns"
```

In the example above we also decided to use named expressions instead of the method `alias` to specify the names of the new columns.
Expand All @@ -103,51 +103,51 @@ Other contexts like `select` and `group_by` also accept named expressions.

The context `filter` allows us to create a second dataframe with a subset of the rows of the original one:

{{code_block('user-guide/getting-started/expressions','filter',['filter','Expr.dt'])}}
{{code_block('user-guide/getting-started','filter',['filter','Expr.dt'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:filter"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:filter"
```

You can also provide multiple predicate expressions as separate parameters, which is more convenient than putting them all together with `&`:

{{code_block('user-guide/getting-started/expressions','filter-multiple',['filter','is_between'])}}
{{code_block('user-guide/getting-started','filter-multiple',['filter','is_between'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:filter-multiple"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:filter-multiple"
```

### `group_by`

The context `group_by` can be used to group together the rows of the dataframe that share the same value across one or more expressions.
The example below counts how many people were born in each decade:

{{code_block('user-guide/getting-started/expressions','group_by',['group_by','alias','Expr.dt'])}}
{{code_block('user-guide/getting-started','group_by',['group_by','alias','Expr.dt'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:group_by"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:group_by"
```

The keyword argument `maintain_order` forces Polars to present the resulting groups in the same order as they appear in the original dataframe.
This slows down the grouping operation but is used here to ensure reproducibility of the examples.

After using the context `group_by` we can use `agg` to compute aggregations over the resulting groups:

{{code_block('user-guide/getting-started/expressions','group_by-agg',['group_by','agg'])}}
{{code_block('user-guide/getting-started','group_by-agg',['group_by','agg'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:group_by-agg"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:group_by-agg"
```

### More complex queries

Contexts and the expressions within can be chained to create more complex queries according to your needs.
In the example below we combine some of the contexts we have seen so far to create a more complex query:

{{code_block('user-guide/getting-started/expressions','complex',['group_by','agg','select','with_columns','Expr.str','Expr.list'])}}
{{code_block('user-guide/getting-started','complex',['group_by','agg','select','with_columns','Expr.str','Expr.list'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:complex"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:complex"
```

## Combining dataframes
Expand All @@ -160,10 +160,10 @@ In this section, we show an example of a join and an example of a concatenation.
Polars provides many different join algorithms.
The example below shows how to use a left outer join to combine two dataframes when a column can be used as a unique identifier to establish a correspondence between rows across the dataframes:

{{code_block('user-guide/getting-started/expressions','join',['join'])}}
{{code_block('user-guide/getting-started','join',['join'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:join"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:join"
```

Polars provides many different join algorithms that you can learn about in the [joins section of the user guide](transformations/joins.md).
Expand All @@ -173,10 +173,10 @@ Polars provides many different join algorithms that you can learn about in the [
Concatenating dataframes creates a taller or wider dataframe, depending on the method used.
Assuming we have a second dataframe with data from other people, we could use vertical concatenation to create a taller dataframe:

{{code_block('user-guide/getting-started/expressions','concat',['concat'])}}
{{code_block('user-guide/getting-started','concat',['concat'])}}

```python exec="on" result="text" session="getting-started/expressions"
--8<-- "python/user-guide/getting-started/expressions.py:concat"
```python exec="on" result="text" session="getting-started"
--8<-- "python/user-guide/getting-started.py:concat"
```

Polars provides vertical and horizontal concatenation, as well as diagonal concatenation.
Expand Down

0 comments on commit 049a91e

Please sign in to comment.