DataFrameColumnExpr
laktory.models.dataframe.DataFrameColumnExpr
¤
Bases: BaseModel, PipelineChild
DataFrame Column Expression defined with a string representation of DataFrame API expression (native or Narwhals) or a SQL statement.
Examples:
Define serializable expressions in native DataFrame API.
import polars as pl
import laktory as lk
df = pl.DataFrame(
{
"x": [1, 2, 3],
}
)
expr1 = lk.models.DataFrameColumnExpr(
expr="pl.col('x')+pl.lit(1)",
dataframe_backend="POLARS",
dataframe_api="NATIVE",
)
expr2 = lk.models.DataFrameColumnExpr(
expr="2*x + 1",
type="SQL",
dataframe_backend="POLARS",
dataframe_api="NATIVE",
)
df = df.with_columns(
y1=expr1.to_expr(),
y2=expr2.to_expr(),
)
print(df)
'''
| x | y1 | y2 |
|---|----|----|
| 1 | 2 | 3 |
| 2 | 3 | 5 |
| 3 | 4 | 7 |
'''
Define serializable expressions in Narwhals DataFrame API.
import narwhals as nw
import polars as pl
import laktory as lk
df = nw.from_native(
pl.DataFrame(
{
"x": [1, 2, 3],
}
)
)
expr1 = lk.models.DataFrameColumnExpr(
expr="nw.col('x')+nw.lit(1)",
dataframe_backend="POLARS",
dataframe_api="NARWHALS",
)
expr2 = lk.models.DataFrameColumnExpr(
expr="2*x + 1",
type="SQL",
dataframe_backend="POLARS",
dataframe_api="NARWHALS",
)
df = df.with_columns(
y1=expr1.to_expr(),
y2=expr2.to_expr(),
)
print(df)
'''
┌──────────────────┐
|Narwhals DataFrame|
|------------------|
| | x | y1 | y2 | |
| |---|----|----| |
| | 1 | 2 | 3 | |
| | 2 | 3 | 5 | |
| | 3 | 4 | 7 | |
└──────────────────┘
'''
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
Expression string representation
TYPE:
|
type
|
Expression type: DF or SQL. If
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
to_expr |
Column expression expressed as DataFrame API object |
to_sql_expr |
Column expression expressed as a SQL Statement |
to_expr()
¤
Column expression expressed as DataFrame API object
Source code in laktory/models/dataframe/dataframecolumnexpr.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
to_sql_expr()
¤
Column expression expressed as a SQL Statement
Source code in laktory/models/dataframe/dataframecolumnexpr.py
159 160 161 162 163 164 165 166 167 | |