Skip to content

Commit 0040315

Browse files
new logic + new excpeption class
1 parent 906a838 commit 0040315

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ public fun <T, C> MoveClause<T, C>.toStart(): DataFrame<T> = to(0)
736736
* @param [insideGroup] If true, selected columns will be moved to the start remaining inside their group,
737737
* else they will be moved to the start on top level.
738738
*/
739+
@Refine
740+
@Interpretable("MoveToStart0")
739741
public fun <T, C> MoveClause<T, C>.toStart(insideGroup: Boolean): DataFrame<T> = to(0, insideGroup)
740742

741743
@Deprecated(TO_RIGHT, ReplaceWith(TO_RIGHT_REPLACE), DeprecationLevel.ERROR)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.jetbrains.kotlinx.dataframe.exceptions
2+
3+
public class ColumnsWithDifferentParentException() :
4+
IllegalArgumentException(),
5+
DataFrameError {
6+
7+
override val message: String
8+
get() = "Cannot move columns to an index remaining inside group if they have different parent"
9+
}

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import org.jetbrains.kotlinx.dataframe.api.getColumnGroup
1515
import org.jetbrains.kotlinx.dataframe.api.getColumnWithPath
1616
import org.jetbrains.kotlinx.dataframe.api.getColumns
1717
import org.jetbrains.kotlinx.dataframe.api.move
18+
import org.jetbrains.kotlinx.dataframe.api.replace
1819
import org.jetbrains.kotlinx.dataframe.api.to
1920
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
2021
import org.jetbrains.kotlinx.dataframe.api.toPath
22+
import org.jetbrains.kotlinx.dataframe.api.with
2123
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
2224
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
2325
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
@@ -143,7 +145,7 @@ internal fun <T, C> MoveClause<T, C>.moveToImpl(columnIndex: Int, insideGroup: B
143145
val parentOfFirst = columnsToMoveParents.first()
144146
if (columnsToMoveParents.any { it != parentOfFirst }) {
145147
throw IllegalArgumentException(
146-
"Cannot move columns with different parent to an index",
148+
"Cannot move columns to an index remaining inside group if they have different parent",
147149
)
148150
}
149151

@@ -152,22 +154,11 @@ internal fun <T, C> MoveClause<T, C>.moveToImpl(columnIndex: Int, insideGroup: B
152154
return moveTo(columnIndex)
153155
}
154156

155-
// logic: remove columns to move and their siblings (from this point, sons), apply them moveTo, reinsert them
156-
val parentPath = df[parentOfFirst].path
157-
val sons = df[parentOfFirst].asColumnGroup()
158-
// remove sons
159-
val sonsWithFullPaths = sons.columns().map { parentPath + it.path }
160-
val intermediateDf = df.removeImpl { sonsWithFullPaths.toColumnSet() }
161-
// move sons and reinsert them
162-
val columnsToMoveWithReducedPath = columnsToMove.map { it.path.last(it.path.size - parentPath.size).toPath() }
163-
val sonsHaveBeenMoved = sons.asDataFrame().move {
164-
columnsToMoveWithReducedPath.toColumnSet()
165-
}.to(columnIndex).columns()
166-
val sonsToInsert = sonsHaveBeenMoved.map { ColumnToInsert(parentPath + it.path, it) }
167-
val secondIntermediateDf = intermediateDf.df.insertImpl(sonsToInsert)
168-
// nested level is good but order of top level is changed -> need to fix it
169-
val rootOfColumnsToMove = df[listOf(parentPath.first()).toPath()]
170-
val indexOfRootOfColumnsToMove = df.columns().indexOf(rootOfColumnsToMove)
171-
val finalDf = secondIntermediateDf.move { listOf(parentPath.first()).toPath() }.to(indexOfRootOfColumnsToMove)
172-
return finalDf
157+
// replace the level where columns to move are with a new one where columns are moved
158+
val columnsToMoveNames = columnsToMove.map { it.name() }
159+
return df.replace { parentOfFirst.asColumnGroup() }.with {
160+
it.asDataFrame()
161+
.move { columnsToMoveNames.toColumnSet() }.to(columnIndex)
162+
.asColumnGroup(it.name())
163+
}
173164
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,6 @@ class MoveTests {
310310
fun `should throw when moving columns of different groups`() {
311311
shouldThrow<IllegalArgumentException> {
312312
grouped.move { "a"["b"] and "b"["c"] }.to(0, true)
313-
}.message shouldBe "Cannot move columns with different parent to an index"
313+
}.message shouldBe "Cannot move columns to an index remaining inside group if they have different parent"
314314
}
315315
}

0 commit comments

Comments
 (0)