Skip to content

Commit 4eadb9f

Browse files
Automated commit of generated code
1 parent 179595e commit 4eadb9f

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import org.jetbrains.kotlinx.dataframe.annotations.Refine
1111
import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
1212
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
1313
import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
14+
import org.jetbrains.kotlinx.dataframe.impl.api.afterImpl
15+
import org.jetbrains.kotlinx.dataframe.impl.api.beforeImpl
1416
import org.jetbrains.kotlinx.dataframe.impl.api.insertImpl
1517
import org.jetbrains.kotlinx.dataframe.impl.columnName
1618
import 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

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/insert.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import org.jetbrains.kotlinx.dataframe.AnyBaseCol
44
import org.jetbrains.kotlinx.dataframe.AnyCol
55
import org.jetbrains.kotlinx.dataframe.DataColumn
66
import org.jetbrains.kotlinx.dataframe.DataFrame
7+
import org.jetbrains.kotlinx.dataframe.api.InsertClause
8+
import org.jetbrains.kotlinx.dataframe.api.after
9+
import org.jetbrains.kotlinx.dataframe.api.before
710
import org.jetbrains.kotlinx.dataframe.api.cast
11+
import org.jetbrains.kotlinx.dataframe.api.move
812
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
913
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
1014
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
1115
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.ReadonlyTreeNode
1216
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.ReferenceData
1317
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.getAncestor
1418
import org.jetbrains.kotlinx.dataframe.impl.columns.withDf
19+
import org.jetbrains.kotlinx.dataframe.impl.removeAt
1520

1621
internal data class ColumnToInsert(
1722
val insertionPath: ColumnPath,
@@ -148,3 +153,13 @@ internal fun <T> insertImpl(
148153

149154
return newColumns.toDataFrame().cast()
150155
}
156+
157+
internal fun <T> InsertClause<T>.afterImpl(columnPath: ColumnPath): DataFrame<T> {
158+
val dstPath = ColumnPath(columnPath.removeAt(columnPath.size - 1) + column.name())
159+
return df.insertImpl(dstPath, column).move { dstPath }.after { columnPath }
160+
}
161+
162+
internal fun <T> InsertClause<T>.beforeImpl(columnPath: ColumnPath): DataFrame<T> {
163+
val dstPath = ColumnPath(columnPath.removeAt(columnPath.size - 1) + column.name())
164+
return df.insertImpl(dstPath, column).move { dstPath }.before { columnPath }
165+
}

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTreeTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.jetbrains.kotlinx.dataframe.api.asDataFrame
2525
import org.jetbrains.kotlinx.dataframe.api.asFrame
2626
import org.jetbrains.kotlinx.dataframe.api.asGroupBy
2727
import org.jetbrains.kotlinx.dataframe.api.at
28+
import org.jetbrains.kotlinx.dataframe.api.before
2829
import org.jetbrains.kotlinx.dataframe.api.by
2930
import org.jetbrains.kotlinx.dataframe.api.cast
3031
import org.jetbrains.kotlinx.dataframe.api.colsOf
@@ -702,6 +703,22 @@ class DataFrameTreeTests : BaseTest() {
702703
typed2.insert(colName) { nameAndCity.name.reversed() }.after { nameAndCity.name }.check()
703704
}
704705

706+
@Test
707+
fun `insert column before`() {
708+
val colName = "reversed"
709+
710+
fun DataFrame<GroupedPerson>.check() {
711+
nameAndCity.columnsCount() shouldBe 3
712+
nameAndCity.columnNames() shouldBe listOf(
713+
typed2.nameAndCity.name.name(),
714+
colName,
715+
typed2.nameAndCity.city.name(),
716+
)
717+
}
718+
719+
typed2.insert(colName) { nameAndCity.name.reversed() }.before { nameAndCity.city }.check()
720+
}
721+
705722
@Test
706723
fun append() {
707724
val res = typed2.append(listOf("Bill", "San Francisco"), null, 66)

0 commit comments

Comments
 (0)