forked from pola-rs/polars-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
how can i conditionally apply, parse dates
- Loading branch information
Showing
10 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pypolars as pl | ||
from pypolars.lazy import * | ||
import numpy as np | ||
|
||
df = pl.DataFrame({"range": np.arange(10), "left": ["foo"] * 10, "right": ["bar"] * 10}) | ||
|
||
out = df.lazy().with_column( | ||
when(col("range") >= 5) | ||
.then(col("left")) | ||
.otherwise(col("right")) | ||
.alias("foo_or_bar") | ||
) | ||
|
||
if __name__ == "__main__": | ||
with open("book/src/outputs/how_can_i_conditionally_apply.txt", "w") as f: | ||
f.write(str(out.collect())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pypolars as pl | ||
from pypolars.lazy import * | ||
|
||
df = pl.DataFrame( | ||
{"date": ["2020-01-02", "2020-01-03", "2020-01-04"], "index": [1, 2, 3]} | ||
) | ||
|
||
parsed = df.lazy().with_column( | ||
col("date").str_parse_date(pl.datatypes.Date32, "%Y-%m-%d") | ||
) | ||
|
||
if __name__ == "__main__": | ||
with open("book/src/outputs/how_can_i_parse_dates_0.txt", "w") as f: | ||
f.write(str(df)) | ||
with open("book/src/outputs/how_can_i_parse_dates_1.txt", "w") as f: | ||
f.write(str(parsed.collect())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# How can I conditionally apply | ||
|
||
You often want to modify or add a column to DataFrame based on some condition/predicate. This is where | ||
the `when().then().otherwise()` expressions are for. As they are basically a full English sentence, they need no further | ||
explanation. | ||
|
||
|
||
## Examples | ||
|
||
```python | ||
{{#include ../examples/how_can_i/conditionally_apply.py:1:10}} | ||
print(out.collect()) | ||
``` | ||
|
||
```text | ||
{{#include ../outputs/how_can_i_conditionally_apply.txt}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Date parsing | ||
|
||
Polars has two date data types: | ||
|
||
* Date32 | ||
- a naive date represented as the number of days since the unix epoch as a 32 bit signed integer. | ||
- Use this for Date objects | ||
* Date64 | ||
- a naive datetime represented as the number of milliseconds since the unix epoch as a 64 bit signed integer. | ||
- Use this for DateTime objects | ||
|
||
Utf8 types can be parsed as one of the two date datetypes. You can try to let Polars parse the date(time) implicitly or | ||
apply you `fmt` rule. Some examples are: | ||
|
||
* `"%Y-%m-%d"` for `"2020-12-31"` | ||
* `"%Y/%B/%d"` for `"2020/December/31"` | ||
* `"%B %y"` for `"December 20"` | ||
|
||
## Examples | ||
|
||
```python | ||
{{#include ../examples/how_can_i/parse_dates.py:4:10}} | ||
print(parsed.collect()) | ||
``` | ||
|
||
```text | ||
{{#include ../outputs/how_can_i_parse_dates_1.txt}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters