Daft
Daft copied to clipboard
better conditional expressions
Is your feature request related to a problem? Please describe.
conditional logic in daft is hard to write and read
# %%
import daft
# %%
data = {
"age": [12, 20, 19, 30, 62]
}
df = daft.from_pydict(data).sort('age')
is_senior = daft.col('age') >=55)
is_adult = daft.col('age') >= 18)
df = df.with_column(
"age_bracket",
is_senior.if_else(
"senior",
is_adult.if_else("adult", "child")
)
)
print(df.collect())
It does not have a natural flow to it, and it makes it difficult to build complex conditional logic.
Describe the solution you'd like
I think even a purely functional syntax would be better than the chaining syntax here.
condition( # if
is_senior, # is_senior:
'senior', # 'senior
condition( # else if
is_adult, # is_adult:
'adult', # 'adult'
'child' # else: 'child
)
)
you could easily add kwargs to this for more readability
condition(
if = is_senior,
then = 'senior',
else = condition(
if = is_adult,
then = 'adult',
else = 'child'
)
)
Alternatively, some languages use a when/then/otherwise
syntax
is_senior = daft.col('age') >=55)
is_adult = daft.col('age') >= 18)
condition = (daft
.when(is_senior).then('senior')
.when(is_adult).then('adult')
.otherwise('child'))
df = df.with_column('age_bracket' condition)
Other dsl's use a switch/match syntax
age = daft.col('age')
(daft.match(age)
.case(age >= 55, 'senior')
.case(age >= 18, 'adult')
.case(19, 'nineteen')
.case(20, 'twenty')
.default('child'))
IMO, any of these proposed alternatives are more intuitive than the current syntax.