Skip to content
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

rename impl fix #1104

Merged
merged 2 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.tree.map
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++] }
val selectedColumns = df.getColumnsWithPaths(columns)

if (selectedColumns.size != newNames.size) {
throw IllegalArgumentException(
"Selected column count (${selectedColumns.size}) must match new " +
"column names count (${newNames.size}).",
)
}

// associate old column names with new ones
val oldToNew = newNames.mapIndexed { index, newName ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to "associate", there's a function named exactly that ;p maybe you can use it here too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need indices here, but there's no associateIndexed unfortunately xD.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withIndex().associate {} works, but then again, this works too :p

selectedColumns[index].path to newName
}.toMap()

return renameImpl { column ->
oldToNew[column.path] ?: throw IllegalArgumentException("Unexpected column: $column")
}
}

internal fun <T, C> RenameClause<T, C>.renameImpl(transform: (ColumnWithPath<C>) -> String): DataFrame<T> {
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/rename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class RenameTests : ColumnsSelectionDslTests() {
simpleDf.rename { all() }.into("a_renamed", "b_renamed", "c_renamed") shouldBe renamedDf
}

@Test
fun `test rename with String to String pairs`() {
val renamedDf = dataFrameOf("a_renamed", "b_renamed", "c_renamed")(
1, 2, 3,
4, 5, 6,
)

simpleDf.rename(
"c" to "c_renamed",
"a" to "a_renamed",
"b" to "b_renamed",
) shouldBe renamedDf
}

@Test
fun `partial grouped rename`() {
val renamedDf = dataFrameOf("a_renamed", "b", "c")(
Expand Down
2 changes: 1 addition & 1 deletion plugins/kotlin-dataframe/testData/box/rename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun box(): String {
val df1 = df.rename { nested.d }.into("newName")
df1.nested.newName

val df2 = df.rename { nested.d and nested }.into("first", "second")
val df2 = df.rename { nested and nested.d }.into("first", "second")
df2.first.second
return "OK"
}