File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
core/generated-sources/src
main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
test/kotlin/org/jetbrains/kotlinx/dataframe/api Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -15,8 +15,23 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.tree.map
1515import org.jetbrains.kotlinx.dataframe.kind
1616
1717internal 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
2237internal fun <T , C > RenameClause <T , C >.renameImpl (transform : (ColumnWithPath <C >) -> String ): DataFrame <T > {
Original file line number Diff line number Diff 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" )(
You can’t perform that action at this time.
0 commit comments