Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add castTo to help working with implicitly generated schemas in notebooks and plugin #747

Merged
merged 1 commit into from
Jun 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public inline fun <reified T> AnyFrame.cast(verify: Boolean = true): DataFrame<T
).cast()
else cast()

public inline fun <reified T> AnyFrame.castTo(
@Suppress("UNUSED_PARAMETER") df: DataFrame<T>,
verify: Boolean = true
): DataFrame<T> {
return cast<T>(verify = verify)
}

public fun <T> AnyRow.cast(): DataRow<T> = this as DataRow<T>

public inline fun <reified T> AnyRow.cast(verify: Boolean = true): DataRow<T> = df().cast<T>(verify)[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import org.jetbrains.kotlinx.dataframe.api.at
import org.jetbrains.kotlinx.dataframe.api.by
import org.jetbrains.kotlinx.dataframe.api.byName
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.castTo
import org.jetbrains.kotlinx.dataframe.api.colsOf
import org.jetbrains.kotlinx.dataframe.api.column
import org.jetbrains.kotlinx.dataframe.api.columnGroup
import org.jetbrains.kotlinx.dataframe.api.columnOf
import org.jetbrains.kotlinx.dataframe.api.concat
import org.jetbrains.kotlinx.dataframe.api.convert
import org.jetbrains.kotlinx.dataframe.api.convertTo
import org.jetbrains.kotlinx.dataframe.api.count
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.dataframe.api.default
import org.jetbrains.kotlinx.dataframe.api.dropNulls
Expand Down Expand Up @@ -100,6 +102,7 @@ import org.jetbrains.kotlinx.dataframe.explainer.PluginCallbackProxy
import org.jetbrains.kotlinx.dataframe.explainer.TransformDataFrameExpressions
import org.jetbrains.kotlinx.dataframe.impl.api.mapNotNullValues
import org.jetbrains.kotlinx.dataframe.indices
import org.jetbrains.kotlinx.dataframe.io.readJson
import org.jetbrains.kotlinx.dataframe.io.readJsonStr
import org.jetbrains.kotlinx.dataframe.io.renderToString
import org.jetbrains.kotlinx.dataframe.testResource
Expand Down Expand Up @@ -1421,4 +1424,33 @@ class Modify : TestBase() {
| 1 kotlin /kotlin 180
|""".trimMargin()
}

@DataSchema
interface ImplicitSchema {
val perf: Double
}

@Test
@Ignore
@Suppress("UNUSED_VARIABLE")
fun castToGenerateSchema() {
// SampleStart
val sample = DataFrame.readJson("sample.json")
// SampleEnd
}

@Test
@Suppress("KotlinConstantConditions")
fun castTo() {
val sample = dataFrameOf("perf")(10.0, 20.0, 12.0).cast<ImplicitSchema>()
val files = listOf<String>() // not intended to run
// SampleStart
for (file in files) {
// df here is expected to have the same structure as sample
val df = DataFrame.readJson(file).castTo(sample)
val count = df.count { perf > 10.0 }
println("$file: $count")
}
// SampleEnd
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions docs/StardustDocs/topics/cast.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,37 @@ df.cast<Person>()
```

To convert [`DataFrame`](DataFrame.md) columns to match given schema, use [`convertTo`](convertTo.md) operation.

**Reusing implicitly generated schema**

```kotlin
castTo<T>(df: DataFrame<T>)
```

In notebooks, dataframe types are implicitly generated.

![Implicitly generated schema](implicitlyGeneratedSchema.png)

This type can be referred to, but its name will change whenever you re-execute cells.
Here how you can do it in a more robust way:

<!---FUN castToGenerateSchema-->

```kotlin
val sample = DataFrame.readJson("sample.json")
```

<!---END-->

<!---FUN castTo-->

```kotlin
for (file in files) {
// df here is expected to have the same structure as sample
val df = DataFrame.readJson(file).castTo(sample)
val count = df.count { perf > 10.0 }
println("$file: $count")
}
```

<!---END-->
Loading