medianOrNull

Returns the median of the comparable values in this DataColumn, or null if there is nothing to compute the median of.

This overload is meant for non-number self-comparable values, like strings or dates. Columns of primitive numbers use the medianOrNull overload with a skipNaN parameter, which returns an interpolated Double.

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.

See also:

  • median — throws instead of returning null for a column with nothing to compute the median of.

  • medianOfOrNull — the median of the values an expression returns for each element.

  • medianByOrNull — the element at the median of the values a selector returns.

  • percentileOrNull — any other percentile of the values in this column.

  • The Median Operation — an overview of all median modes.

For more information: See median on the documentation website.

Example

// The median city in the "city" column (in alphabetical order),
// or `null` if the column contains no values other than `null`
df.city.medianOrNull()

Return

The median of the values in this column, or null if there are no values to compute the median of.


fun <T : Comparable<T & Any>?, Number?> DataColumn<T>.medianOrNull(skipNaN: Boolean = skipNaNDefault): Double?

Returns the median of the numbers in this DataColumn, as a Double, or null if there is nothing to compute the median of.

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 NaN values, the result will be NaN, 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, BigDecimal) 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; convert them to a primitive number type first.

For more information about the resulting types: See "median Type Conversion" on the documentation website.

See also:

  • median — throws instead of returning null for a column with nothing to compute the median of.

  • medianOfOrNull — the median of the values an expression returns for each element.

  • medianByOrNull — the element at the median of the values a selector returns.

  • percentileOrNull — any other percentile of the values in this column.

  • The Median Operation — an overview of all median modes.

For more information: See median on the documentation website.

Example

// The median weight in the "weight" column,
// or `null` if the column contains no values other than `null`
df.weight.medianOrNull()

Return

The median of the values in this column, as a Double, or null if there are no values to compute the median of.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.


fun <T, C : Comparable<C & Any>?> DataFrame<T>.medianOrNull(columns: ColumnsSelector<T, C>): C?

Returns a single median of all the comparable values in the selected columns of this DataFrame, or null if there is nothing to compute the median of.

This overload is meant for non-number self-comparable values, like strings or dates. For columns of primitive numbers, the medianOrNull 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.

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.

See also:

  • median — throws instead of returning null when there's nothing to compute the median of.

  • medianFor — the median of each selected column separately.

  • medianOfOrNull — the median of the values a row expression returns for each row.

  • percentileOrNull — any other percentile of all values in the selected columns.

  • The Median Operation — an overview of all median modes.

For more information: See median on the documentation website.

Select or express columns using the Columns Selection DSL.

This DSL is initiated by a Columns Selector lambda, which operates in the context of the Columns Selection DSL and expects you to return a SingleColumn or ColumnSet (so, a ColumnsResolver). This is an entity formed by calling any (combination) of the functions in the DSL that is or can be resolved into one or more columns.

The Columns Selection DSL allows using Extension Properties for specifying columns type- and name-safe.

Check out: Columns Selection DSL Grammar

    

See Column Selectors on the documentation website.

Examples

// The median of all first and last names in the "name" column group,
// or `null` if there are no values to compute the median of
df.medianOrNull<_, String> { name.firstName and name.lastName }
// The same, with the lambda inside the parentheses
df.medianOrNull({ name.firstName and name.lastName })

Return

The median of all the values in the selected columns, or null if there are no values to compute the median of.

Parameters

columns

The ColumnsSelector used to select the columns to compute the median of.


fun <T, C : Comparable<C & Any>?, Number?> DataFrame<T>.medianOrNull(skipNaN: Boolean = skipNaNDefault, columns: ColumnsSelector<T, C>): Double?

Returns a single median of all the numbers in the selected columns of this DataFrame, as a Double, or null if there is nothing to compute the median of.

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 NaN values, the result will be NaN, 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, BigDecimal) 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; convert them to a primitive number type first.

For more information about the resulting types: See "median Type Conversion" on the documentation website.

See also:

  • median — throws instead of returning null when there's nothing to compute the median of.

  • medianFor — the median of each selected column separately.

  • medianOfOrNull — the median of the values a row expression returns for each row.

  • percentileOrNull — any other percentile of all values in the selected columns.

  • The Median Operation — an overview of all median modes.

For more information: See median on the documentation website.

Select or express columns using the Columns Selection DSL.

This DSL is initiated by a Columns Selector lambda, which operates in the context of the Columns Selection DSL and expects you to return a SingleColumn or ColumnSet (so, a ColumnsResolver). This is an entity formed by calling any (combination) of the functions in the DSL that is or can be resolved into one or more columns.

The Columns Selection DSL allows using Extension Properties for specifying columns type- and name-safe.

Check out: Columns Selection DSL Grammar

    

See Column Selectors on the documentation website.

Example

// The median of all values in the "age" and "weight" columns,
// or `null` if there are no values to compute the median of
df.medianOrNull { age and weight }

Return

The median of all the values in the selected columns, as a Double, or null if there are no values to compute the median of.

Parameters

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.

columns

The ColumnsSelector used to select the columns to compute the median of.


fun <T> DataFrame<T>.medianOrNull(vararg columns: String, skipNaN: Boolean = skipNaNDefault): Any?

Returns a single median of all the values in the columns of this DataFrame with the given names, or null if there is nothing to compute the median of.

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 NaN values, the result will be NaN, 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.

For primitive numbers, 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. For all other self-comparable values, the resulting median is selected from the values using quantile estimation method R3, so it has the same type as those 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.

For more information about the resulting types: See "median Type Conversion" on the documentation website.

See also:

  • median — throws instead of returning null when there's nothing to compute the median of.

  • medianFor — the median of each selected column separately.

  • percentileOrNull — any other percentile of all values in the selected columns.

  • The Median Operation — an overview of all median modes.

For more information: See median on the documentation website.

Select single or multiple columns using their names as Strings. (String API).

Example

// The median of all values in the "age" and "weight" columns, as a `Double`,
// or `null` if there are no values to compute the median of
df.medianOrNull("age", "weight")

Return

The median of all the values in the selected columns, or null if there are no values to compute the median of.

Parameters

columns

The names of the columns to compute the median of. These must be existing columns whose values are mutually comparable, else an IllegalStateException is thrown.

skipNaN

If true, NaN values are ignored, just like null values. If false (the default), a NaN in the input is propagated to the result. This only has an effect on Double and Float values.


fun <T, C : Comparable<C & Any>?> DataFrame<T>.medianOrNull(vararg columns: ColumnReference<C>): C?
fun <T, C : Comparable<C & Any>?, Number?> DataFrame<T>.medianOrNull(vararg columns: ColumnReference<C>, skipNaN: Boolean = skipNaNDefault): Double?
fun <T, C : Comparable<C & Any>?> DataFrame<T>.medianOrNull(vararg columns: KProperty<C>): C?
fun <T, C : Comparable<C & Any>?, Number?> DataFrame<T>.medianOrNull(vararg columns: KProperty<C>, skipNaN: Boolean = skipNaNDefault): Double?

Deprecated

It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html