medianOfOrNull
Returns the median of the comparable values that the given expression returns for each element of this DataColumn, or null if there is nothing to compute the median of.
This overload is meant for expressions returning non-number self-comparable values, like strings or dates. For expressions returning primitive numbers, the medianOfOrNull overload with a skipNaN parameter is used, which returns an interpolated Double.
Note: Due to a limitation in Kotlin's overload resolution (KT-76683), calling this overload with a lambda returning non-number comparable values, like strings or dates, requires either explicit type arguments, or passing the lambda inside the parentheses (see the examples below). For a lambda returning primitive numbers, the overload returning a Double is picked automatically.
From Kotlin 2.5, this limitation can be lifted by enabling eager lambda analysis (KT-51107) with the compiler option -XXLanguage:+EagerLambdaAnalysis, like kotlin { compilerOptions { freeCompilerArgs.add("-XXLanguage:+EagerLambdaAnalysis") } } in Gradle. Then, the right overload is picked for lambdas returning non-number comparable values too, without explicit type arguments. Note that this is an internal compiler option without any stability guarantees, so it is not recommended for production use yet.
The result of the expression is considered the 'input' of this operation.
Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.
null values in the input are always ignored.
Returns null when there is nothing to compute the median of, for instance, when the input is empty or contains only null values.
The resulting median is selected from the values using quantile estimation method R3, so it has the same type as the values (minus nullability, if the values were nullable).
Currently, only the default quantile estimation methods can be used. In the future, it might become possible to choose a different one, see #1121.
For more information about the resulting types: See "median Type Conversion" on the documentation website.
Don't confuse medianOfOrNull with medianByOrNull, which returns the element the median expression value belongs to instead of that value.
See also:
medianOf— throws instead of returningnullfor a column with nothing to compute the median of.percentileOfOrNull— any other percentile of the values an expression returns.The Median Operation— an overview of allmedianmodes.
For more information: See median on the documentation website.
Examples
// The median of all last names in the "name" column group, in upper case,
// or `null` if the column group is empty
df.name.medianOfOrNull<_, String> { it.lastName.uppercase() }
// The same, with the lambda inside the parentheses
df.name.medianOfOrNull({ it.lastName.uppercase() })Return
The median of the values expression returns, or null if there are no values to compute the median of.
Parameters
A function that returns the value to include for each element of this column.
Returns the median of the numbers that the given expression returns for each element of this DataColumn, as a Double, or null if there is nothing to compute the median of.
The result of the expression is considered the 'input' of this operation.
Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.
null values in the input are always ignored.
If the input contains values, the result will be NaNNaN, unless skipNaN is set to true.
Returns null when there is nothing to compute the median of, for instance, when the input is empty or contains only null values.
The resulting median is a Double, interpolated between the values using quantile estimation method R8. Converting Long values to Double may lose precision for very large values.
Currently, only the default quantile estimation methods can be used. In the future, it might become possible to choose a different one, see #1121.
Big numbers (, BigInteger) are not explicitly supported. They are self-comparable, so some overloads select a value without interpolating it, while others throw an exception at runtime. Don't rely on this; BigDecimal them to a primitive number type first.convert
For more information about the resulting types: See "median Type Conversion" on the documentation website.
Don't confuse medianOfOrNull with medianByOrNull, which returns the element the median expression value belongs to instead of that value.
See also:
medianOf— throws instead of returningnullfor a column with nothing to compute the median of.percentileOfOrNull— any other percentile of the values an expression returns.The Median Operation— an overview of allmedianmodes.
For more information: See median on the documentation website.
Example
// The median length of all first names in the "name"/"firstName" column,
// or `null` if the column is empty
df.name.firstName.medianOfOrNull { it.length }Return
The median of the values expression returns, as a Double, or null if there are no values to compute the median of.
Parameters
A function that returns the value to include for each element of this column.
Returns the median of the comparable values that the given expression returns for each row of this DataFrame, or null if there is nothing to compute the median of.
This overload is meant for expressions returning non-number self-comparable values, like strings or dates. For expressions returning primitive numbers, the medianOfOrNull overload with a skipNaN parameter is used, which returns an interpolated Double.
Note: Due to a limitation in Kotlin's overload resolution (KT-76683), calling this overload with a lambda returning non-number comparable values, like strings or dates, requires either explicit type arguments, or passing the lambda inside the parentheses (see the examples below). For a lambda returning primitive numbers, the overload returning a Double is picked automatically.
From Kotlin 2.5, this limitation can be lifted by enabling eager lambda analysis (KT-51107) with the compiler option -XXLanguage:+EagerLambdaAnalysis, like kotlin { compilerOptions { freeCompilerArgs.add("-XXLanguage:+EagerLambdaAnalysis") } } in Gradle. Then, the right overload is picked for lambdas returning non-number comparable values too, without explicit type arguments. Note that this is an internal compiler option without any stability guarantees, so it is not recommended for production use yet.
The given RowExpression is evaluated for each row of the dataframe. The row is both the receiver and the argument (it) of the expression, so the values in it can be accessed directly.
For more information: See RowExpression on the documentation website.
The result of the expression is considered the 'input' of this operation.
Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.
null values in the input are always ignored.
Returns null when there is nothing to compute the median of, for instance, when the input is empty or contains only null values.
The resulting median is selected from the values using quantile estimation method R3, so it has the same type as the values (minus nullability, if the values were nullable).
Currently, only the default quantile estimation methods can be used. In the future, it might become possible to choose a different one, see #1121.
For more information about the resulting types: See "median Type Conversion" on the documentation website.
Don't confuse medianOfOrNull with medianByOrNull, which returns the row the median expression value belongs to instead of that value.
See also:
medianOf— throws instead of returningnullwhen there's nothing to compute the median of.medianOrNull— a single median of all values in the selected columns.percentileOfOrNull— any other percentile of the values a row expression returns.The Median Operation— an overview of allmedianmodes.
For more information: See median on the documentation website.
Examples
// The median of the full names of all rows, or `null` if this dataframe is empty
df.medianOfOrNull<_, String> { name.firstName + " " + name.lastName }
// The same, with the lambda inside the parentheses
df.medianOfOrNull({ name.firstName + " " + name.lastName })Return
The median of the values expression returns, or null if there are no values to compute the median of.
Parameters
The RowExpression to compute the value to include for each row.
Returns the median of the numbers that the given expression returns for each row of this DataFrame, as a Double, or null if there is nothing to compute the median of.
The given RowExpression is evaluated for each row of the dataframe. The row is both the receiver and the argument (it) of the expression, so the values in it can be accessed directly.
For more information: See RowExpression on the documentation website.
The result of the expression is considered the 'input' of this operation.
Only self-comparable values are supported: values of a type T : Comparable<T> that are mutually comparable (like strings, primitive numbers, or dates). This includes all primitive number types, but no mix of different number types, which would result in a ClassCastException.
null values in the input are always ignored.
If the input contains values, the result will be NaNNaN, unless skipNaN is set to true.
Returns null when there is nothing to compute the median of, for instance, when the input is empty or contains only null values.
The resulting median is a Double, interpolated between the values using quantile estimation method R8. Converting Long values to Double may lose precision for very large values.
Currently, only the default quantile estimation methods can be used. In the future, it might become possible to choose a different one, see #1121.
Big numbers (, BigInteger) are not explicitly supported. They are self-comparable, so some overloads select a value without interpolating it, while others throw an exception at runtime. Don't rely on this; BigDecimal them to a primitive number type first.convert
For more information about the resulting types: See "median Type Conversion" on the documentation website.
Don't confuse medianOfOrNull with medianByOrNull, which returns the row the median expression value belongs to instead of that value.
See also:
medianOf— throws instead of returningnullwhen there's nothing to compute the median of.medianOrNull— a single median of all values in the selected columns.percentileOfOrNull— any other percentile of the values a row expression returns.The Median Operation— an overview of allmedianmodes.
For more information: See median on the documentation website.
Example
// The median weight-to-age ratio of all rows, or `null` if this dataframe is empty
df.medianOfOrNull { (weight ?: 0) / age }Return
The median of the values expression returns, as a Double, or null if there are no values to compute the median of.
Parameters
The RowExpression to compute the value to include for each row.