Skip to content

Commit 692eba8

Browse files
Merge pull request #1104 from Kotlin/rename_fix
rename impl fix
2 parents 436c442 + f594b9d commit 692eba8

File tree

3 files changed

+32
-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

+32
-3
lines changed

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ 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+
val selectedColumns = df.getColumnsWithPaths(columns)
19+
20+
if (selectedColumns.size != newNames.size) {
21+
throw IllegalArgumentException(
22+
"Selected column count (${selectedColumns.size}) must match new " +
23+
"column names count (${newNames.size}).",
24+
)
25+
}
26+
27+
// associate old column names with new ones
28+
val oldToNew = newNames.mapIndexed { index, newName ->
29+
selectedColumns[index].path to newName
30+
}.toMap()
31+
32+
return renameImpl { column ->
33+
oldToNew[column.path] ?: throw IllegalArgumentException("Unexpected column: $column")
34+
}
2035
}
2136

2237
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)