Skip to content

Commit 3d45cdd

Browse files
add kdocs
1 parent 2bf037a commit 3d45cdd

File tree

4 files changed

+184
-39
lines changed

4 files changed

+184
-39
lines changed

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

Lines changed: 174 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,77 @@ 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
2122
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
23+
import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls
2224
import org.jetbrains.kotlinx.dataframe.exceptions.DuplicateColumnNamesException
2325
import org.jetbrains.kotlinx.dataframe.exceptions.UnequalColumnSizesException
2426
import org.jetbrains.kotlinx.dataframe.impl.api.insertImpl
2527
import org.jetbrains.kotlinx.dataframe.impl.columns.resolveSingle
2628
import org.jetbrains.kotlinx.dataframe.util.DEPRECATED_ACCESS_API
2729
import kotlin.reflect.KProperty
2830

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

3533
/**
36-
* Creates new [DataFrame] with given columns added to the end of original [DataFrame.columns] list.
34+
* Adds new [columns] to the end of this [DataFrame] (at the top level).
35+
*
36+
* Returns a new [DataFrame] with the new [columns] appended to the original list of [DataFrame.columns].
3737
*
38-
* Original [DataFrame] is not modified.
38+
* For more information: {@include [DocumentationUrls.Add]}.
3939
*
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
40+
* @param columns columns to add.
41+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
42+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
43+
* @return new [DataFrame] with added columns.
4444
*/
4545
public fun <T> DataFrame<T>.add(vararg columns: AnyBaseCol): DataFrame<T> = addAll(columns.asIterable())
4646

4747
/**
48-
* Creates new [DataFrame] with given columns added to the end of original [DataFrame.columns] list.
48+
* Adds new [columns] to the end of this [DataFrame] (at the top level).
4949
*
50-
* Original [DataFrame] is not modified.
50+
* Returns a new [DataFrame] with the new [columns] appended to the original list of [DataFrame.columns].
5151
*
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
52+
* For more information: {@include [DocumentationUrls.Add]}.
53+
*
54+
* @param columns columns to add.
55+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
56+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
57+
* @return new [DataFrame] with added columns.
5658
*/
5759
public fun <T> DataFrame<T>.addAll(columns: Iterable<AnyBaseCol>): DataFrame<T> =
5860
dataFrameOf(columns() + columns).cast()
5961

6062
/**
61-
* Creates new [DataFrame] with all columns from given [dataFrames] added to the end of original [DataFrame.columns] list.
63+
* Adds all columns from the given [dataFrames] to the end of this [DataFrame] (at the top level).
64+
*
65+
* Returns a new [DataFrame] with the columns from the specified
66+
* [dataFrames] appended to the original list of [DataFrame.columns].
6267
*
63-
* Original [DataFrame] is not modified.
68+
* For more information: {@include [DocumentationUrls.Add]}.
6469
*
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
70+
* @param dataFrames dataFrames to get columns from.
71+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
72+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
73+
* @return new [DataFrame] with added columns.
6974
*/
7075
public fun <T> DataFrame<T>.add(vararg dataFrames: AnyFrame): DataFrame<T> = addAll(dataFrames.asIterable())
7176

