Skip to content

Commit 2c4f742

Browse files
committed
updates convert and convertTo docs with new char parser behavior and generally a better explanation of how convertTo works
1 parent ccdd066 commit 2c4f742

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package org.jetbrains.kotlinx.dataframe.samples.api
44

55
import io.kotest.matchers.shouldBe
6+
import org.jetbrains.kotlinx.dataframe.AnyFrame
67
import org.jetbrains.kotlinx.dataframe.DataFrame
78
import org.jetbrains.kotlinx.dataframe.DataRow
89
import org.jetbrains.kotlinx.dataframe.alsoDebug
@@ -1102,11 +1103,17 @@ class Modify : TestBase() {
11021103
@TransformDataFrameExpressions
11031104
fun customConverters() {
11041105
// SampleStart
1105-
val df = dataFrameOf("a", "b")(1, "2")
1106+
val df: AnyFrame = dataFrameOf(
1107+
"a" to columnOf(1, 2, 3),
1108+
"b" to columnOf("1", "2", "3"),
1109+
)
11061110
df.convertTo<MySchema> {
1107-
convert<Int>().with { MyType(it) } // converts `a` from Int to MyType
1108-
parser { MyType(it.toInt()) } // converts `b` from String to MyType
1109-
fill { c }.with { a.value + b.value } // computes missing column `c`
1111+
// providing the converter: Int -> MyType, so column `a` can be converted
1112+
convert<Int>().with { MyType(it) }
1113+
// providing the parser: String -> MyType, so column `b` can be converted
1114+
parser { MyType(it.toInt()) }
1115+
// providing the filler for `c`, as it's missing in `df`
1116+
fill { c }.with { a.value + b.value }
11101117
}
11111118
// SampleEnd
11121119
}

docs/StardustDocs/topics/convert.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ df.convert { name }.asColumn { col ->
5656
<!---END-->
5757

5858

59-
`convert` supports automatic type conversions between the following types:
59+
`convert {}.to<>()` supports automatic type conversions between the following types:
6060
* `String`, `Char` (uses [`parse`](parse.md) to convert from `String` to other types)
6161
* `Boolean`
6262
* `Byte`
@@ -71,6 +71,7 @@ df.convert { name }.asColumn { col ->
7171
* `LocalDate` (kotlinx.datetime and java.time)
7272
* `LocalTime` (kotlinx.datetime and java.time)
7373
* `Instant` (kotlinx.datetime, kotlin.time, and java.time)
74+
* `enum` classes (by name)
7475

7576
Note that converting between `Char` and `Int` is done by ASCII character code.
7677
This means the `Char` `'1'` becomes the `Int` `49`.

docs/StardustDocs/topics/convertTo.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
[//]: # (title: convertTo)
22
<!---IMPORT org.jetbrains.kotlinx.dataframe.samples.api.Modify-->
33

4-
[Converts](convert.md) columns in [`DataFrame`](DataFrame.md) to match a given schema [`Schema`](schema.md).
4+
[Converts](convert.md) all columns in the [`DataFrame`](DataFrame.md) to match a given schema [`Schema`](schema.md).
55

66
```kotlin
77
convertTo<Schema>(excessiveColumns = ExcessiveColumns.Keep)
88
```
99

10-
**Related operations**: [](adjustSchema.md)
10+
**Related operations**: [](adjustSchema.md), [](convert.md)
11+
12+
Conversion to match the target schema is done mostly automatically;
13+
DataFrame knows how to convert between many types (see [](convert.md) for details and the supported types).
14+
15+
However, if you have a custom type in your target schema, or the automatic conversion fails,
16+
you can provide a custom converter, parser, or filler for it.
17+
These have priority over the automatic ones.
1118

1219
Customization DSL:
13-
* `convert`—how specific column types should be converted
14-
* `parser`—how to parse strings into custom types
15-
* `fill`—how to fill missing columns
20+
* `convert<A>.with { it.toB() }`
21+
* Provides `convertTo<>()` with the knowledge of how to convert `A` to `B`
22+
* `parser { YourType.fromString(it) }`
23+
* Provides `convertTo<>()` with the knowledge of how to parse strings/chars into `YourType`
24+
* Shortcut for `convert<String>().with { YourType.fromString(it) }`
25+
* Chars are treated as strings unless you explicitly specify `convert<Char>().with { YourType.fromChar(it) }`
26+
* `fill { some cols }.with { rowExpression }`
27+
* Makes `convertTo<>()` fill missing (or existing) columns from the target schema
28+
with values computed by the given row expression
1629

1730
<!---FUN customConvertersData-->
1831

@@ -27,11 +40,17 @@ class MySchema(val a: MyType, val b: MyType, val c: Int)
2740
<!---FUN customConverters-->
2841

2942
```kotlin
30-
val df = dataFrameOf("a", "b")(1, "2")
43+
val df: AnyFrame = dataFrameOf(
44+
"a" to columnOf(1, 2, 3),
45+
"b" to columnOf("1", "2", "3"),
46+
)
3147
df.convertTo<MySchema> {
32-
convert<Int>().with { MyType(it) } // converts `a` from Int to MyType
33-
parser { MyType(it.toInt()) } // converts `b` from String to MyType
34-
fill { c }.with { a.value + b.value } // computes missing column `c`
48+
// providing the converter: Int -> MyType, so column `a` can be converted
49+
convert<Int>().with { MyType(it) }
50+
// providing the parser: String -> MyType, so column `b` can be converted
51+
parser { MyType(it.toInt()) }
52+
// providing the filler for `c`, as it's missing in `df`
53+
fill { c }.with { a.value + b.value }
3554
}
3655
```
3756

0 commit comments

Comments
 (0)