@@ -11,6 +11,8 @@ import org.jetbrains.kotlinx.dataframe.annotations.Refine
1111import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
1212import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
1313import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
14+ import org.jetbrains.kotlinx.dataframe.impl.api.afterImpl
15+ import org.jetbrains.kotlinx.dataframe.impl.api.beforeImpl
1416import org.jetbrains.kotlinx.dataframe.impl.api.insertImpl
1517import org.jetbrains.kotlinx.dataframe.impl.columnName
1618import org.jetbrains.kotlinx.dataframe.impl.removeAt
@@ -446,10 +448,59 @@ public fun <T> InsertClause<T>.after(columnPath: ColumnPath): DataFrame<T> {
446448 return df.insertImpl(dstPath, column).move { dstPath }.after { columnPath }
447449}
448450
449- internal fun <T > InsertClause<T>.afterImpl (columnPath : ColumnPath ): DataFrame <T > {
450- val dstPath = ColumnPath (columnPath.removeAt(columnPath.size - 1 ) + column.name())
451- return df.insertImpl(dstPath, column).move { dstPath }.after { columnPath }
452- }
451+ // endregion
452+
453+ // region before
454+
455+ /* *
456+ * Inserts the new column previously specified with [insert]
457+ * at the position immediately before the selected [column] (on the same level).
458+ *
459+ * For more information: [See `insert` on the documentation website.](https://kotlin.github.io/dataframe/insert.html)
460+ *
461+ * See [Grammar][InsertDocs.Grammar] for more details.
462+ *
463+ * See also: [SelectingColumns.Dsl].
464+ *
465+ * ### Examples:
466+ * ```kotlin
467+ * // Insert a new column "age" before the "name" column
468+ * df.insert(age).before { name }
469+ *
470+ * // Insert a new column "sum" before the nested "min" column (inside the "stats" column group)
471+ * val dfWithSum = df.insert("sum") { a + b }.before { stats.min }
472+ * ```
473+ *
474+ * @param [column] The [ColumnSelector] used to choose an existing column in this [DataFrame],
475+ * before which the new column will be inserted.
476+ * @return A new [DataFrame] with the inserted column placed before the selected column.
477+ */
478+ @Refine
479+ @Interpretable(" InsertBefore0" )
480+ public fun <T > InsertClause<T>.before (column : ColumnSelector <T , * >): DataFrame <T > = beforeImpl(df.getColumnPath(column))
481+
482+ /* *
483+ * Inserts the new column previously specified with [insert]
484+ * at the position immediately before the column with the given [name][column].
485+ *
486+ * For more information: [See `insert` on the documentation website.](https://kotlin.github.io/dataframe/insert.html)
487+ *
488+ * See [Grammar][InsertDocs.Grammar] for more details.
489+ *
490+ * See also: [SelectingColumns.ColumnNames].
491+ *
492+ * ### Example
493+ * ```kotlin
494+ * // Insert a new column "age" before the "name" column
495+ * df.insert(age).before("name")
496+ * ```
497+ *
498+ * @param [column] The [String] name of the column in this [DataFrame]
499+ * before which the new column will be inserted.
500+ * @return A new [DataFrame] with the inserted column placed before the specified column.
501+ */
502+ public fun <T > InsertClause<T>.before (column : String ): DataFrame <T > =
503+ df.add(this .column).move(this .column).before(column)
453504
454505// endregion
455506
0 commit comments