Skip to content

Commit f86a6cc

Browse files
committed
implicit conversion from Char to String in DataColumn.convertTo and DataFrame.convert()
1 parent df2ca63 commit f86a6cc

File tree

2 files changed

+29
-1
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

2 files changed

+29
-1
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,13 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
367367

368368
Char::class -> when (toClass) {
369369
Int::class -> convert<Char> { it.code }
370-
else -> null
370+
371+
else -> // convert char to string and then to target type
372+
getConverter(typeOf<String>(), to, options)?.let { stringConverter ->
373+
convert<Char> {
374+
stringConverter(it.toString())
375+
}
376+
}
371377
}
372378

373379
Int::class -> when (toClass) {

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.dataframe.api
33
import io.kotest.assertions.throwables.shouldNotThrow
44
import io.kotest.assertions.throwables.shouldThrow
55
import io.kotest.matchers.shouldBe
6+
import io.kotest.matchers.shouldNotBe
67
import kotlinx.datetime.Clock
78
import kotlinx.datetime.Instant
89
import kotlinx.datetime.LocalTime
@@ -69,6 +70,20 @@ class ConvertTests {
6970
@Test
7071
fun `convert string to enum`() {
7172
columnOf("A", "B").convertTo<EnumClass>() shouldBe columnOf(EnumClass.A, EnumClass.B)
73+
74+
dataFrameOf(columnOf("A", "B") named "colA")
75+
.convert("colA").to<EnumClass>()
76+
.getColumn("colA") shouldBe columnOf(EnumClass.A, EnumClass.B).named("colA")
77+
}
78+
79+
@Test
80+
fun `convert char to enum`() {
81+
// Char -> String -> Enum
82+
columnOf('A', 'B').convertTo<EnumClass>() shouldBe columnOf(EnumClass.A, EnumClass.B)
83+
84+
dataFrameOf(columnOf('A', 'B') named "colA")
85+
.convert("colA").to<EnumClass>()
86+
.getColumn("colA") shouldBe columnOf(EnumClass.A, EnumClass.B).named("colA")
7287
}
7388

7489
@JvmInline
@@ -199,6 +214,13 @@ class ConvertTests {
199214
val col = columnOf(65, 66)
200215
col.convertTo<Char>() shouldBe columnOf('A', 'B')
201216
col.convertTo<Char>().convertTo<Int>() shouldBe col
217+
218+
// this means
219+
columnOf('1', '2').convertToInt() shouldNotBe columnOf(1, 2)
220+
columnOf('1', '2').convertToInt() shouldBe columnOf(49, 50)
221+
222+
// but
223+
columnOf('1', '2').convertToString().convertToInt() shouldBe columnOf(1, 2)
202224
}
203225

204226
@Test

0 commit comments

Comments
 (0)