File tree 3 files changed +32
-3
lines changed
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 Original file line number Diff line number Diff line change @@ -15,8 +15,23 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.tree.map
15
15
import org.jetbrains.kotlinx.dataframe.kind
16
16
17
17
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
+ }
20
35
}
21
36
22
37
internal 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() {
31
31
simpleDf.rename { all() }.into(" a_renamed" , " b_renamed" , " c_renamed" ) shouldBe renamedDf
32
32
}
33
33
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
+
34
48
@Test
35
49
fun `partial grouped rename` () {
36
50
val renamedDf = dataFrameOf(" a_renamed" , " b" , " c" )(
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ fun box(): String {
12
12
val df1 = df.rename { nested.d }.into(" newName" )
13
13
df1.nested.newName
14
14
15
- val df2 = df.rename { nested.d and nested }.into(" first" , " second" )
15
+ val df2 = df.rename { nested and nested.d }.into(" first" , " second" )
16
16
df2.first.second
17
17
return " OK"
18
18
}
You can’t perform that action at this time.
0 commit comments