requireColumn
Resolves column in this DataFrame and checks that its runtime type is a subtype of C. Throws if the column can't be resolved or if its type doesn't match.
From the compiler plugin perspective, a new column will appear in the compile-time schema as a result of this operation.
The aim here is to help incrementally migrate workflows to extension properties API.
We recommend considering declaring a DataSchema and use cast or convertTo if you end up with more than a few requireColumn calls.
String API Example:
val repos = DataFrame
.readCsv("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains_repositories.csv")
repos
.filter { "stargazers_count"<Int>() > 100 }
.sortByDesc("stargazers_count")
.select("full_name", "stargazers_count")Notice how stargazers_count String is repeated three times. We can use requireColumn to add information about this column to the compile-time schema.
Refactored example:
val repos = DataFrame
.readCsv("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains_repositories.csv")
.requireColumn { "stargazers_count"<Int>() }
repos
.filter { stargazers_count > 100 }
.sortByDesc { stargazers_count }
.select { "full_name" and stargazers_count }This way code becomes a bit more robust. For example, after .rename { stargazers_count }.into("stars") usages of the stargazers_count property become compile time errors that are easy to spot and update to stars.
val repos = DataFrame
.readCsv("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains_repositories.csv")
.requireColumn { "stargazers_count"<Int>() }
.rename { stargazers_count }.into("stars")
repos
.filter { stars > 100 }
.sortByDesc { stars }
.select { "full_name" and stars }requireColumn can be used to change the type of a known column:
val df = dataFrameOf("name" to columnOf<Any>("Joe"))
val typed = df.requireColumn { name.cast<String>() }
typed.filter { name.length > 2 }For more information: See requireColumn on the documentation website.