What is the best way of converting a data class in...
# datascience
g
What is the best way of converting a data class into a dataframe? It would be great to have something similar to
Copy code
@DataSchema
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
Copy code
val myDf = ConcreteDf(columnOne = intArrayOf(1,2), columnTwo = doubleArrayOf(3.0, 4.0, 5.0))
myDf.toDataFrame() // <--- this would be nice
i
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.
j
btw, you can also annotate data classes with
@DataSchema
, works the same as with interfaces 🙂