dataframe icon indicating copy to clipboard operation
dataframe copied to clipboard

Exception in documentation example of `maxByOrNull`

Open sorokod opened this issue 4 months ago • 5 comments

In Operations/Access/Get Rows we have:

df.maxByOrNull { weight }

Executing this in a Kotlin Notebook cell results in: Type mismatch: inferred type is Int? but Int was expected

Gradle settings:

plugins {
    kotlin("jvm") version "2.2.20-Beta1"
    kotlin("plugin.dataframe") version "2.2.20-Beta1"
}

dependencies {
    implementation("org.jetbrains.kotlinx:dataframe:1.0.0-Beta2")
    testImplementation(kotlin("test"))
}

sorokod avatar Aug 21 '25 11:08 sorokod

Also caused by https://github.com/Kotlin/dataframe/issues/1116

It's listed in the known issues for the release (https://github.com/Kotlin/dataframe/releases/tag/v1.0.0-Beta2) that for 1.0.0-Beta2 in notebooks "min, max, median, and percentile might not work". Stick to 1.0.0-dev-7089 if you want to use 1.0.0-Beta2 in notebooks for now.

Jolanrensen avatar Aug 21 '25 12:08 Jolanrensen

Right, I can see that there is a lot of work in progress. Do note that this happens in 1.0.0-dev-7089 too.

sorokod avatar Aug 21 '25 18:08 sorokod

Well yes, in the example, the weight column is nullable, so it's not allowed in notebooks in 1.0.0-dev-7089 with the maxByOrNull {} operation. That's what the error message says: "Type mismatch: inferred type is Int? but Int was expected".

What would work is something like: df.maxByOrNull { weight ?: 0 }.

But yes, the workaround is not very nice, so I hope https://youtrack.jetbrains.com/issue/KT-76441/IllegalStateException-null-DefinitelyNotNullType-for-T-exception-while-analyzing-expression gets fixed soon.

Jolanrensen avatar Aug 21 '25 19:08 Jolanrensen

@Jolanrensen please revisit this also

zaleslaw avatar Dec 05 '25 13:12 zaleslaw

https://github.com/Kotlin/dataframe/issues/1401#issuecomment-3616911559

Jolanrensen avatar Dec 05 '25 13:12 Jolanrensen