7277
/**
73-
* Creates new [DataFrame] with all columns from given [dataFrames] added to the end of original [DataFrame.columns] list.
78+
* Adds all columns from the given [dataFrames] to the end of this [DataFrame] (at the top level).
79+
*
80+
* Returns a new [DataFrame] with the columns from the specified
81+
* [dataFrames] appended to the original list of [DataFrame.columns].
7482
*
75-
* Original [DataFrame] is not modified.
83+
* For more information: {@include [DocumentationUrls.Add]}.
7684
*
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
85+
* @param dataFrames dataFrames to get columns from.
86+
* @throws [DuplicateColumnNamesException] if columns in an expected result have repeated names.
87+
* @throws [UnequalColumnSizesException] if columns in an expected result have different sizes.
88+
* @return new [DataFrame] with added columns.
8189
*/
8290
@JvmName("addAllFrames")
8391
public fun <T> DataFrame<T>.addAll(dataFrames: Iterable<AnyFrame>): DataFrame<T> =
@@ -115,14 +123,41 @@ public interface AddDataRow<out T> : DataRow<T> {
115123
public typealias AddExpression<T, R> = Selector<AddDataRow<T>, R>
116124

117125
/**
118-
* Creates new column using row [expression] and adds it to the end of [DataFrame]
126+
* Creates a new column using [AddExpression] and
127+
* adds a new column to the end of this [DataFrame] (at the top level).
128+
*
129+
* An [AddExpression] allows to compute a value for each row in the new column
130+
* based on the values from that row in the original [DataFrame].
131+
* Also, you can use other methods such as [prev] and [next] to access other rows,
132+
* including [newValue][AddDataRow.newValue] to retrieve already computed values of this column
133+
* in previous rows.
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+
* @param name name for a new column.
154+
* If it is empty, a unique column name will be generated.
155+
* Otherwise, it should be unique for original [DataFrame].
156+
* @param infer a value of [Infer] that specifies how to compute column [type][BaseColumn.type] for a new column.
157+
* Defaults to [Infer.Nulls].
158+
* @param expression [AddExpression] that computes column value for every [DataRow] of a new column.
159+
* @return new [DataFrame] with added column.
121160
*
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
126161
* @throws DuplicateColumnNamesException if [DataFrame] already contains a column with given [name]
127162
*/
128163
@Refine
@@ -149,6 +184,36 @@ public inline fun <reified R, T> DataFrame<T>.add(
149184
noinline expression: AddExpression<T, R>,
150185
): DataFrame<T> = add(column.path(), infer, expression)
151186

187+
/**
188+
* Creates a new column using [AddExpression] and inserts it at the specified [ColumnPath].
189+
*
190+
* An [AddExpression] allows to compute a value for each row in the new column
191+
* based on the values from that row in the original [DataFrame].
192+
* Also, you can use other methods such as [prev] and [next] to access other rows,
193+
* including [newValue][AddDataRow.newValue] to retrieve already computed values of this column
194+
* in previous rows.
195+
*
196+
* Returns a new [DataFrame] with the new column inserted at the given [path].
197+
* {@include [org.jetbrains.kotlinx.dataframe.documentation.ColumnPathCreation]}
198+
*
199+
* ## Example
200+
*
201+
* ```kotlin
202+
* // Add a new column "sum" inside the "info" column group (which will be created if it doesn't exist).
203+
* // The column contains the sum of values from the "firstValue" and "secondValue" columns for each row.
204+
* val dfWithSum = df.add(pathOf("info", "sum")) { firstValue + secondValue }
205+
* ```
206+
*
207+
* @param path Target [ColumnPath] for the new column.
208+
* If it points to a nested location,
209+
* intermediate columns will be created if necessary.
210+
* @param infer A value of [Infer] that specifies how to compute the column [type][BaseColumn.type] for the new column.
211+
* Defaults to [Infer.Nulls].
212+
* @param expression An [AddExpression] that computes the column value for every [DataRow] of the new column.
213+
* @return A new [DataFrame] with the added column.
214+
*
215+
* @throws DuplicateColumnNamesException If the [DataFrame] already contains a column at the specified [path].
216+
*/
152217
public inline fun <reified R, T> DataFrame<T>.add(
153218
path: ColumnPath,
154219
infer: Infer = Infer.Nulls,
@@ -163,6 +228,10 @@ public inline fun <reified R, T> DataFrame<T>.add(
163228

164229
// region Create and add several columns
165230

231+
/**
232+
* Receiver that is used by the [add] and [mapToFrame]
233+
* for adding new columns and column groups based on [DataFrame] columns and row values.
234+
*/
166235
public class AddDsl<T>(
167236
@PublishedApi internal val df: DataFrame<T>,
168237
) : ColumnsContainer<T> by df,
@@ -253,6 +322,38 @@ public class AddDsl<T>(
253322
public infix fun AddGroup<T>.into(column: AnyColumnGroupAccessor): Unit = into(column.name())
254323
}
255324

325+
/**
326+
* Creates new columns using the [AddDsl] builder.
327+
*
328+
* An [AddDsl] allows to add multiple new columns and column groups to a [DataFrame]
329+
* using concise syntax based on `from`, `into` operations and [AddExpression]s.
330+
*
331+
* Returns a new [DataFrame] with the newly added columns.
332+
*
333+
* ## Example
334+
*
335+
* ```kotlin
336+
* val dfWithAdded = df.add {
337+
* // Add new column "yearOfBirth" computed as 2021 minus value in "age" column
338+
* "yearOfBirth" from { 2021 - age }
339+
*
340+
* // Add column "is adult" with result of age > 18
341+
* age > 18 into "is adult"
342+
*
343+
* // Add column group "details"
344+
* group("details") {
345+
* // Add column "last name length" with length of lastName
346+
* name.lastName.length() into "last name length"
347+
*
348+
* // Add column "full name" by combining firstName and lastName
349+
* "full name" from { name.firstName + " " + name.lastName }
350+
* }
351+
* }
352+
* ```
353+
*
354+
* @param body An [AddDsl] expression used to define new columns and column groups.
355+
* @return A new [DataFrame] with the added columns.
356+
*/
256357
@Refine
257358
@Interpretable("AddWithDsl")
258359
public fun <T> DataFrame<T>.add(body: AddDsl<T>.() -> Unit): DataFrame<T> {
@@ -261,6 +362,45 @@ public fun <T> DataFrame<T>.add(body: AddDsl<T>.() -> Unit): DataFrame<T> {
261362
return dataFrameOf(this@add.columns() + dsl.columns).cast()
262363
}
263364

365+
/**
366+
* Creates a new column using [AddExpression] and
367+
* adds a new column to the end of each group (i.e., [DataFrame]s) of this [GroupBy] (at the top level).
368+
*
369+
* An [AddExpression] allows to compute a value for each row in the new column
370+
* based on the values from that row in the original group [DataFrame]s.
371+
* Also, you can use other methods such as [prev] and [next] to access other rows,
372+
* including [newValue][AddDataRow.newValue] to retrieve already computed values of this column
373+
* in previous rows.
374+
*
375+
* Returns a new [GroupBy] with the new column
376+
* appended to each group [DataFrame] to the original list of [DataFrame.columns].
377+
*
378+
* ## Example
379+
*
380+
* ```kotlin
381+
* // Add a new column "sum" that contains the sum of values from the "firstValue"
382+
* // and "secondValue" columns for each row.
383+
* val gbWithSum = gb.add("sum") { firstValue + secondValue }
384+
*
385+
* // Add a "fibonacci" column with the Fibonacci sequence:
386+
* // for the first two rows, the value is 1;
387+
* // for subsequent rows, it's the sum of the two previous Fibonacci values.
388+
* val gbWithFibonacci = gb.add("fibonacci") {
389+
* if (index() < 2) 1
390+
* else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
391+
* }
392+
* ```
393+
*
394+
* @param name name for a new column.
395+
* If it is empty, a unique column name will be generated.
396+
* Otherwise, it should be unique for original group [DataFrame]s.
397+
* @param infer a value of [Infer] that specifies how to compute column [type][BaseColumn.type] for a new column.
398+
* Defaults to [Infer.Nulls].
399+
* @param expression [AddExpression] that computes column value for every [DataRow] of a new column.
400+
* @return new [GroupBy] with added column.
401+
*
402+
* @throws DuplicateColumnNamesException if group [DataFrame]s already contains a column with given [name].
403+
*/
264404
@Refine
265405
@Interpretable("GroupByAdd")
266406
public inline fun <reified R, T, G> GroupBy<T, G>.add(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ public fun <T, C> GroupClause<T, C>.into(column: ColumnsSelectionDsl<T>.(ColumnW
181181
* Groups columns, previously selected with [group], into a new or existing column group
182182
* within the [DataFrame] by specifying its path via [ColumnsSelectionDsl] expression.
183183
*
184-
* If the specified path refers to a non-existent column group, it will be created automatically,
185-
* including any missing intermediate segments.
184+
* {@include [org.jetbrains.kotlinx.dataframe.documentation.ColumnPathCreation]}
186185
*
187186
* See [Selecting Columns][SelectingColumns].
188187
*

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ public fun <T> DataFrame<T>.moveToEnd(vararg columns: KProperty<*>): DataFrame<T
379379
/**
380380
* Moves columns, previously selected with [move] into a new position specified by a
381381
* given column path within the [DataFrame].
382-
* If there are non-existent column groups on this path, they will be created.
382+
*
383+
* {@include [org.jetbrains.kotlinx.dataframe.documentation.ColumnPathCreation]}
383384
*
384385
* See [Selecting Columns][SelectingColumns].
385386
*
@@ -417,7 +418,8 @@ public fun <T, C> MoveClause<T, C>.into(column: String): DataFrame<T> = pathOf(c
417418
* Moves columns, previously selected with [move] into a new position specified by a
418419
* given column path within the [DataFrame].
419420
* Provides selected column indices.
420-
* If there are non-existent column groups on this path, they will be created.
421+
*
422+
* {@include [org.jetbrains.kotlinx.dataframe.documentation.ColumnPathCreation]}
421423
*
422424
* See [Selecting Columns][SelectingColumns].
423425
*
@@ -480,7 +482,8 @@ public fun <T, C> MoveClause<T, C>.under(column: AnyColumnGroupAccessor): DataFr
480482
* Moves columns, previously selected with [move] under a new or
481483
* an existing column group specified by a
482484
* column path within the [DataFrame].
483-
* If there are non-existent column groups on this path, they will be created.
485+
*
486+
* {@include [org.jetbrains.kotlinx.dataframe.documentation.ColumnPathCreation]}
484487
*
485488
* See [Selecting Columns][SelectingColumns].
486489
*

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/documentation/DocumentationUrls.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,7 @@ internal interface DocumentationUrls {
101101

102102
/** [See `convert` on the documentation website.]({@include [Url]}/convert.html) */
103103
interface Convert
104+
105+
/** [See `add` on the documentation website.]({@include [Url]}/add.html) */
106+
interface Add
104107
}

0 commit comments

Comments
 (0)