| |
- trunc(column_expression_or_constant1, column_expression_or_constant2)
- DESCRIPTION:
Function returns numeric_value (column_expression_or_constant1) truncated
places_value (column_expression_or_constant2) places to the right or left
of the decimal point.
TRUNC functions as follows:
* It truncates places_value places to the right of the decimal point if
places_value is positive.
* It truncates (makes 0) places_value places to the left of the decimal
point if places_value is negative.
* It truncates to 0 places if places_value is zero or is omitted.
* If numeric_value or places_value is NULL, the function returns NULL.
PARAMETERS:
column_expression_or_constant1:
Required Argument.
Specifies a ColumnExpression of a numeric column or a numeric constant value.
Format for the argument: '<dataframe>.<dataframe_column>.expression'.
column_expression_or_constant2:
Optional Argument.
Specifies a ColumnExpression of a numeric column or a numeric constant value.
This decides the number of places to truncate.
If not specified, values specified by "column_expression_or_constant1" are
truncated to 0 places by default.
Format for the argument: '<dataframe>.<dataframe_column>.expression'.
NOTE:
Function accepts positional arguments only.
EXAMPLES:
# Load the data to run the example.
>>> load_example_data("dataframe", "admissions_train")
>>>
# Create a DataFrame on 'admissions_train' table.
>>> admissions_train = DataFrame("admissions_train")
>>> admissions_train
masters gpa stats programming admitted
id
22 yes 3.46 Novice Beginner 0
36 no 3.00 Advanced Novice 0
15 yes 4.00 Advanced Advanced 1
38 yes 2.65 Advanced Beginner 1
5 no 3.44 Novice Novice 0
17 no 3.83 Advanced Advanced 1
34 yes 3.85 Advanced Beginner 0
13 no 4.00 Advanced Novice 1
26 yes 3.57 Advanced Advanced 1
19 yes 1.98 Advanced Advanced 0
>>>
# Import func from sqlalchemy to execute sign() function.
>>> from sqlalchemy import func
# Create a sqlalchemy Function object and pass the Function object as input to DataFrame.assign().
# Note: We are using 'TRUNC', 'trunc' and 'Trunc' as function names. Function names are case-insensitive.
>>> df = admissions_train.assign(trunc_gpa_1 = func.TRUNC(admissions_train.gpa.expression),
... trunc_gpa_admitted = func.trunc(admissions_train.gpa.expression,
... admissions_train.admitted.expression),
... trunc_const_3 = func.Trunc(234232342.433412, -3))
>>> print(df)
masters gpa stats programming admitted trunc_const_3 trunc_gpa_1 trunc_gpa_admitted
id
5 no 3.44 Novice Novice 0 234232000.0 3.0 3.0
7 yes 2.33 Novice Novice 1 234232000.0 2.0 2.3
22 yes 3.46 Novice Beginner 0 234232000.0 3.0 3.0
17 no 3.83 Advanced Advanced 1 234232000.0 3.0 3.8
13 no 4.00 Advanced Novice 1 234232000.0 4.0 4.0
19 yes 1.98 Advanced Advanced 0 234232000.0 1.0 1.0
36 no 3.00 Advanced Novice 0 234232000.0 3.0 3.0
15 yes 4.00 Advanced Advanced 1 234232000.0 4.0 4.0
34 yes 3.85 Advanced Beginner 0 234232000.0 3.0 3.0
40 yes 3.95 Novice Beginner 0 234232000.0 3.0 3.0
>>>
|