diff --git a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt index f7cdacd630..8b97babaf2 100644 --- a/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt +++ b/core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt @@ -367,7 +367,13 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n Char::class -> when (toClass) { Int::class -> convert { it.code } - else -> null + + else -> // convert char to string and then to target type + getConverter(typeOf(), to, options)?.let { stringConverter -> + convert { + stringConverter(it.toString()) + } + } } Int::class -> when (toClass) { diff --git a/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt b/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt index 47c49736db..55d4a802ea 100644 --- a/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt +++ b/core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt @@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.dataframe.api import io.kotest.assertions.throwables.shouldNotThrow import io.kotest.assertions.throwables.shouldThrow import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlinx.datetime.LocalTime @@ -69,6 +70,20 @@ class ConvertTests { @Test fun `convert string to enum`() { columnOf("A", "B").convertTo() shouldBe columnOf(EnumClass.A, EnumClass.B) + + dataFrameOf(columnOf("A", "B") named "colA") + .convert("colA").to() + .getColumn("colA") shouldBe columnOf(EnumClass.A, EnumClass.B).named("colA") + } + + @Test + fun `convert char to enum`() { + // Char -> String -> Enum + columnOf('A', 'B').convertTo() shouldBe columnOf(EnumClass.A, EnumClass.B) + + dataFrameOf(columnOf('A', 'B') named "colA") + .convert("colA").to() + .getColumn("colA") shouldBe columnOf(EnumClass.A, EnumClass.B).named("colA") } @JvmInline @@ -199,6 +214,13 @@ class ConvertTests { val col = columnOf(65, 66) col.convertTo() shouldBe columnOf('A', 'B') col.convertTo().convertTo() shouldBe col + + // this means + columnOf('1', '2').convertToInt() shouldNotBe columnOf(1, 2) + columnOf('1', '2').convertToInt() shouldBe columnOf(49, 50) + + // but + columnOf('1', '2').convertToString().convertToInt() shouldBe columnOf(1, 2) } @Test