API
Docs

Abs

Function to get the absolute value of a number.

Returns

Expr

Returns an expression object denoting the absolute value of the input data. The data type of the resulting expression is same as that of the input.

1from fennel.expr import col
2
3expr = col("x").abs()  # equivalent to col("x").num.abs()
4
5assert expr.typeof(schema={"x": int}) == int
6assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
7assert expr.typeof(schema={"x": float}) == float
8assert expr.typeof(schema={"x": Optional[float]}) == Optional[float]
9
10# can be evaluated with a dataframe
11df = pd.DataFrame({"x": pd.Series([1, -2, pd.NA], dtype=pd.Int64Dtype())})
12assert expr.eval(df, schema={"x": Optional[int]}).tolist() == [1, 2, pd.NA]
13
14with pytest.raises(ValueError):
15    expr.typeof(schema={"x": str})
Getting absolute value of numeric using abs

python

Errors

Invoking on a non-numeric type:

Error during typeof or eval if the input expression is not of type int, float, optional int or optional float.

Ceil

Function in num namespace to get the ceil of a number.

Returns

Expr

Returns an expression object denoting the ceil of the input data. The data type of the resulting expression is int if the input was int or float or Optional[int] when the input is Optional[int] or Optional[float].

1from fennel.expr import col
2
3expr = col("x").ceil()  # equivalent to col("x").num.ceil()
4assert expr.typeof(schema={"x": int}) == int
5assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
6assert expr.typeof(schema={"x": float}) == int
7assert expr.typeof(schema={"x": Optional[float]}) == Optional[int]
8
9# can be evaluated with a dataframe
10df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
11assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
12    2,
13    -2,
14    pd.NA,
15]
16
17with pytest.raises(ValueError):
18    expr.typeof(schema={"x": str})
Getting ceil value of a number

python

Errors

Invoking on a non-numeric type:

Error during typeof or eval if the input expression is not of type int, float, optional int or optional float.

Floor

Function in num namespace to get the floor of a number.

Returns

Expr

Returns an expression object denoting the floor of the input data. The data type of the resulting expression is int if the input was int or float or Optional[int] when the input is Optional[int] or Optional[float].

1from fennel.expr import col
2
3expr = col("x").floor()  # equivalent to col("x").num.floor()
4assert expr.typeof(schema={"x": int}) == int
5assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
6assert expr.typeof(schema={"x": float}) == int
7assert expr.typeof(schema={"x": Optional[float]}) == Optional[int]
8
9# can be evaluated with a dataframe
10df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
11assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
12    1,
13    -3,
14    pd.NA,
15]
16
17with pytest.raises(ValueError):
18    expr.typeof(schema={"x": str})
Getting floor value of a number

python

Errors

Invoking on a non-numeric type:

Error during typeof or eval if the input expression is not of type int, float, optional int or optional float.

Round

Function in num namespace to round a number.

Parameters

precision:int

Default: 0

The number of the decimal places to round the input to.

Returns

Expr

Returns an expression object denoting the rounded value of the input data. The data type of the resulting expression is int / Optional[int] if precision is set to 0 or float / Optional[int] for precisions > 0.

1from fennel.expr import col
2
3expr = col("x").ceil()  # equivalent to col("x").num.ceil()
4assert expr.typeof(schema={"x": int}) == int
5assert expr.typeof(schema={"x": Optional[int]}) == Optional[int]
6assert expr.typeof(schema={"x": float}) == int
7assert expr.typeof(schema={"x": Optional[float]}) == Optional[int]
8
9# can be evaluated with a dataframe
10df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
11assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
12    2,
13    -2,
14    pd.NA,
15]
16
17with pytest.raises(ValueError):
18    expr.typeof(schema={"x": str})
Rounding a number using Fennel expressions

python

Errors

Invoking on a non-numeric type:

Error during typeof or eval if the input expression is not of type int, float, optional int or optional float.

Invalid precision:

Precision must be a non-negative integer.

To String

Function in num namespace to convert a number to a string.

Returns

Expr

Returns an expression object denoting the string value of the input data. The data type of the resulting expression is str (or Optional[str] if the input is an optional number).

1from fennel.expr import col
2
3expr = col("x").num.to_string()
4
5# type is str or optional str
6assert expr.typeof(schema={"x": int}) == str
7assert expr.typeof(schema={"x": Optional[int]}) == Optional[str]
8assert expr.typeof(schema={"x": float}) == str
9assert expr.typeof(schema={"x": Optional[float]}) == Optional[str]
10
11# can be evaluated with a dataframe
12df = pd.DataFrame({"x": pd.Series([1.1, -2.3, None])})
13assert expr.eval(df, schema={"x": Optional[float]}).tolist() == [
14    "1.1",
15    "-2.3",
16    pd.NA,
17]
Converting a number to a string using Fennel expressions

python

Errors

Invoking on a non-numeric type:

Error during typeof or eval if the input expression is not of type int, float, optional int or optional float.