Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.kotlinx.dataframe.Selector
import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
import org.jetbrains.kotlinx.dataframe.annotations.Refine
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
import org.jetbrains.kotlinx.dataframe.impl.columnName
Expand All @@ -25,7 +26,7 @@ import kotlin.reflect.typeOf

@Deprecated(DEPRECATED_ACCESS_API)
@AccessApiOverload
public inline fun <C, reified R> ColumnReference<C>.map(
internal inline fun <C, reified R> ColumnReference<C>.map(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this affect our.api file? otherwise do an apiDump

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did run, seems like it doesn't.

infer: Infer = Infer.Nulls,
noinline transform: (C) -> R,
): ColumnReference<R> = createComputedColumnReference(name(), typeOf<R>(), infer) { transform(this@map()) }
Expand Down Expand Up @@ -69,6 +70,13 @@ public inline fun <T, R> DataColumn<T>.mapIndexed(

// region DataFrame

/**
* Note: When this method is applied to a **[ColumnGroup]**,
* its behavior differs from [DataColumn.map].
* To apply `map` as if on a regular [DataColumn] (i.e., a column of [DataRow]s
* whose values correspond to values in columns of the group),
* call [ColumnGroup.asDataColumn] first.
*/
public inline fun <T, R> DataFrame<T>.map(transform: RowExpression<T, R>): List<R> = rows().map { transform(it, it) }

public inline fun <T, reified R> DataFrame<T>.mapToColumn(
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/map.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.kotlinx.dataframe.api

import io.kotest.matchers.shouldBe
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.junit.Test

class MapTests {
Expand All @@ -18,4 +19,19 @@ class MapTests {
}
df["b"][1] shouldBe 5
}

@Test
fun `ColumnGroup map`() {
val group = dataFrameOf("x", "y")(1, 10, 2, 20, 3, 30).asColumnGroup("g")
val sums = group.asDataFrame().map { row -> row["x"] as Int + row["y"] as Int }
sums shouldBe listOf(11, 22, 33)
}

@Test
fun `ColumnGroup asDataColumn map`() {
val group = dataFrameOf("x", "y")(1, 10, 2, 20, 3, 30).asColumnGroup("g")
val col: DataColumn<Int> = group.asDataColumn().map { it["x"] as Int + it["y"] as Int }
col.name() shouldBe "g"
col.toList() shouldBe listOf(11, 22, 33)
}
}
3 changes: 2 additions & 1 deletion dataframe-json/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ dependencies {
implementation(libs.serialization.json)
implementation(libs.sl4j)

testImplementation(libs.kotlin.test)
// Use Kotlin test integration for JUnit 5 to satisfy variant 'kotlin-test-framework-junit5'
testImplementation(libs.kotlin.test.junit5)
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.jupiter.engine)
testImplementation(libs.junit.jupiter.params)
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ kotlin-compiler = { group = "org.jetbrains.kotlin", name = "kotlin-compiler", ve
kotlin-compiler-embeddable = { group = "org.jetbrains.kotlin", name = "kotlin-compiler-embeddable", version.ref = "kotlin" }
kotlin-compiler-internal-test-framework = { group = "org.jetbrains.kotlin", name = "kotlin-compiler-internal-test-framework", version.ref = "kotlin" }
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlin" }
kotlin-test-junit5 = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit5", version.ref = "kotlin" }
kotlin-script-runtime = { group = "org.jetbrains.kotlin", name = "kotlin-script-runtime", version.ref = "kotlin" }
kotlin-annotations-jvm = { group = "org.jetbrains.kotlin", name = "kotlin-annotations-jvm", version.ref = "kotlin" }
kotlin-jupyter-test-kit = { group = "org.jetbrains.kotlinx", name = "kotlin-jupyter-test-kit", version.ref = "kotlinJupyter" }
Expand Down