-
Notifications
You must be signed in to change notification settings - Fork 75
Move to(Start) keeping inside group #1489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 13 commits
8f73fb8
b581a92
cebda41
ccf9ba2
e5137ae
6fc8e98
cbf53e7
26db065
2aeb956
eed89bc
e212f36
a19d523
4627c60
8b4ca9e
98cac42
3c0a96c
b07bad9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,6 +537,27 @@ public fun <T, C> MoveClause<T, C>.under( | |
@Interpretable("MoveTo") | ||
public fun <T, C> MoveClause<T, C>.to(columnIndex: Int): DataFrame<T> = moveTo(columnIndex) | ||
|
||
/** | ||
* Moves columns, previously selected with [move] to a new position specified | ||
* by [columnIndex] within the column group (remaining inside the group). | ||
* | ||
* Returns a new [DataFrame] with updated columns structure. | ||
* | ||
* For more information: {@include [DocumentationUrls.Move]} | ||
* | ||
* ### Examples: | ||
* ```kotlin | ||
* df.move { age and weight }.to(0) | ||
* df.move("age", "weight").to(2) | ||
|
||
* ``` | ||
* | ||
* @param [columnIndex] The index specifying the position in the [ColumnGroup] columns | ||
* * where the selected columns will be moved. | ||
|
||
*/ | ||
@Refine | ||
@Interpretable("MoveTo") | ||
public fun <T, C> MoveClause<T, C>.to(columnIndex: Int, insideGroup: Boolean): DataFrame<T> = moveTo(columnIndex, insideGroup) | ||
|
||
/** | ||
* Moves columns, previously selected with [move] to the top-level within the [DataFrame]. | ||
* Moved columns name can be specified via special ColumnSelectionDsl. | ||
|
@@ -691,6 +712,24 @@ public fun <T, C> MoveClause<T, C>.toLeft(): DataFrame<T> = to(0) | |
@Interpretable("MoveToStart0") | ||
public fun <T, C> MoveClause<T, C>.toStart(): DataFrame<T> = to(0) | ||
|
||
/** | ||
* Moves columns, previously selected with [move] to the start of their [ColumnGroup] (remaining inside the group). | ||
|
||
* | ||
* Returns a new [DataFrame] with updated columns. | ||
* | ||
* For more information: {@include [DocumentationUrls.Move]} | ||
* | ||
* ### Examples: | ||
* ```kotlin | ||
* df.move { age and weight }.toStart() | ||
* df.move { colsOf<String>() }.toStart() | ||
* df.move("age", "weight").toStart() | ||
* ``` | ||
*/ | ||
Jolanrensen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Refine | ||
@Interpretable("MoveToStart0") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need to add compiler plugin support for the |
||
public fun <T, C> MoveClause<T, C>.toStart(insideGroup: Boolean): DataFrame<T> = to(0, insideGroup) | ||
|
||
@Deprecated(TO_RIGHT, ReplaceWith(TO_RIGHT_REPLACE), DeprecationLevel.ERROR) | ||
public fun <T, C> MoveClause<T, C>.toRight(): DataFrame<T> = to(df.ncol) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,14 @@ import org.jetbrains.kotlinx.dataframe.api.ColumnsSelectionDsl | |
import org.jetbrains.kotlinx.dataframe.api.MoveClause | ||
import org.jetbrains.kotlinx.dataframe.api.after | ||
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup | ||
import org.jetbrains.kotlinx.dataframe.api.before | ||
import org.jetbrains.kotlinx.dataframe.api.cast | ||
import org.jetbrains.kotlinx.dataframe.api.getColumn | ||
import org.jetbrains.kotlinx.dataframe.api.getColumnGroup | ||
import org.jetbrains.kotlinx.dataframe.api.getColumnWithPath | ||
import org.jetbrains.kotlinx.dataframe.api.getColumns | ||
import org.jetbrains.kotlinx.dataframe.api.move | ||
import org.jetbrains.kotlinx.dataframe.api.toColumnAccessor | ||
import org.jetbrains.kotlinx.dataframe.api.toDataFrame | ||
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath | ||
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath | ||
|
@@ -23,6 +26,7 @@ import org.jetbrains.kotlinx.dataframe.impl.asList | |
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnWithPath | ||
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.ColumnPosition | ||
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.getOrPut | ||
import org.jetbrains.kotlinx.dataframe.indices | ||
import org.jetbrains.kotlinx.dataframe.path | ||
|
||
internal fun <T, C> MoveClause<T, C>.afterOrBefore(column: ColumnSelector<T, *>, isAfter: Boolean): DataFrame<T> { | ||
|
@@ -124,3 +128,65 @@ internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int): DataFrame<T> { | |
} | ||
return newColumnList.toDataFrame().cast() | ||
} | ||
|
||
internal fun <T, C> MoveClause<T, C>.moveTo(columnIndex: Int, insideGroup: Boolean): DataFrame<T> { | ||
|
||
val columnsToMove = df.getColumns(columns) | ||
|
||
// check if columns to move have the same parent | ||
val columnsToMoveParents = columnsToMove.map { it.path.dropLast() } | ||
val parentOfFirst = columnsToMoveParents.first() | ||
if (columnsToMoveParents.any { it != parentOfFirst }) { | ||
throw IllegalArgumentException( | ||
"Cannot move columns with different parent to an index", | ||
) | ||
} | ||
|
||
// if columns will be moved to top level or columns to move are at top level | ||
if (!insideGroup || parentOfFirst.isEmpty()) { | ||
Jolanrensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return moveTo(columnIndex) | ||
} | ||
|
||
// columns are nested and will be eventually moved inside their own group | ||
// finding reference column | ||
val parentPath = df[parentOfFirst].path | ||
val referenceAndSiblings = df[parentOfFirst].asColumnGroup().columns() | ||
val referenceAndSiblingsPaths = referenceAndSiblings.map { parentPath + it.path } | ||
val reference = if (columnIndex >= referenceAndSiblingsPaths.size){ | ||
Jolanrensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
referenceAndSiblingsPaths.last() | ||
} else { | ||
referenceAndSiblingsPaths.get(columnIndex) | ||
Jolanrensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
// two cases : columns to move are before, or after, the reference | ||
val columnsBeforeReferenceToMove = mutableListOf<ColumnPath>() | ||
val columnsAfterReferenceToMove = mutableListOf<ColumnPath>() | ||
val columnsToMovePath = columnsToMove.map { it.path } | ||
var referenceWasIterated = false | ||
referenceAndSiblingsPaths.forEach { | ||
if (it.path.last() == reference.path.last()) | ||
referenceWasIterated = true | ||
else { | ||
if (columnsToMovePath.contains(it)){ | ||
if (referenceWasIterated) | ||
columnsAfterReferenceToMove.add(it) | ||
else | ||
columnsBeforeReferenceToMove.add(it) | ||
} | ||
} | ||
} | ||
|
||
// move columns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably easier to view the logic using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and lift |
||
if (columnsBeforeReferenceToMove.isNotEmpty() && columnsAfterReferenceToMove.isNotEmpty()) { | ||
val intermediateDf = df.move { columnsBeforeReferenceToMove.toColumnSet() }.after { reference } | ||
val newReference = columnsBeforeReferenceToMove.last() | ||
val finalDf = intermediateDf.move { columnsAfterReferenceToMove.toColumnSet() }.after { newReference } | ||
return finalDf | ||
} | ||
if (columnsBeforeReferenceToMove.isNotEmpty()) | ||
return df.move { columnsBeforeReferenceToMove.toColumnSet() }.after { reference } | ||
if (columnsAfterReferenceToMove.isNotEmpty()) | ||
return df.move { columnsAfterReferenceToMove.toColumnSet() }.before { reference } | ||
|
||
// if it is not needed to move any of the nested columns | ||
return df | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*if
insideGroup == true
that is