Gabriele Cacchioni

03/26/2023, 4:16 PM
What is the best way of converting a data class into a dataframe? It would be great to have something similar to
interface Schema {
    val columnOne: IntArray
    val columnTwo: DoubleArray

data class ConcreteDf(
    override val columnOne: IntArray,
    override val columnTwo: DoubleArray
) : Schema {
    // override equals and hashcode
and then be able to do
val myDf = ConcreteDf(columnOne = intArrayOf(1,2), columnTwo = doubleArrayOf(3.0, 4.0, 5.0))
myDf.toDataFrame() // <--- this would be nice

Ilya Muradyan

03/26/2023, 9:11 PM
You can transform the iterable of data class objects into dataframe — it will take column names out of class properties names. But API you suggest is a bit unclear — at least because it requires creating an extension function on Any type. Another thing is what types to consider as columns. And what to do if the target object implements Iterable itself? If you really need it, you can easily write such an extension yourself using reflection solving all these design problems in a way that matches your particular case.

Jolan Rensen [JB]

04/07/2023, 10:56 AM
btw, you can also annotate data classes with
, works the same as with interfaces 🙂