Skip to content

Commit d81af82

Browse files
Automated commit of generated code
1 parent 1e98ec9 commit d81af82

File tree

6 files changed

+1071
-39
lines changed

6 files changed

+1071
-39
lines changed

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

Lines changed: 188 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.jetbrains.kotlinx.dataframe.Selector
1515
import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload
1616
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
1717
import org.jetbrains.kotlinx.dataframe.annotations.Refine
18+
import org.jetbrains.kotlinx.dataframe.api.add
1819
import org.jetbrains.kotlinx.dataframe.columns.BaseColumn
1920
import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
2021
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
@@ -26,58 +27,64 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.resolveSingle
2627
import org.jetbrains.kotlinx.dataframe.util.DEPRECATED_ACCESS_API
2728
import kotlin.reflect.KProperty
2829

29-
/*
30-
* `add` operation adds new columns to DataFrame.
31-
*/
32-
3330
// region Add existing columns
3431

3532
/**
36-
* Creates new [DataFrame] with given columns added to the end of original [DataFrame.columns] list.
33+
* Adds new [columns] to the end of this [DataFrame] (at the top level).
3734
*
38-
* Original [DataFrame] is not modified.
35+
* Returns a new [DataFrame] with the new [columns] appended to the original list of [DataFrame.columns].
3936
*
40-
* @param columns columns to add
41-
* @throws [DuplicateColumnNamesException] if columns in expected result have repeated names
42-
* @throws [UnequalColumnSizesException] if columns in expected result have different sizes
43-
* @return new [DataFrame] with added columns
37+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
38+
*
39+
* @param columns columns to add.
40+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
41+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
42+
* @return new [DataFrame] with added columns.
4443
*/
4544
public fun <T> DataFrame<T>.add(vararg columns: AnyBaseCol): DataFrame<T> = addAll(columns.asIterable())
4645

4746
/**
48-
* Creates new [DataFrame] with given columns added to the end of original [DataFrame.columns] list.
47+
* Adds new [columns] to the end of this [DataFrame] (at the top level).
48+
*
49+
* Returns a new [DataFrame] with the new [columns] appended to the original list of [DataFrame.columns].
4950
*
50-
* Original [DataFrame] is not modified.
51+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
5152
*
52-
* @param columns columns to add
53-
* @throws [DuplicateColumnNamesException] if columns in expected result have repeated names
54-
* @throws [UnequalColumnSizesException] if columns in expected result have different sizes
55-
* @return new [DataFrame] with added columns
53+
* @param columns columns to add.
54+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
55+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
56+
* @return new [DataFrame] with added columns.
5657
*/
5758
public fun <T> DataFrame<T>.addAll(columns: Iterable<AnyBaseCol>): DataFrame<T> =
5859
dataFrameOf(columns() + columns).cast()
5960

6061
/**
61-
* Creates new [DataFrame] with all columns from given [dataFrames] added to the end of original [DataFrame.columns] list.
62+
* Adds all columns from the given [dataFrames] to the end of this [DataFrame] (at the top level).
6263
*
63-
* Original [DataFrame] is not modified.
64+
* Returns a new [DataFrame] with the columns from the specified
65+
* [dataFrames] appended to the original list of [DataFrame.columns].
6466
*
65-
* @param dataFrames dataFrames to get columns from
66-
* @throws [DuplicateColumnNamesException] if columns in expected result have repeated names
67-
* @throws [UnequalColumnSizesException] if columns in expected result have different sizes
68-
* @return new [DataFrame] with added columns
67+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
68+
*
69+
* @param dataFrames dataFrames to get columns from.
70+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
71+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
72+
* @return new [DataFrame] with added columns.
6973
*/
7074
public fun <T> DataFrame<T>.add(vararg dataFrames: AnyFrame): DataFrame<T> = addAll(dataFrames.asIterable())
7175

