Skip to content

Commit 4a0c0bb

Browse files
add move before
1 parent a34302f commit 4a0c0bb

File tree

1 file changed

+58
-0
lines changed
  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api

1 file changed

+58
-0
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,64 @@ internal fun <T> InsertClause<T>.afterImpl(columnPath: ColumnPath): DataFrame<T>
345345

346346
// endregion
347347

348+
// region before
349+
350+
/**
351+
* Inserts the new column previously specified with [insert]
352+
* at the position immediately before the selected [column] (on the same level).
353+
*
354+
* For more information: {@include [DocumentationUrls.Insert]}
355+
*
356+
* See [Grammar][InsertDocs.Grammar] for more details.
357+
*
358+
* See also: [SelectingColumns.Dsl].
359+
*
360+
* ### Examples:
361+
* ```kotlin
362+
* // Insert a new column "age" before the "name" column
363+
* df.insert(age).before { name }
364+
*
365+
* // Insert a new column "sum" before the nested "min" column (inside the "stats" column group)
366+
* val dfWithSum = df.insert("sum") { a + b }.before { stats.min }
367+
* ```
368+
*
369+
* @param [column] The [ColumnSelector] used to choose an existing column in this [DataFrame],
370+
* before which the new column will be inserted.
371+
* @return A new [DataFrame] with the inserted column placed before the selected column.
372+
*/
373+
@Refine
374+
@Interpretable("InsertBefore0")
375+
public fun <T> InsertClause<T>.before(column: ColumnSelector<T, *>): DataFrame<T> = beforeImpl(df.getColumnPath(column))
376+
377+
/**
378+
* Inserts the new column previously specified with [insert]
379+
* at the position immediately before the column with the given [name][column].
380+
*
381+
* For more information: {@include [DocumentationUrls.Insert]}
382+
*
383+
* See [Grammar][InsertDocs.Grammar] for more details.
384+
*
385+
* See also: [SelectingColumns.ColumnNames].
386+
*
387+
* ### Example
388+
* ```kotlin
389+
* // Insert a new column "age" before the "name" column
390+
* df.insert(age).before("name")
391+
* ```
392+
*
393+
* @param [column] The [String] name of the column in this [DataFrame]
394+
* before which the new column will be inserted.
395+
* @return A new [DataFrame] with the inserted column placed before the specified column.
396+
*/
397+
public fun <T> InsertClause<T>.before(column: String): DataFrame<T> = df.add(this.column).move(this.column).before(column)
398+
399+
internal fun <T> InsertClause<T>.beforeImpl(columnPath: ColumnPath): DataFrame<T> {
400+
val dstPath = ColumnPath(columnPath.removeAt(columnPath.size - 1) + column.name())
401+
return df.insertImpl(dstPath, column).move { dstPath }.before { columnPath }
402+
}
403+
404+
// endregion
405+
348406
// region at
349407

350408
/**

0 commit comments

Comments
 (0)