-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting now using with_columns for both polars eager and lazy execution. Inherits from with_columns_base. Some functionality is shared with h_pandas.with_columns -- TODO: refactor out common logic when appropriate.
- Loading branch information
1 parent
debb559
commit b6b12a2
Showing
16 changed files
with
2,402 additions
and
123 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,8 @@ | ||
# Using with_columns with Polars | ||
|
||
We show the ability to use the familiar `with_columns` from `polars`. Supported for both `pl.DataFrame` and `pl.LazyFrame`. | ||
|
||
To see the example look at the notebook. | ||
|
||
![image info](./DAG_DataFrame.png) | ||
![image info](./DAG_lazy.png) |
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,51 @@ | ||
import polars as pl | ||
|
||
from hamilton.function_modifiers import config | ||
|
||
""" | ||
Notes: | ||
1. This file is used for all the [ray|dask|spark]/hello_world examples. | ||
2. It therefore show cases how you can write something once and not only scale it, but port it | ||
to different frameworks with ease! | ||
""" | ||
|
||
|
||
@config.when(case="millions") | ||
def avg_3wk_spend__millions(spend: pl.Series) -> pl.Series: | ||
"""Rolling 3 week average spend.""" | ||
return ( | ||
spend.to_frame("spend").select(pl.col("spend").rolling_mean(window_size=3) / 1e6) | ||
).to_series(0) | ||
|
||
|
||
@config.when(case="thousands") | ||
def avg_3wk_spend__thousands(spend: pl.Series) -> pl.Series: | ||
"""Rolling 3 week average spend.""" | ||
return ( | ||
spend.to_frame("spend").select(pl.col("spend").rolling_mean(window_size=3) / 1e3) | ||
).to_series(0) | ||
|
||
|
||
def spend_per_signup(spend: pl.Series, signups: pl.Series) -> pl.Series: | ||
"""The cost per signup in relation to spend.""" | ||
return spend / signups | ||
|
||
|
||
def spend_mean(spend: pl.Series) -> float: | ||
"""Shows function creating a scalar. In this case it computes the mean of the entire column.""" | ||
return spend.mean() | ||
|
||
|
||
def spend_zero_mean(spend: pl.Series, spend_mean: float) -> pl.Series: | ||
"""Shows function that takes a scalar. In this case to zero mean spend.""" | ||
return spend - spend_mean | ||
|
||
|
||
def spend_std_dev(spend: pl.Series) -> float: | ||
"""Function that computes the standard deviation of the spend column.""" | ||
return spend.std() | ||
|
||
|
||
def spend_zero_mean_unit_variance(spend_zero_mean: pl.Series, spend_std_dev: float) -> pl.Series: | ||
"""Function showing one way to make spend have zero mean and unit variance.""" | ||
return spend_zero_mean / spend_std_dev |
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,47 @@ | ||
import polars as pl | ||
|
||
from hamilton.function_modifiers import config | ||
|
||
""" | ||
Notes: | ||
1. This file is used for all the [ray|dask|spark]/hello_world examples. | ||
2. It therefore show cases how you can write something once and not only scale it, but port it | ||
to different frameworks with ease! | ||
""" | ||
|
||
|
||
@config.when(case="millions") | ||
def avg_3wk_spend__millions(spend: pl.Expr) -> pl.Expr: | ||
"""Rolling 3 week average spend.""" | ||
return spend.rolling_mean(window_size=3) / 1e6 | ||
|
||
|
||
@config.when(case="thousands") | ||
def avg_3wk_spend__thousands(spend: pl.Expr) -> pl.Expr: | ||
"""Rolling 3 week average spend.""" | ||
return spend.rolling_mean(window_size=3) / 1e3 | ||
|
||
|
||
def spend_per_signup(spend: pl.Expr, signups: pl.Expr) -> pl.Expr: | ||
"""The cost per signup in relation to spend.""" | ||
return spend / signups | ||
|
||
|
||
def spend_mean(spend: pl.Expr) -> float: | ||
"""Shows function creating a scalar. In this case it computes the mean of the entire column.""" | ||
return spend.mean() | ||
|
||
|
||
def spend_zero_mean(spend: pl.Expr, spend_mean: float) -> pl.Expr: | ||
"""Shows function that takes a scalar. In this case to zero mean spend.""" | ||
return spend - spend_mean | ||
|
||
|
||
def spend_std_dev(spend: pl.Expr) -> float: | ||
"""Function that computes the standard deviation of the spend column.""" | ||
return spend.std() | ||
|
||
|
||
def spend_zero_mean_unit_variance(spend_zero_mean: pl.Expr, spend_std_dev: float) -> pl.Expr: | ||
"""Function showing one way to make spend have zero mean and unit variance.""" | ||
return spend_zero_mean / spend_std_dev |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Oops, something went wrong.