7276
/**
73-
* Creates new [DataFrame] with all columns from given [dataFrames] added to the end of original [DataFrame.columns] list.
77+
* Adds all columns from the given [dataFrames] to the end of this [DataFrame] (at the top level).
78+
*
79+
* Returns a new [DataFrame] with the columns from the specified
80+
* [dataFrames] appended to the original list of [DataFrame.columns].
7481
*
75-
* Original [DataFrame] is not modified.
82+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
7683
*
77-
* @param dataFrames dataFrames to get columns from
78-
* @throws [DuplicateColumnNamesException] if columns in expected result have repeated names
79-
* @throws [UnequalColumnSizesException] if columns in expected result have different sizes
80-
* @return new [DataFrame] with added columns
84+
* @param dataFrames dataFrames to get columns from.
85+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
86+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
87+
* @return new [DataFrame] with added columns.
8188
*/
8289
@JvmName("addAllFrames")
8390
public fun <T> DataFrame<T>.addAll(dataFrames: Iterable<AnyFrame>): DataFrame<T> =
@@ -115,14 +122,44 @@ public interface AddDataRow<out T> : DataRow<T> {
115122
public typealias AddExpression<T, R> = Selector<AddDataRow<T>, R>
116123

117124
/**
118-
* Creates new column using row [expression] and adds it to the end of [DataFrame]
125+
* Creates a new column using an [AddExpression] and
126+
* adds a new column to the end of this [DataFrame] (at the top level).
127+
*
128+
* With an [AddExpression][org.jetbrains.kotlinx.dataframe.api.AddExpression], you define the value that each row in the new column should have.
129+
* This can be based on values from the same row in the original [DataFrame][org.jetbrains.kotlinx.dataframe.DataFrame].
130+
*
131+
* You can also use functions like [prev][org.jetbrains.kotlinx.dataframe.api.prev] and [next][org.jetbrains.kotlinx.dataframe.api.next] to access other rows, and combine them with
132+
* [newValue][org.jetbrains.kotlinx.dataframe.api.AddDataRow.newValue] to reference values already computed in the new column.
133+
* For example, use `prev().newValue()` to access the new column value from the previous row.
134+
*
135+
* Returns a new [DataFrame] with the new column appended to the original list of [DataFrame.columns].
136+
*
137+
* ## Example
138+
*
139+
* ```kotlin
140+
* // Add a new column "sum" that contains the sum of values from the "firstValue"
141+
* // and "secondValue" columns for each row.
142+
* val dfWithSum = df.add("sum") { firstValue + secondValue }
143+
*
144+
* // Add a "fibonacci" column with the Fibonacci sequence:
145+
* // for the first two rows, the value is 1;
146+
* // for subsequent rows, it's the sum of the two previous Fibonacci values.
147+
* val dfWithFibonacci = df.add("fibonacci") {
148+
* if (index() < 2) 1
149+
* else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
150+
* }
151+
* ```
119152
*
120-
* Original [DataFrame] is not modified.
153+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
154+
*
155+
* @param name name for a new column.
156+
* If it is empty, a unique column name will be generated.
157+
* Otherwise, it should be unique for original [DataFrame].
158+
* @param infer a value of [Infer] that specifies how to compute column [type][BaseColumn.type] for a new column.
159+
* Defaults to [Infer.Nulls].
160+
* @param expression [AddExpression] that computes column value for every [DataRow] of a new column.
161+
* @return new [DataFrame] with added column.
121162
*
122-
* @param name name for a new column. If it is empty, a unique column name will be generated. Otherwise, it should be unique for original [DataFrame].
123-
* @param infer a value of [Infer] that specifies how to compute column [type][BaseColumn.type] for a new column
124-
* @param expression [AddExpression] that computes column value for every [DataRow]
125-
* @return new [DataFrame] with added column
126163
* @throws DuplicateColumnNamesException if [DataFrame] already contains a column with given [name]
127164
*/
128165
@Refine
@@ -149,6 +186,40 @@ public inline fun <reified R, T> DataFrame<T>.add(
149186
noinline expression: AddExpression<T, R>,
150187
): DataFrame<T> = add(column.path(), infer, expression)
151188

189+
/**
190+
* Creates a new column using [AddExpression] and inserts it at the specified [ColumnPath].
191+
*
192+
* With an [AddExpression][org.jetbrains.kotlinx.dataframe.api.AddExpression], you define the value that each row in the new column should have.
193+
* This can be based on values from the same row in the original [DataFrame][org.jetbrains.kotlinx.dataframe.DataFrame].
194+
*
195+
* You can also use functions like [prev][org.jetbrains.kotlinx.dataframe.api.prev] and [next][org.jetbrains.kotlinx.dataframe.api.next] to access other rows, and combine them with
196+
* [newValue][org.jetbrains.kotlinx.dataframe.api.AddDataRow.newValue] to reference values already computed in the new column.
197+
* For example, use `prev().newValue()` to access the new column value from the previous row.
198+
*
199+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
200+
*
201+
* Returns a new [DataFrame] with the new column inserted at the given [path].
202+
* If the specified path is partially or fully missing — that is, if any segment of the path
203+
* does not correspond to an existing column or column group — all missing parts will be created automatically.
204+
*
205+
* ## Example
206+
*
207+
* ```kotlin
208+
* // Add a new column "sum" inside the "info" column group (which will be created if it doesn't exist).
209+
* // The column contains the sum of values from the "firstValue" and "secondValue" columns for each row.
210+
* val dfWithSum = df.add(pathOf("info", "sum")) { firstValue + secondValue }
211+
* ```
212+
*
213+
* @param path Target [ColumnPath] for the new column.
214+
* If it points to a nested location,
215+
* intermediate columns will be created if necessary.
216+
* @param infer A value of [Infer] that specifies how to compute the column [type][BaseColumn.type] for the new column.
217+
* Defaults to [Infer.Nulls].
218+
* @param expression An [AddExpression] that computes the column value for every [DataRow] of the new column.
219+
* @return A new [DataFrame] with the added column.
220+
*
221+
* @throws DuplicateColumnNamesException If the [DataFrame] already contains a column at the specified [path].
222+
*/
152223
public inline fun <reified R, T> DataFrame<T>.add(
153224
path: ColumnPath,
154225
infer: Infer = Infer.Nulls,
@@ -163,6 +234,10 @@ public inline fun <reified R, T> DataFrame<T>.add(
163234

164235
// region Create and add several columns
165236

237+
/**
238+
* Receiver that is used by the [add] and [mapToFrame]
239+
* for adding new columns and column groups based on [DataFrame] columns and row values.
240+
*/
166241
public class AddDsl<T>(
167242
@PublishedApi internal val df: DataFrame<T>,
168243
) : ColumnsContainer<T> by df,
@@ -253,6 +328,43 @@ public class AddDsl<T>(
253328
public infix fun AddGroup<T>.into(column: AnyColumnGroupAccessor): Unit = into(column.name())
254329
}
255330

331+
/**
332+
* Creates new columns using the [AddDsl] builder.
333+
*
334+
* An [AddDsl] allows to add multiple new columns and column groups to a [DataFrame]
335+
* using concise syntax based on `from`, `into` operations and [AddExpression]s.
336+
*
337+
* Returns a new [DataFrame] with the newly added columns.
338+
*
339+
* ## Example
340+
*
341+
* ```kotlin
342+
* val dfWithAdded = df.add {
343+
* // Add new column "yearOfBirth" computed as 2021 minus value in "age" column
344+
* "yearOfBirth" from { 2021 - age }
345+
*
346+
* // Add column "is adult" with result of age > 18
347+
* age > 18 into "is adult"
348+
*
349+
* // Add new column "role" using expression
350+
* expr { if ( department == "IT") "developer" else "analyst" } into "role"
351+
*
352+
* // Add column group "details"
353+
* group("details") {
354+
* // Add column "last name length" with length of lastName
355+
* name.lastName.length() into "last name length"
356+
*
357+
* // Add column "full name" by combining firstName and lastName
358+
* "full name" from { name.firstName + " " + name.lastName }
359+
* }
360+
* }
361+
* ```
362+
*
363+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
364+
*
365+
* @param body An [AddDsl] expression used to define new columns and column groups.
366+
* @return A new [DataFrame] with the added columns.
367+
*/
256368
@Refine
257369
@Interpretable("AddWithDsl")
258370
public fun <T> DataFrame<T>.add(body: AddDsl<T>.() -> Unit): DataFrame<T> {
@@ -261,6 +373,48 @@ public fun <T> DataFrame<T>.add(body: AddDsl<T>.() -> Unit): DataFrame<T> {
261373
return dataFrameOf(this@add.columns() + dsl.columns).cast()
262374
}
263375

376+
/**
377+
* Creates a new column using [AddExpression] and
378+
* adds a new column to the end of each group (i.e., [DataFrame]s) of this [GroupBy] (at the top level).
379+
*
380+
* With an [AddExpression][org.jetbrains.kotlinx.dataframe.api.AddExpression], you define the value that each row in the new column should have.
381+
* This can be based on values from the same row in the original [DataFrame][org.jetbrains.kotlinx.dataframe.DataFrame].
382+
*
383+
* You can also use functions like [prev][org.jetbrains.kotlinx.dataframe.api.prev] and [next][org.jetbrains.kotlinx.dataframe.api.next] to access other rows, and combine them with
384+
* [newValue][org.jetbrains.kotlinx.dataframe.api.AddDataRow.newValue] to reference values already computed in the new column.
385+
* For example, use `prev().newValue()` to access the new column value from the previous row.
386+
*
387+
* Returns a new [GroupBy] with the new column
388+
* appended to each group [DataFrame] to the original list of [DataFrame.columns].
389+
*
390+
* ## Example
391+
*
392+
* ```kotlin
393+
* // Add a new column "sum" that contains the sum of values from the "firstValue"
394+
* // and "secondValue" columns for each row.
395+
* val gbWithSum = gb.add("sum") { firstValue + secondValue }
396+
*
397+
* // Add a "fibonacci" column with the Fibonacci sequence:
398+
* // for the first two rows, the value is 1;
399+
* // for subsequent rows, it's the sum of the two previous Fibonacci values.
400+
* val gbWithFibonacci = gb.add("fibonacci") {
401+
* if (index() < 2) 1
402+
* else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
403+
* }
404+
* ```
405+
*
406+
* For more information: [See `add` on the documentation website.](https://kotlin.github.io/dataframe/add.html).
407+
*
408+
* @param name name for a new column.
409+
* If it is empty, a unique column name will be generated.
410+
* Otherwise, it should be unique for original group [DataFrame]s.
411+
* @param infer a value of [Infer] that specifies how to compute column [type][BaseColumn.type] for a new column.
412+
* Defaults to [Infer.Nulls].
413+
* @param expression [AddExpression] that computes column value for every [DataRow] of a new column.
414+
* @return new [GroupBy] with added column.
415+
*
416+
* @throws DuplicateColumnNamesException if group [DataFrame]s already contains a column with given [name].
417+
*/
264418
@Refine
265419
@Interpretable("GroupByAdd")
266420
public inline fun <reified R, T, G> GroupBy<T, G>.add(

0 commit comments

Comments
 (0)