Skip to content

Commit d13bef7

Browse files
committed
Update docs for convertTo
1 parent 913d6a0 commit d13bef7

File tree

3 files changed

+39
-16
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api
  • docs/StardustDocs/topics

3 files changed

+39
-16
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,25 @@ import kotlin.reflect.KProperty
1616
import kotlin.reflect.KType
1717
import kotlin.reflect.typeOf
1818

19-
public enum class ExcessiveColumns { Remove, Keep, Fail }
19+
/**
20+
* Specifies how to handle columns in original dataframe that were not mathced to any column in destination dataframe schema.
21+
*/
22+
public enum class ExcessiveColumns {
23+
/**
24+
* Remove excessive columns from resulting dataframe
25+
*/
26+
Remove,
27+
28+
/**
29+
* Keep excessive columns in resulting dataframe
30+
*/
31+
Keep,
32+
33+
/**
34+
* Throw [ExcessiveColumnsException] if any excessive columns were found in the original dataframe
35+
*/
36+
Fail
37+
}
2038

2139
/**
2240
* Holds data context for [fill] operation

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,14 +926,16 @@ class Modify : TestBase() {
926926
@Test
927927
fun customConverters() {
928928
// SampleStart
929-
class IntClass(val value: Int)
929+
class MyType(val value: Int)
930930

931931
@DataSchema
932-
class IntSchema(val ints: IntClass)
932+
class MySchema(val a: MyType, val b: MyType, val c: Int)
933933

934-
val df = dataFrameOf("ints")(1, 2, 3)
935-
df.convertTo<IntSchema> {
936-
convert<Int>().with { IntClass(it) }
934+
val df = dataFrameOf("a", "b")(1, "2")
935+
df.convertTo<MySchema> {
936+
convert<Int>().with { MyType(it) } // used to convert `a` from Int to MyType
937+
parser { MyType(it.toInt()) } // used to convert `b` from String to MyType
938+
fill { c }.with { a.value + b.value } // used to compute missing column `c`
937939
}
938940
// SampleEnd
939941
}

docs/StardustDocs/topics/convertTo.md

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

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

66
```kotlin
7-
convertTo<T>()
7+
convertTo<Schema>(excessiveColumns = ExcessiveColumns.Keep)
88
```
99

10-
Any additional columns will be dropped.
11-
12-
You can provide custom converters and parsers:
10+
Customization DSL:
11+
* `convert` - how specific column types should be converted
12+
* `parser` - how to parse strings into custom types
13+
* `fill` - how to fill missing columns
1314
<!---FUN customConverters-->
1415

1516
```kotlin
16-
class IntClass(val value: Int)
17+
class MyType(val value: Int)
1718

1819
@DataSchema
19-
class IntSchema(val ints: IntClass)
20+
class MySchema(val a: MyType, val b: MyType, val c: Int)
2021

21-
val df = dataFrameOf("ints")(1, 2, 3)
22-
df.convertTo<IntSchema> {
23-
convert<Int>().with { IntClass(it) }
22+
val df = dataFrameOf("a", "b")(1, "2")
23+
df.convertTo<MySchema> {
24+
convert<Int>().with { MyType(it) } // used to convert `a` from Int to MyType
25+
parser { MyType(it.toInt()) } // used to convert `b` from String to MyType
26+
fill { c }.with { a.value + b.value } // used to compute missing column `c`
2427
}
2528
```
2629

0 commit comments

Comments
 (0)