dataframe icon indicating copy to clipboard operation
dataframe copied to clipboard

`Map<String, Any?>.toDataRow()` function?

Open Jolanrensen opened this issue 1 year ago • 1 comments

DataFrame has the function pair: Map<String, Iterable<Any?>>.toDataFrame() and AnyFrame.toMap(): Map<String, List<Any?>>

But for DataRow there's just: AnyRow.toMap(): Map<String, Any?>.

Especially in notebooks it might be helpful to be able to convert a simple Map to a DataRow to get generated accessors. It might also make notations like https://github.com/Kotlin/dataframe/issues/710 possible (so to construct a DataFrame from rows instead of from columns, albeit at a performance cost).

Jolanrensen avatar Jun 04 '24 12:06 Jolanrensen

Additionally, we should allow adding unfolding features like maxDepth to unpack properties. Plus, for large maps, it might be preferable to make a KeyValueDataFrame instead of a DataRow.

Something like this? maybe something more efficient tho

inline fun <reified T> Map<String, T>.toDataRow(
    vararg props: KProperty<*>,
    maxDepth: Int = 0,
): DataRow<T> =
    toDataRow {
        properties(roots = props, maxDepth = maxDepth)
    }

inline fun <reified T> Map<String, T>.toDataRow(
    noinline body: CreateDataFrameDsl<T>.() -> Unit = { properties() },
): DataRow<T> =
    values.toDataFrame(body)
        .add(keys.toColumn("key"))
        .pivot("key").values()

Jolanrensen avatar Sep 27 '24 15:09 Jolanrensen

Hey, i would like to work on this issue

CarloMariaProietti avatar Sep 05 '25 14:09 CarloMariaProietti

Reading the issue, i understand that the returned DataRow should be part of a DataFrame which contains only that row. Is it correct?

CarloMariaProietti avatar Sep 06 '25 07:09 CarloMariaProietti

@CarloMariaProietti Hi! Thanks for picking this up :)

Yes, that's one way to tackle it, I'll check out the PR to see how it works

Jolanrensen avatar Sep 08 '25 11:09 Jolanrensen