65 lines
1.6 KiB
Markdown
Vendored
65 lines
1.6 KiB
Markdown
Vendored
[//]: # (title: cast)
|
|
<!---IMPORT org.jetbrains.kotlinx.dataframe.samples.api.Modify-->
|
|
|
|
Changes the type argument of the [`DataFrame`](DataFrame.md) instance without changing its contents.
|
|
|
|
```kotlin
|
|
cast<T>(verify = false)
|
|
```
|
|
|
|
Related operations: [](adjustSchema.md)
|
|
|
|
**Parameters:**
|
|
* `verify: Boolean = false` —
|
|
when `true`, the function throws an exception if the [`DataFrame`](DataFrame.md) instance doesn't match the given schema.
|
|
Otherwise, it just changes the format type without actual data checks.
|
|
|
|
Use this operation to change the formal type of a [`DataFrame`](DataFrame.md) instance
|
|
to match the expected schema and enable generated [extension properties](extensionPropertiesApi.md) for it.
|
|
|
|
```kotlin
|
|
@DataSchema
|
|
interface Person {
|
|
val age: Int
|
|
val name: String
|
|
}
|
|
|
|
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.
|
|
|
|

|
|
|
|
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-->
|