CaseBuilder

class hail.expr.builders.CaseBuilder[source]

Class for chaining multiple if-else statements.

Examples

>>> x = hl.literal('foo bar baz')
>>> expr = (hl.case()
...           .when(x[:3] == 'FOO', 1)
...           .when(x.length() == 11, 2)
...           .when(x == 'secret phrase', 3)
...           .default(0))
>>> hl.eval(expr)
2

Notes

All expressions appearing as the then parameters to when() or default() method calls must be the same type.

Parameters

missing_false (bool) – Treat missing predicates as False.

See also

case(), cond(), switch()

Attributes

Methods

default

Finish the case statement by adding a default case.

or_error

Finish the case statement by throwing an error with the given message.

or_missing

Finish the case statement by returning missing.

when

Add a branch.

default(then)[source]

Finish the case statement by adding a default case.

Notes

If no condition from a when() call is True, then then is returned.

Parameters

then (Expression)

Returns

Expression

or_error(message)[source]

Finish the case statement by throwing an error with the given message.

Notes

If no condition from a CaseBuilder.when() call is True, then an error is thrown.

Parameters

message (Expression of type tstr)

Returns

Expression

or_missing()[source]

Finish the case statement by returning missing.

Notes

If no condition from a CaseBuilder.when() call is True, then the result is missing.

Parameters

then (Expression)

Returns

Expression

when(condition, then)[source]

Add a branch. If condition is True, then returns then.

Warning

Missingness is treated similarly to cond(). Missingness is not treated as False. A condition that evaluates to missing will return a missing result, not proceed to the next case. Always test missingness first in a CaseBuilder.

Parameters
Returns

CaseBuilder – Mutates and returns self.