@@ -7,17 +7,102 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
7
7
import org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload
8
8
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
9
9
import org.jetbrains.kotlinx.dataframe.annotations.Refine
10
+ import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
10
11
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
11
12
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
13
+ import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls
14
+ import org.jetbrains.kotlinx.dataframe.documentation.DslGrammarLink
15
+ import org.jetbrains.kotlinx.dataframe.documentation.ExcludeFromSources
16
+ import org.jetbrains.kotlinx.dataframe.documentation.Indent
17
+ import org.jetbrains.kotlinx.dataframe.documentation.LineBreak
18
+ import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
12
19
import org.jetbrains.kotlinx.dataframe.impl.columnName
13
20
import kotlin.experimental.ExperimentalTypeInference
14
21
import kotlin.reflect.KProperty
15
22
16
23
// region DataFrame
17
24
25
+ /* *
26
+ * Groups the specified [columns\] within the [DataFrame].
27
+ *
28
+ * This function does not immediately group the columns but instead select columns to group and
29
+ * returns a [GroupClause],
30
+ * which serves as an intermediate step.
31
+ * The [GroupClause] allows specifying the final
32
+ * destination of the selected columns using methods such
33
+ * as [into][GroupClause.into] and,
34
+ * that return a new [DataFrame] with grouped columns.
35
+ * Check out [Grammar].
36
+ *
37
+ * @include [SelectingColumns.ColumnGroupsAndNestedColumnsMention]
38
+ *
39
+ * See [Selecting Columns][GroupSelectingOptions].
40
+ *
41
+ * For more information: {@include [DocumentationUrls.Group]}
42
+ *
43
+ * Reverse operation: [ungroup].
44
+ *
45
+ * It is a special case of [move] operation.
46
+ *
47
+ * Don't confuse this with [groupBy],
48
+ * which groups the dataframe by the values in the selected columns!
49
+ */
50
+ internal interface GroupDocs {
51
+
52
+ /* *
53
+ * {@comment Version of [SelectingColumns] with correctly filled in examples}
54
+ * @include [SelectingColumns] {@include [SetGroupOperationArg]}
55
+ */
56
+ interface GroupSelectingOptions
57
+
58
+ /* *
59
+ * ## Group Operation Grammar
60
+ * {@include [LineBreak]}
61
+ * {@include [DslGrammarLink]}
62
+ * {@include [LineBreak]}
63
+ *
64
+ * [**`into`**][GroupClause.into]**`(`**`groupName: `[`String`][String]**`)`**
65
+ *
66
+ * {@include [Indent]}
67
+ * __`.`__[**`into`**][GroupClause.into]**` { `**`groupNameExpression: `[`ColumnsSelector`][ColumnsSelector]**` } `**
68
+ */
69
+ interface Grammar
70
+ }
71
+
72
+ /* * {@set [SelectingColumns.OPERATION] [group][group]} */
73
+ @ExcludeFromSources
74
+ private interface SetGroupOperationArg
75
+
76
+ /* *
77
+ * {@include [GroupDocs]}
78
+ * ### This Group Overload
79
+ */
80
+ @ExcludeFromSources
81
+ private interface CommonGroupDocs
82
+
83
+ /* *
84
+ * @include [CommonGroupDocs]
85
+ * @include [SelectingColumns.Dsl] {@include [SetGroupOperationArg]}
86
+ * ### Examples:
87
+ * ```kotlin
88
+ * df.group { columnA and columnB }.into("valueCols")
89
+ * df.group { colsOf<String>() }.into { it.name.split(".").first() }
90
+ * ```
91
+ * @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to group.
92
+ */
18
93
@Interpretable(" Group0" )
19
94
public fun <T , C > DataFrame<T>.group (columns : ColumnsSelector <T , C >): GroupClause <T , C > = GroupClause (this , columns)
20
95
96
+ /* *
97
+ * @include [CommonGroupDocs]
98
+ * @include [SelectingColumns.ColumnNames] {@include [SetGroupOperationArg]}
99
+ * ### Example:
100
+ * ```kotlin
101
+ * df.group("second").into("valueCols")
102
+ * df.group("prop.A", "prop.B", "cnt.A", "cnt.B").into { it.name.split(".").first() }
103
+ * ```
104
+ * @param [columns\] The [Column Names][String] used to select the columns of this [DataFrame] to group.
105
+ */
21
106
public fun <T > DataFrame<T>.group (vararg columns : String ): GroupClause <T , Any ?> = group { columns.toColumnSet() }
22
107
23
108
@AccessApiOverload
@@ -31,23 +116,110 @@ public fun <T> DataFrame<T>.group(vararg columns: KProperty<*>): GroupClause<T,
31
116
32
117
// region GroupClause
33
118
119
+ /* *
120
+ * An intermediate class used in the [group] operation.
121
+ *
122
+ * This class itself does nothing—it is just a transitional step before specifying
123
+ * how to group the selected columns.
124
+ * It must be followed by one of the positioning methods
125
+ * to produce a new [DataFrame] with the updated column structure.
126
+ *
127
+ * Use the following methods to finalize the move:
128
+ * - [into(groupName)][GroupClause.into] – groups selected columns into a one column group.
129
+ * - [into { groupNameExpression }][GroupClause.into] – groups each column into a group
130
+ * by specifying path or name.
131
+ *
132
+ * See [Grammar][GroupDocs.Grammar] for more details.
133
+ */
34
134
public class GroupClause <T , C >(internal val df : DataFrame <T >, internal val columns : ColumnsSelector <T , C >) {
35
135
override fun toString (): String = " GroupClause(df=$df , columns=$columns )"
36
136
}
37
137
38
138
// region into
39
139
140
+ /* *
141
+ * Groups columns, previously selected with [group], into new or existing column groups
142
+ * within the [DataFrame], using an [ColumnsSelectionDsl] expression to specify the target group name for each column.
143
+ * The expression is applied to each selected column and determines the name of the column group
144
+ * it will be placed into.
145
+ *
146
+ * If a column group with the specified name does not exist, it will be created.
147
+ *
148
+ * See [Selecting Columns][SelectingColumns].
149
+ *
150
+ * For more information: {@include [DocumentationUrls.Group]}
151
+ *
152
+ * @include [SelectingColumns.ColumnNames]
153
+ *
154
+ * ### Example:
155
+ * ```kotlin
156
+ * df.group { all() }.into { it.type().toString() }
157
+ * ```
158
+ *
159
+ * @param column A [ColumnsSelector] expression that takes a column and returns the name of the [ColumnGroup]
160
+ * where that column should be grouped.
161
+ * All selected columns will be moved under the groups defined by this expression.
162
+ */
40
163
@JvmName(" intoString" )
41
164
@OverloadResolutionByLambdaReturnType
42
165
@OptIn(ExperimentalTypeInference ::class )
43
166
public fun <T , C > GroupClause <T , C >.into (column : ColumnsSelectionDsl <T >.(ColumnWithPath <C >) -> String ): DataFrame <T > =
44
167
df.move(columns).under { column(it).toColumnAccessor() }
45
168
169
+ /* *
170
+ * Groups columns, previously selected with [group], into a new or existing column group
171
+ * within the [DataFrame] by specifying its path via [ColumnsSelectionDsl] expression.
172
+ *
173
+ * If the specified path refers to a non-existent column group, it will be created automatically,
174
+ * including any missing intermediate segments.
175
+ *
176
+ * See [Selecting Columns][SelectingColumns].
177
+ *
178
+ * For more information: {@include [DocumentationUrls.Group]}
179
+ *
180
+ * @include [SelectingColumns.ColumnNames]
181
+ *
182
+ * ### Examples:
183
+ * ```kotlin
184
+ * // Group selected columns into an existing column group:
185
+ * df.group("age", "weight").into { info }
186
+ * // Group selected columns into a nested column group using a path that may
187
+ * // contain both existing and new segments:
188
+ * df.group { employee.age and employee.weight }.into { pathOf("info", "personal") }
189
+ * // Group selected columns under their ancestor group located two levels up in the path hierarchy
190
+ * df.group { colsAtAnyDepth().colsOf<String>() }.into { it.path.dropLast(2) }
191
+ * ```
192
+ *
193
+ * @param column A [ColumnsSelector] expression that takes a column and returns the full path to the [ColumnGroup]
194
+ * where that column should be grouped.
195
+ * All selected columns will be moved under the groups defined by this expression.
196
+ */
46
197
@JvmName(" intoColumn" )
47
198
public fun <T , C > GroupClause <T , C >.into (
48
199
column : ColumnsSelectionDsl <T >.(ColumnWithPath <C >) -> AnyColumnReference ,
49
200
): DataFrame <T > = df.move(columns).under(column)
50
201
202
+ /* *
203
+ * Groups columns, previously selected with [group], into a new or existing column group
204
+ * within the [DataFrame], by specifying its name.
205
+ *
206
+ * If a column group with the specified name does not exist, it will be created.
207
+ *
208
+ * See [Selecting Columns][SelectingColumns].
209
+ *
210
+ * For more information: {@include [DocumentationUrls.Group]}
211
+ *
212
+ * @include [SelectingColumns.ColumnNames]
213
+ *
214
+ * ### Examples:
215
+ * ```kotlin
216
+ * df.group("age", "weight").into("info")
217
+ * df.group { age and weight }.into("info")
218
+ * ```
219
+ *
220
+ * @param [column] A [ColumnsSelector] that defines the path to a [ColumnGroup]
221
+ * in the [DataFrame], where the selected columns will be moved.
222
+ */
51
223
@Refine
52
224
@Interpretable(" Into0" )
53
225
public fun <T , C > GroupClause <T , C >.into (column : String ): DataFrame <T > = into(columnGroup().named(column))
0 commit comments