-
Notifications
You must be signed in to change notification settings - Fork 76
Fixed rename behavior #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7d6c91e
147ee90
d447799
53c6531
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package org.jetbrains.kotlinx.dataframe.impl.api | ||
|
|
||
| import org.jetbrains.kotlinx.dataframe.DataFrame | ||
| import org.jetbrains.kotlinx.dataframe.api.RenameClause | ||
| import org.jetbrains.kotlinx.dataframe.api.cast | ||
| import org.jetbrains.kotlinx.dataframe.api.getColumnsWithPaths | ||
| import org.jetbrains.kotlinx.dataframe.api.insert | ||
| import org.jetbrains.kotlinx.dataframe.api.under | ||
| import org.jetbrains.kotlinx.dataframe.columns.ColumnKind | ||
| import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath | ||
| import org.jetbrains.kotlinx.dataframe.impl.columns.tree.allChildrenNotNull | ||
| import org.jetbrains.kotlinx.dataframe.impl.columns.tree.collectTree | ||
| import org.jetbrains.kotlinx.dataframe.kind | ||
|
|
||
| internal fun <T, C> RenameClause<T, C>.renameImpl(newNames: Array<out String>): DataFrame<T> { | ||
| var i = 0 | ||
| return renameImpl { newNames[i++] } | ||
| } | ||
|
|
||
| internal fun <T, C> RenameClause<T, C>.renameImpl(transform: (ColumnWithPath<C>) -> String): DataFrame<T> { | ||
| val selectedColumns = df.getColumnsWithPaths(columns) | ||
| val tree = df.getColumnsWithPaths { all().rec() }.collectTree() | ||
|
|
||
| // perform rename in nodes | ||
| tree.allChildrenNotNull().forEach { node -> | ||
| val column = selectedColumns.find { it.data == node.data } ?: return@forEach | ||
| val newName = transform(column) | ||
| node.name = newName | ||
| } | ||
|
|
||
| // build up a new DataFrame using the modified names | ||
| var newDf = DataFrame.empty(df.rowsCount()).cast<T>() | ||
Jolanrensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tree.allChildrenNotNull().forEach { node -> | ||
| val path = node.pathFromRoot().dropLast(1) | ||
| val col = node.data.rename(node.name) | ||
|
|
||
| when (col.kind) { | ||
| ColumnKind.Value, ColumnKind.Frame -> | ||
| newDf = newDf.insert(col).under(path) | ||
|
|
||
| ColumnKind.Group -> Unit | ||
| } | ||
| } | ||
|
|
||
| return newDf | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package org.jetbrains.kotlinx.dataframe.impl.api | ||
|
|
||
| import org.jetbrains.kotlinx.dataframe.DataFrame | ||
| import org.jetbrains.kotlinx.dataframe.api.RenameClause | ||
| import org.jetbrains.kotlinx.dataframe.api.cast | ||
| import org.jetbrains.kotlinx.dataframe.api.getColumnsWithPaths | ||
| import org.jetbrains.kotlinx.dataframe.api.insert | ||
| import org.jetbrains.kotlinx.dataframe.api.under | ||
| import org.jetbrains.kotlinx.dataframe.columns.ColumnKind | ||
| import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath | ||
| import org.jetbrains.kotlinx.dataframe.impl.columns.tree.allChildrenNotNull | ||
| import org.jetbrains.kotlinx.dataframe.impl.columns.tree.collectTree | ||
| import org.jetbrains.kotlinx.dataframe.kind | ||
|
|
||
| internal fun <T, C> RenameClause<T, C>.renameImpl(newNames: Array<out String>): DataFrame<T> { | ||
| var i = 0 | ||
| return renameImpl { newNames[i++] } | ||
| } | ||
|
|
||
| internal fun <T, C> RenameClause<T, C>.renameImpl(transform: (ColumnWithPath<C>) -> String): DataFrame<T> { | ||
| val selectedColumns = df.getColumnsWithPaths(columns) | ||
| val tree = df.getColumnsWithPaths { all().rec() }.collectTree() | ||
|
|
||
| // perform rename in nodes | ||
| tree.allChildrenNotNull().forEach { node -> | ||
| val column = selectedColumns.find { it.data == node.data } ?: return@forEach | ||
|
||
| val newName = transform(column) | ||
| node.name = newName | ||
| } | ||
|
|
||
| // build up a new DataFrame using the modified names | ||
| var newDf = DataFrame.empty(df.rowsCount()).cast<T>() | ||
| tree.allChildrenNotNull().forEach { node -> | ||
|
||
| val path = node.pathFromRoot().dropLast(1) | ||
| val col = node.data.rename(node.name) | ||
|
|
||
| when (col.kind) { | ||
| ColumnKind.Value, ColumnKind.Frame -> | ||
| newDf = newDf.insert(col).under(path) | ||
|
|
||
| ColumnKind.Group -> Unit | ||
| } | ||
| } | ||
|
|
||
| return newDf | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.