Skip to content

Commit dea4884

Browse files
rename impl fix
1 parent 5e9df02 commit dea4884

File tree

3 files changed

+22
-3
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api
  • plugins/kotlin-dataframe/testData/box

3 files changed

+22
-3
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/rename.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.tree.map
1515
import org.jetbrains.kotlinx.dataframe.kind
1616

1717
internal fun <T, C> RenameClause<T, C>.renameImpl(newNames: Array<out String>): DataFrame<T> {
18-
var i = 0
19-
return renameImpl { newNames[i++] }
18+
// associate old column names with new ones
19+
val selectedColumns = df.getColumnsWithPaths(columns)
20+
val oldToNew = newNames.mapIndexed { index, newName ->
21+
selectedColumns[index].name to newName
22+
}.toMap()
23+
24+
return renameImpl { column -> oldToNew[column.name] ?: column.name }
2025
}
2126

2227
internal fun <T, C> RenameClause<T, C>.renameImpl(transform: (ColumnWithPath<C>) -> String): DataFrame<T> {

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/rename.kt

+14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ class RenameTests : ColumnsSelectionDslTests() {
3131
simpleDf.rename { all() }.into("a_renamed", "b_renamed", "c_renamed") shouldBe renamedDf
3232
}
3333

34+
@Test
35+
fun `test rename with String to String pairs`() {
36+
val renamedDf = dataFrameOf("a_renamed", "b_renamed", "c_renamed")(
37+
1, 2, 3,
38+
4, 5, 6,
39+
)
40+
41+
simpleDf.rename(
42+
"c" to "c_renamed",
43+
"a" to "a_renamed",
44+
"b" to "b_renamed",
45+
) shouldBe renamedDf
46+
}
47+
3448
@Test
3549
fun `partial grouped rename`() {
3650
val renamedDf = dataFrameOf("a_renamed", "b", "c")(

plugins/kotlin-dataframe/testData/box/rename.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fun box(): String {
1212
val df1 = df.rename { nested.d }.into("newName")
1313
df1.nested.newName
1414

15-
val df2 = df.rename { nested.d and nested }.into("first", "second")
15+
val df2 = df.rename { nested and nested.d }.into("first", "second")
1616
df2.first.second
1717
return "OK"
1818
}

0 commit comments

Comments
 (0)