Yogeshvu
08/14/2024, 7:42 PMYogeshvu
08/14/2024, 7:42 PMYogeshvu
08/14/2024, 7:43 PMdata class Person(val name: String, val age: Int)
val persons = listOf(Person("Alice", 15), Person("Bob", 20), Person("Charlie", 22))
val df = persons.toDataFrame()
println("The Person dataframe is")
println(df)
val dfAny = (persons as List<Any>).toDataFrame()
println("Any Data Frame is ")
println(dfAny)
Yogeshvu
08/14/2024, 7:43 PMJolan Rensen [JB]
08/15/2024, 7:01 PMtoDataFrame()
works by retrieving the class of Person by reified type. If you make it a List<Any> that won't work. However, I see you're working in a notebook right? :)
Try and execute the cell and in a next cell access dfAny
Under the hood it should infer the types based on the data inside. (If not, try df.unbox()
Jolan Rensen [JB]
08/15/2024, 7:03 PMDISPLAY()
instead of print (or stare your DataFrame at the last line of the cell), the Dataframe renders nicely :)Yogeshvu
08/15/2024, 7:14 PMJolan Rensen [JB]
08/16/2024, 9:48 AMList<Any>.toDataFrame()
indeed doesn't work.
.toDataFrame()
is shorthand for .toDataFrame { properties() }
(which is sorta mentioned here). What this means is that it will try, with reflection, to build a dataframe with the properties of, in this case, Any
. Any
has no properties, so the dataframe will be empty.
I made a little explanation notebook here https://gist.github.com/Jolanrensen/34b5b1572e28b9192697c71f93c29bc2Yogeshvu
08/16/2024, 1:58 PMList<*>
would that work here?Jolan Rensen [JB]
08/16/2024, 2:00 PMList<out T>
, List<*>
is exactly the same as List<Any?>
, so no 🙂Nikita Klimenko [JB]
08/19/2024, 1:19 PMtoDataFrame(KClass)
. Then it will be possible to convert dynamically, for example like this: list.toDataFrame(list[0]::class)
public inline fun <reified T> Iterable<T>.toDataFrame(noinline body: CreateDataFrameDsl<T>.() -> Unit): DataFrame<T> =
createDataFrameImpl(T::class, body)
Yogeshvu
08/19/2024, 4:08 PMNikita Klimenko [JB]
08/19/2024, 4:47 PMinternal class A(val b: Int)
internal fun main() {
val a: List<Any?> = listOf(A(1))
val df = a.toDataFrame {
val props = (a[0]!!::class.createType().classifier as? KClass<*>)!!.memberProperties.toTypedArray()
properties(*props)
}
df.print()
}
===
b
0 1
Let me know if it works for you or API needs to be different in the PRYogeshvu
08/19/2024, 6:26 PMval df = data.toDataFrame {
val props = (data[0]!!::class.createType().classifier as? KClass<*>)!!.memberProperties.toTypedArray()
properties(*props)
}
` thanks!