Skip to content

Commit 215b43b

Browse files
authored
Merge pull request #1489 from CarloMariaProietti/move_to_inside_group
Move to(Start) keeping inside group
2 parents 46b6e3f + ee54c88 commit 215b43b

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-0
lines changed

core/api/core.api

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,10 +3424,14 @@ public final class org/jetbrains/kotlinx/dataframe/api/MoveKt {
34243424
public static final fun move (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;
34253425
public static final fun move (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;
34263426
public static final fun moveTo (Lorg/jetbrains/kotlinx/dataframe/DataFrame;ILkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3427+
public static final fun moveTo (Lorg/jetbrains/kotlinx/dataframe/DataFrame;IZLkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3428+
public static final fun moveTo (Lorg/jetbrains/kotlinx/dataframe/DataFrame;IZ[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34273429
public static final fun moveTo (Lorg/jetbrains/kotlinx/dataframe/DataFrame;I[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34283430
public static final fun moveTo (Lorg/jetbrains/kotlinx/dataframe/DataFrame;I[Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34293431
public static final fun moveTo (Lorg/jetbrains/kotlinx/dataframe/DataFrame;I[Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34303432
public static final fun moveToEnd (Lorg/jetbrains/kotlinx/dataframe/DataFrame;Lkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3433+
public static final fun moveToEnd (Lorg/jetbrains/kotlinx/dataframe/DataFrame;ZLkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3434+
public static final fun moveToEnd (Lorg/jetbrains/kotlinx/dataframe/DataFrame;Z[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34313435
public static final fun moveToEnd (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34323436
public static final fun moveToEnd (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34333437
public static final fun moveToEnd (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
@@ -3440,14 +3444,19 @@ public final class org/jetbrains/kotlinx/dataframe/api/MoveKt {
34403444
public static final fun moveToRight (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34413445
public static final fun moveToRight (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34423446
public static final fun moveToStart (Lorg/jetbrains/kotlinx/dataframe/DataFrame;Lkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3447+
public static final fun moveToStart (Lorg/jetbrains/kotlinx/dataframe/DataFrame;ZLkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3448+
public static final fun moveToStart (Lorg/jetbrains/kotlinx/dataframe/DataFrame;Z[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34433449
public static final fun moveToStart (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34443450
public static final fun moveToStart (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lkotlin/reflect/KProperty;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34453451
public static final fun moveToStart (Lorg/jetbrains/kotlinx/dataframe/DataFrame;[Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34463452
public static final fun to (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;I)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3453+
public static final fun to (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;IZ)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34473454
public static final fun toEnd (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3455+
public static final fun toEnd (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Z)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34483456
public static final fun toLeft (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34493457
public static final fun toRight (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34503458
public static final fun toStart (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3459+
public static final fun toStart (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Z)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34513460
public static final fun toTop (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Lkotlin/jvm/functions/Function2;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34523461
public static synthetic fun toTop$default (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
34533462
public static final fun under (Lorg/jetbrains/kotlinx/dataframe/api/MoveClause;Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
@@ -5593,6 +5602,11 @@ public final class org/jetbrains/kotlinx/dataframe/exceptions/ColumnTypeMismatch
55935602
public final fun getColumn ()Lorg/jetbrains/kotlinx/dataframe/DataColumn;
55945603
}
55955604

5605+
public final class org/jetbrains/kotlinx/dataframe/exceptions/ColumnsWithDifferentParentException : java/lang/IllegalArgumentException, org/jetbrains/kotlinx/dataframe/exceptions/DataFrameError {
5606+
public fun <init> (Ljava/lang/String;)V
5607+
public fun getMessage ()Ljava/lang/String;
5608+
}
5609+
55965610
public abstract interface class org/jetbrains/kotlinx/dataframe/exceptions/DataFrameError {
55975611
public abstract fun getMessage ()Ljava/lang/String;
55985612
}

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

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
2121
import org.jetbrains.kotlinx.dataframe.impl.api.afterOrBefore
2222
import org.jetbrains.kotlinx.dataframe.impl.api.moveImpl
2323
import org.jetbrains.kotlinx.dataframe.impl.api.moveTo
24+
import org.jetbrains.kotlinx.dataframe.impl.api.moveToImpl
2425
import org.jetbrains.kotlinx.dataframe.ncol
2526
import org.jetbrains.kotlinx.dataframe.util.DEPRECATED_ACCESS_API
2627
import org.jetbrains.kotlinx.dataframe.util.MOVE_TO_LEFT
@@ -220,6 +221,53 @@ public fun <T> DataFrame<T>.moveTo(newColumnIndex: Int, vararg columns: AnyColum
220221
public fun <T> DataFrame<T>.moveTo(newColumnIndex: Int, vararg columns: KProperty<*>): DataFrame<T> =
221222
moveTo(newColumnIndex) { columns.toColumnSet() }
222223

224+
/**
225+
* Moves the specified [columns\] to a new position specified
226+
* by [newColumnIndex]. If [insideGroup] is true selected columns
227+
* will be moved remaining within their [ColumnGroup],
228+
* else they will be moved to the top level.
229+
*
230+
* @include [CommonMoveToDocs]
231+
* @include [SelectingColumns.Dsl] {@include [SetMoveToOperationArg]}
232+
* ### Examples:
233+
* ```kotlin
234+
* df.moveTo(0, true) { length and age }
235+
* df.moveTo(2, false) { cols(1..5) }
236+
* ```
237+
* @param [newColumnIndex] The index specifying the position in the [DataFrame] columns
238+
* where the selected columns will be moved.
239+
* @param [insideGroup] If true, selected columns will be moved remaining inside their group,
240+
* else they will be moved to the top level.
241+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to move.
242+
*/
243+
public fun <T> DataFrame<T>.moveTo(
244+
newColumnIndex: Int,
245+
insideGroup: Boolean,
246+
columns: ColumnsSelector<T, *>,
247+
): DataFrame<T> = move(columns).to(newColumnIndex, insideGroup)
248+
249+
/**
250+
* Moves the specified [columns\] to a new position specified
251+
* by [columnIndex]. If [insideGroup] is true selected columns
252+
* will be moved remaining within their [ColumnGroup],
253+
* else they will be moved to the top level.
254+
*
255+
* @include [CommonMoveToDocs]
256+
* @include [SelectingColumns.ColumnNames] {@include [SetMoveToOperationArg]}
257+
* ### Examples:
258+
* ```kotlin
259+
* df.moveTo(0, true) { length and age }
260+
* df.moveTo(2, false) { cols(1..5) }
261+
* ```
262+
* @param [newColumnIndex] The index specifying the position in the [DataFrame] columns
263+
* where the selected columns will be moved.
264+
* @param [insideGroup] If true, selected columns will be moved remaining inside their group,
265+
* else they will be moved to the top level.
266+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to move.
267+
*/
268+
public fun <T> DataFrame<T>.moveTo(newColumnIndex: Int, insideGroup: Boolean, vararg columns: String): DataFrame<T> =
269+
moveTo(newColumnIndex, insideGroup) { columns.toColumnSet() }
270+
223271
// endregion
224272

225273
// region moveToStart
@@ -265,6 +313,18 @@ public fun <T> DataFrame<T>.moveToLeft(columns: ColumnsSelector<T, *>): DataFram
265313
@Interpretable("MoveToStart1")
266314
public fun <T> DataFrame<T>.moveToStart(columns: ColumnsSelector<T, *>): DataFrame<T> = move(columns).toStart()
267315

316+
/**
317+
* @include [CommonMoveToStartDocs]
318+
* @include [SelectingColumns.Dsl.WithExample] {@include [SetMoveToStartOperationArg]}
319+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to move.
320+
* @param [insideGroup] If true, selected columns will be moved to the start remaining inside their group,
321+
* else they will be moved to the start of the top level.
322+
*/
323+
@Refine
324+
@Interpretable("MoveToStart1")
325+
public fun <T> DataFrame<T>.moveToStart(insideGroup: Boolean, columns: ColumnsSelector<T, *>): DataFrame<T> =
326+
move(columns).toStart(insideGroup)
327+
268328
@Deprecated(MOVE_TO_LEFT, ReplaceWith(MOVE_TO_LEFT_REPLACE), DeprecationLevel.ERROR)
269329
public fun <T> DataFrame<T>.moveToLeft(vararg columns: String): DataFrame<T> = moveToStart { columns.toColumnSet() }
270330

@@ -275,6 +335,16 @@ public fun <T> DataFrame<T>.moveToLeft(vararg columns: String): DataFrame<T> = m
275335
*/
276336
public fun <T> DataFrame<T>.moveToStart(vararg columns: String): DataFrame<T> = moveToStart { columns.toColumnSet() }
277337

338+
/**
339+
* @include [CommonMoveToStartDocs]
340+
* @include [SelectingColumns.ColumnNames.WithExample] {@include [SetMoveToStartOperationArg]}
341+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to move.
342+
* @param [insideGroup] If true, selected columns will be moved to the start remaining inside their group,
343+
* else they will be moved to the start of the top level.
344+
*/
345+
public fun <T> DataFrame<T>.moveToStart(insideGroup: Boolean, vararg columns: String): DataFrame<T> =
346+
moveToStart(insideGroup) { columns.toColumnSet() }
347+
278348
@Deprecated(MOVE_TO_LEFT, ReplaceWith(MOVE_TO_LEFT_REPLACE), DeprecationLevel.ERROR)
279349
@AccessApiOverload
280350
public fun <T> DataFrame<T>.moveToLeft(vararg columns: AnyColumnReference): DataFrame<T> =
@@ -340,6 +410,18 @@ public fun <T> DataFrame<T>.moveToRight(columns: ColumnsSelector<T, *>): DataFra
340410
@Interpretable("MoveToEnd1")
341411
public fun <T> DataFrame<T>.moveToEnd(columns: ColumnsSelector<T, *>): DataFrame<T> = move(columns).toEnd()
342412

413+
/**
414+
* @include [CommonMoveToEndDocs]
415+
* @include [SelectingColumns.Dsl.WithExample] {@include [SetMoveToEndOperationArg]}
416+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to move.
417+
* @param [insideGroup] If true, selected columns will be moved to the end remaining inside their group,
418+
* else they will be moved to the end of the top level.
419+
*/
420+
@Refine
421+
@Interpretable("MoveToEnd1")
422+
public fun <T> DataFrame<T>.moveToEnd(insideGroup: Boolean, columns: ColumnsSelector<T, *>): DataFrame<T> =
423+
move(columns).toEnd(insideGroup)
424+
343425
@Deprecated(MOVE_TO_RIGHT, ReplaceWith(MOVE_TO_RIGHT_REPLACE), DeprecationLevel.ERROR)
344426
public fun <T> DataFrame<T>.moveToRight(vararg columns: String): DataFrame<T> = moveToEnd { columns.toColumnSet() }
345427

@@ -350,6 +432,16 @@ public fun <T> DataFrame<T>.moveToRight(vararg columns: String): DataFrame<T> =
350432
*/
351433
public fun <T> DataFrame<T>.moveToEnd(vararg columns: String): DataFrame<T> = moveToEnd { columns.toColumnSet() }
352434

435+
/**
436+
* @include [CommonMoveToEndDocs]
437+
* @include [SelectingColumns.ColumnNames.WithExample] {@include [SetMoveToEndOperationArg]}
438+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to move.
439+
* @param [insideGroup] If true, selected columns will be moved to the end remaining inside their group,
440+
* else they will be moved to the end of the top level.
441+
*/
442+
public fun <T> DataFrame<T>.moveToEnd(insideGroup: Boolean, vararg columns: String): DataFrame<T> =
443+
moveToEnd(insideGroup) { columns.toColumnSet() }
444+
353445
@Deprecated(MOVE_TO_RIGHT, ReplaceWith(MOVE_TO_RIGHT_REPLACE), DeprecationLevel.ERROR)
354446
@AccessApiOverload
355447
public fun <T> DataFrame<T>.moveToRight(vararg columns: AnyColumnReference): DataFrame<T> =
@@ -549,6 +641,32 @@ public fun <T, C> MoveClause<T, C>.under(
549641
@Interpretable("MoveTo")
550642
public fun <T, C> MoveClause<T, C>.to(columnIndex: Int): DataFrame<T> = moveTo(columnIndex)
551643

644+
/**
645+
* Moves columns, previously selected with [move] to a new position specified
646+
* by [columnIndex]. If [insideGroup] is true, selected columns will be moved remaining within their [ColumnGroup],
647+
* else they will be moved to the top level.
648+
*
649+
* Returns a new [DataFrame] with updated columns structure.
650+
*
651+
* For more information: {@include [DocumentationUrls.Move]}
652+
*
653+
* ### Examples:
654+
* ```kotlin
655+
* df.move { age and weight }.to(0, true)
656+
* df.move("age", "weight").to(2, false)
657+
* ```
658+
*
659+
* @param [columnIndex] The index specifying the position in the [ColumnGroup] columns
660+
* where the selected columns will be moved.
661+
*
662+
* @param [insideGroup] If true, selected columns will be moved remaining inside their group,
663+
* else they will be moved to the top level.
664+
*/
665+
@Refine
666+
@Interpretable("MoveTo")
667+
public fun <T, C> MoveClause<T, C>.to(columnIndex: Int, insideGroup: Boolean): DataFrame<T> =
668+
moveToImpl(columnIndex, insideGroup)
669+
552670
/**
553671
* Moves columns, previously selected with [move] to the top-level within the [DataFrame].
554672
* Moved columns name can be specified via special ColumnSelectionDsl.
@@ -703,6 +821,28 @@ public fun <T, C> MoveClause<T, C>.toLeft(): DataFrame<T> = to(0)
703821
@Interpretable("MoveToStart0")
704822
public fun <T, C> MoveClause<T, C>.toStart(): DataFrame<T> = to(0)
705823

824+
/**
825+
* If insideGroup is true, moves columns previously selected with [move] to the start of their [ColumnGroup].
826+
* Else, selected columns will be moved to the start of their [DataFrame] (to the top-level).
827+
*
828+
* Returns a new [DataFrame] with updated columns.
829+
*
830+
* For more information: {@include [DocumentationUrls.Move]}
831+
*
832+
* ### Examples:
833+
* ```kotlin
834+
* df.move { age and weight }.toStart(true)
835+
* df.move { colsOf<String>() }.toStart(true)
836+
* df.move("age", "weight").toStart(false)
837+
* ```
838+
*
839+
* @param [insideGroup] If true, selected columns will be moved to the start remaining inside their group,
840+
* else they will be moved to the start on top level.
841+
*/
842+
@Refine
843+
@Interpretable("MoveToStart0")
844+
public fun <T, C> MoveClause<T, C>.toStart(insideGroup: Boolean): DataFrame<T> = to(0, insideGroup)
845+
706846
@Deprecated(TO_RIGHT, ReplaceWith(TO_RIGHT_REPLACE), DeprecationLevel.ERROR)
707847
public fun <T, C> MoveClause<T, C>.toRight(): DataFrame<T> = to(df.ncol)
708848

@@ -724,6 +864,28 @@ public fun <T, C> MoveClause<T, C>.toRight(): DataFrame<T> = to(df.ncol)
724864
@Interpretable("MoveToEnd0")
725865
public fun <T, C> MoveClause<T, C>.toEnd(): DataFrame<T> = to(df.ncol)
726866

867+
/**
868+
* If insideGroup is true, moves columns previously selected with [move] to the end of their [ColumnGroup].
869+
* Else, selected columns will be moved to the end of their [DataFrame] (to the top-level).
870+
*
871+
* Returns a new [DataFrame] with updated columns.
872+
*
873+
* For more information: {@include [DocumentationUrls.Move]}
874+
*
875+
* ### Examples:
876+
* ```kotlin
877+
* df.move { age and weight }.toEnd(true)
878+
* df.move { colsOf<String>() }.toEnd(true)
879+
* df.move("age", "weight").toEnd(false)
880+
* ```
881+
*
882+
* @param [insideGroup] If true, selected columns will be moved to the end remaining inside their group,
883+
* else they will be moved to the end on top level.
884+
*/
885+
@Refine
886+
@Interpretable("MoveToEnd0")
887+
public fun <T, C> MoveClause<T, C>.toEnd(insideGroup: Boolean): DataFrame<T> = to(df.ncol, insideGroup)
888+
727889
/**
728890
* An intermediate class used in the [move] operation.
729891
*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.jetbrains.kotlinx.dataframe.exceptions
2+
3+
public class ColumnsWithDifferentParentException(message: String) :
4+
IllegalArgumentException(),
5+
DataFrameError {
6+
7+
override val message: String = message
8+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ import org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl
88
import org.jetbrains.kotlinx.dataframe.api.MoveClause
99
import org.jetbrains.kotlinx.dataframe.api.after
1010
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
11+
import org.jetbrains.kotlinx.dataframe.api.asDataFrame
1112
import org.jetbrains.kotlinx.dataframe.api.cast
1213
import org.jetbrains.kotlinx.dataframe.api.getColumn
1314
import org.jetbrains.kotlinx.dataframe.api.getColumnGroup
1415
import org.jetbrains.kotlinx.dataframe.api.getColumnWithPath
16+
import org.jetbrains.kotlinx.dataframe.api.getColumns
1517
import org.jetbrains.kotlinx.dataframe.api.move
18+
import org.jetbrains.kotlinx.dataframe.api.replace
19+
import org.jetbrains.kotlinx.dataframe.api.to
1620
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
21+
import org.jetbrains.kotlinx.dataframe.api.with
1722
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
1823
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
1924
import org.jetbrains.kotlinx.dataframe.columns.UnresolvedColumnsPolicy
2025
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
26+
import org.jetbrains.kotlinx.dataframe.exceptions.ColumnsWithDifferentParentException
2127
import org.jetbrains.kotlinx.dataframe.impl.DataFrameReceiver
2228
import org.jetbrains.kotlinx.dataframe.impl.asList
2329
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnWithPath
2430
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.ColumnPosition
2531
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.getOrPut
2632
import org.jetbrains.kotlinx.dataframe.path
33+
import kotlin.collections.first
2734

2835
internal fun <T, C> MoveClause<T, C>.afterOrBefore(column: ColumnSelector<T, *>, isAfter: Boolean): DataFrame<T> {
2936
val removeResult = df.removeImpl(columns = columns)
@@ -124,3 +131,33 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int): DataFrame<T> {
124131
}
125132
return newColumnList.toDataFrame().cast()
126133
}
134+
135+
internal fun <T, C> MoveClause<T, C>.moveToImpl(columnIndex: Int, insideGroup: Boolean): DataFrame<T> {
136+
if (!insideGroup) {
137+
return moveTo(columnIndex)
138+
}
139+
140+
val columnsToMove = df.getColumns(columns)
141+
142+
// check if columns to move have the same parent
143+
val columnsToMoveParents = columnsToMove.map { it.path.dropLast() }
144+
val parentOfFirst = columnsToMoveParents.first()
145+
if (columnsToMoveParents.any { it != parentOfFirst }) {
146+
throw ColumnsWithDifferentParentException(
147+
"Cannot move columns to an index remaining inside group if they have different parent",
148+
)
149+
}
150+
151+
// if columns will be moved to top level or columns to move are at top level
152+
if (parentOfFirst.isEmpty()) {
153+
return moveTo(columnIndex)
154+
}
155+
156+
// replace the level where columns to move are with a new one where columns are moved
157+
val columnsToMoveNames = columnsToMove.map { it.name() }
158+
return df.replace { parentOfFirst.asColumnGroup() }.with {
159+
it.asDataFrame()
160+
.move { columnsToMoveNames.toColumnSet() }.to(columnIndex)
161+
.asColumnGroup(it.name())
162+
}
163+
}

0 commit comments

Comments
 (0)