Skip to content

Commit 2f7e746

Browse files
Automated commit of generated code
1 parent c5899ef commit 2f7e746

File tree

6 files changed

+399
-4
lines changed

6 files changed

+399
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public interface DataFrame<out T> :
6868

6969
// region rows
7070

71+
/**
72+
* Returns the total number of rows of this [DataFrame].
73+
*
74+
* @return The number of rows in the [DataFrame].
75+
*/
7176
public fun rowsCount(): Int
7277

7378
public operator fun iterator(): Iterator<DataRow<T>> = rows().iterator()

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

Lines changed: 253 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateValue
1212

1313
// region DataColumn
1414

15+
/**
16+
* Counts the elements in this [DataColumn] that satisfy a given [predicate] or returns the total count
17+
* if no predicate is provided.
18+
*
19+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
20+
*
21+
* @param predicate An optional predicate used to filter the elements.
22+
* The predicate should return `true` for elements to be counted.
23+
* If `null` (by default), all elements are counted.
24+
* @return The count of elements in the column
25+
* that either match the predicate or the total count of elements if no predicate is provided.
26+
*/
1527
public fun <T> DataColumn<T>.count(predicate: Predicate<T>? = null): Int =
1628
if (predicate == null) {
1729
size()
@@ -23,27 +35,127 @@ public fun <T> DataColumn<T>.count(predicate: Predicate<T>? = null): Int =
2335

2436
// region DataRow
2537

38+
/**
39+
* Returns the number of columns in this [DataRow].
40+
*
41+
* @return the number of columns in this row.
42+
* @see [columnsCount].
43+
*/
2644
public fun AnyRow.count(): Int = columnsCount()
2745

46+
/**
47+
* Counts the number of elements in the current row that satisfy the given [predicate].
48+
*
49+
* @param predicate A predicate function to test each element.
50+
* The predicate should return `true` for elements to be counted.
51+
* @return The number of elements that satisfy the predicate.
52+
*/
2853
public inline fun AnyRow.count(predicate: Predicate<Any?>): Int = values().count(predicate)
2954

3055
// endregion
3156

3257
// region DataFrame
3358

59+
/**
60+
* Returns the total number of rows of this [DataFrame].
61+
*
62+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
63+
*
64+
* @return The number of rows in the [DataFrame].
65+
*/
3466
public fun <T> DataFrame<T>.count(): Int = rowsCount()
3567

68+
/**
69+
* Counts the number of rows in this [DataFrame] that satisfy the given [predicate].
70+
*
71+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] both as `this` and `it`,
72+
* allowing you to define a [Boolean] condition using the row's values,
73+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient access.
74+
*
75+
* See also:
76+
* - [filter][DataFrame.filter] — filters rows using a [RowFilter] condition.
77+
* - [countDistinct][DataFrame.countDistinct] — counts distinct rows or values.
78+
*
79+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
80+
*
81+
* ### Example
82+
* ```kotlin
83+
* // Count rows where the value in the "age" column is greater than 18
84+
* // and the "name/firstName" column starts with 'A'
85+
* df.count { age > 18 && name.firstName.startsWith("A") }
86+
* // Count rows
87+
* df.count { prev()?.length >= 50.0 ?: false }
88+
* ```
89+
*
90+
* @param T The schema marker type of the [DataFrame].
91+
* @param predicate A [RowFilter] that returns `true` for rows that should be counted.
92+
* @return The number of rows that satisfy the predicate.
93+
*/
3694
public inline fun <T> DataFrame<T>.count(predicate: RowFilter<T>): Int = rows().count { predicate(it, it) }
3795

3896
// endregion
3997

4098
// region GroupBy
4199

100+
/**
101+
* Aggregates this [GroupBy] by counting the number of rows in each group.
102+
*
103+
* Returns a new [DataFrame] where each row corresponds to a group.
104+
* The resulting frame contains:
105+
* - the original group key columns,
106+
* - a new column (named [resultName], default is `"count"`) that contains the number of rows in each group.
107+
*
108+
* This is equivalent to applying `.aggregate { count() }`, but more efficient.
109+
*
110+
* See also [DataFrame.groupBy] and common [aggregate][Grouped.aggregate].
111+
*
112+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
113+
*
114+
* ### Example
115+
* ```kotlin
116+
* // Counts number of rows for each city, returning
117+
* // a new DataFrame with columns "city" and "count"
118+
* df.groupBy { city }.count()
119+
* ```
120+
*
121+
* @param resultName The name of the result column that will store the group sizes. Defaults to `"count"`.
122+
* @return A new [DataFrame] with group keys and corresponding group sizes.
123+
*/
42124
@Refine
43125
@Interpretable("GroupByCount0")
44126
public fun <T> Grouped<T>.count(resultName: String = "count"): DataFrame<T> =
45127
aggregateValue(resultName) { count() default 0 }
46128

129+
/**
130+
* Aggregates this [GroupBy] by counting the number of rows in each group
131+
* that satisfy the given [predicate].
132+
*
133+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] both as `this` and `it`,
134+
* allowing you to define a [Boolean] condition using the row's values,
135+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient access.
136+
*
137+
* Returns a new [DataFrame] where each row corresponds to a group.
138+
* The resulting frame contains:
139+
* - the original group key columns,
140+
* - a new column (named [resultName], defaults to `"count"`)
141+
* that stores the number of rows in each group matching the [predicate].
142+
*
143+
* This is equivalent to calling `.aggregate { count(predicate) }`, but more efficient.
144+
*
145+
* See also [DataFrame.groupBy] and common [aggregate][Grouped.aggregate].
146+
*
147+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
148+
*
149+
* ### Example
150+
* ```kotlin
151+
* // Count rows for each city where the "income" value is greater than 30.0.
152+
* // Returns a new DataFrame with columns "city" and "pointsCount".
153+
* df.groupBy { city }.count("pointsCount") { income >= 30.0 }
154+
* ```
155+
*
156+
* @param resultName The name of the result column containing the group sizes. Defaults to `"count"`.
157+
* @return A new [DataFrame] with group keys and filtered row counts per group.
158+
*/
47159
@Refine
48160
@Interpretable("GroupByCount0")
49161
public inline fun <T> Grouped<T>.count(
@@ -55,20 +167,159 @@ public inline fun <T> Grouped<T>.count(
55167

56168
// region Pivot
57169

170+
/**
171+
* Aggregates this [Pivot] by counting the number of rows in each group.
172+
*
173+
* Returns a single [DataRow] where:
174+
* - each column corresponds to a [pivot] group — if multiple pivot keys were used,
175+
* the result will contain column groups for each pivot key, with columns inside
176+
* corresponding to the values of that key;
177+
* - each value contains the number of rows in that group.
178+
*
179+
* The original [Pivot] column structure is preserved.
180+
* If the [Pivot] was created using multiple or nested keys
181+
* (e.g., via [and][PivotDsl.and] or [then][PivotDsl.then]),
182+
* the structure remains unchanged — only the contents of each group
183+
* are replaced with the number of rows in that group.
184+
*
185+
* This is equivalent to calling `.aggregate { count() }`, but more efficient.
186+
*
187+
* See also:
188+
* - [pivot].
189+
* - common [aggregate][Pivot.aggregate].
190+
* - [pivotCounts][DataFrame.pivotCounts] shortcut.
191+
*
192+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
193+
*
194+
* ### Example
195+
* ```kotlin
196+
* // Count the number of rows for each city.
197+
* // Returns a single DataRow with one column per city and the count of rows in each.
198+
* df.pivot { city }.count()
199+
* ```
200+
*
201+
* @return A single [DataRow] with one column per group and the corresponding group size as its value.
202+
*/
58203
public fun <T> Pivot<T>.count(): DataRow<T> = delegate { count() }
59204

205+
/**
206+
* Aggregates this [Pivot] by counting the number of rows in each group
207+
* that satisfy the given [predicate].
208+
*
209+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] both as `this` and `it`,
210+
* allowing you to define a [Boolean] condition using the row's values,
211+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient access.
212+
*
213+
* Returns a single [DataRow] where:
214+
* - each column corresponds to a [pivot] group — if multiple pivot keys were used,
215+
* the result will contain column groups for each pivot key, with columns inside
216+
* corresponding to the values of that key;
217+
* - each value contains the number of rows in that group matching the [predicate].
218+
*
219+
* The original [Pivot] column structure is preserved.
220+
* If the [Pivot] was created using multiple or nested keys
221+
* (e.g., via [and][PivotDsl.and] or [then][PivotDsl.then]),
222+
* the structure remains unchanged — only the contents of each group
223+
* are replaced with the number of rows (matching the [predicate]) in that group.
224+
*
225+
* This is equivalent to calling `.aggregate { count(predicate) }`, but more efficient.
226+
*
227+
* See also:
228+
* - [pivot].
229+
* - common [aggregate][Pivot.aggregate].
230+
* - [pivotCounts][DataFrame.pivotCounts] shortcut.
231+
*
232+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
233+
*
234+
* ### Example
235+
* ```kotlin
236+
* // Count rows for each city where the "income" value is greater than 30.0.
237+
* // Returns a single DataRow with one column per city and the count of matching rows.
238+
* df.pivot { city }.count { income > 30.0 }
239+
* ```
240+
*
241+
* @return A single [DataRow] with original [Pivot] columns and filtered row counts per group.
242+
*/
60243
public inline fun <T> Pivot<T>.count(crossinline predicate: RowFilter<T>): DataRow<T> = delegate { count(predicate) }
61244

62245
// endregion
63246

64247
// region PivotGroupBy
65248

249+
/**
250+
* Aggregates this [PivotGroupBy] by counting the number of rows in each
251+
* combined [pivot] + [groupBy] group.
252+
*
253+
* Returns a new [DataFrame] containing a following matrix:
254+
* - one row per [groupBy] key (or keys set);
255+
* - one column group per [pivot] key, where each inner column corresponds to a value of that key;
256+
* - each cell contains the number of rows in the corresponding pivot–group pair.
257+
*
258+
* The original [Pivot] column structure is preserved.
259+
* If the [Pivot] was created using multiple or nested keys
260+
* (e.g., via [and][PivotDsl.and] or [then][PivotDsl.then]),
261+
* the result will contain nested column groups reflecting that key structure,
262+
* with each group containing columns for the values of the corresponding key.
263+
*
264+
* This is equivalent to calling `.aggregate { count() }`, but more efficient.
265+
*
266+
* See also:
267+
* - [pivot], [DataFrame.groupBy], [Pivot.groupBy] and [GroupBy.pivot].
268+
* - common [aggregate][PivotGroupBy.aggregate];
269+
* - [GroupBy.pivotCounts] shortcut.
270+
*
271+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
272+
*
273+
* ### Example
274+
* ```kotlin
275+
* // Compute a matrix with "city" values horizontally and
276+
* // "age" values vertically, where each cell contains
277+
* // the number of rows with the corresponding age–city pair.
278+
* df.pivot { city }.groupBy { age }.count()
279+
* ```
280+
*
281+
* @return A [DataFrame] with [groupBy] rows and pivoted counts as columns.
282+
*/
66283
public fun <T> PivotGroupBy<T>.count(): DataFrame<T> = aggregate { count() default 0 }
67284

285+
/**
286+
* Aggregates this [PivotGroupBy] by counting the number of rows in each
287+
* combined [pivot] + [groupBy] group, that satisfy the given [predicate].
288+
*
289+
* Returns a new [DataFrame] containing a following matrix:
290+
* - one row per [groupBy] key (or keys set);
291+
* - one column group per [pivot] key, where each inner column corresponds to a value of that key;
292+
* - each cell contains the number of rows in the corresponding pivot–group pair.
293+
*
294+
* The original [Pivot] column structure is preserved.
295+
* If the [Pivot] was created using multiple or nested keys
296+
* (e.g., via [and][PivotDsl.and] or [then][PivotDsl.then]),
297+
* the result will contain nested column groups reflecting that key structure,
298+
* with each group containing columns for the values
299+
* (matching the [predicate]) of the corresponding key.
300+
*
301+
* This is equivalent to calling `.aggregate { count() }`, but more efficient.
302+
*
303+
* See also:
304+
* - [pivot], [DataFrame.groupBy], [Pivot.groupBy] and [GroupBy.pivot].
305+
* - common [aggregate][PivotGroupBy.aggregate];
306+
* - [GroupBy.pivotCounts] shortcut.
307+
*
308+
* For more information: [See `count` on the documentation website.](https://kotlin.github.io/dataframe/count.html)
309+
*
310+
* ### Example
311+
* ```kotlin
312+
* // Compute a matrix with "city" values horizontally and
313+
* // "age" values vertically, where each cell contains
314+
* // the number of rows with the corresponding age–city pair.
315+
* df.pivot { city }.groupBy { age }.count()
316+
* ```
317+
*
318+
* @return A [DataFrame] with [groupBy] rows and pivoted counts as columns matching the [predicate]..
319+
*/
68320
public inline fun <T> PivotGroupBy<T>.count(crossinline predicate: RowFilter<T>): DataFrame<T> =
69321
aggregate {
70-
count(predicate) default
71-
0
322+
count(predicate) default 0
72323
}
73324

74325
// endregion

0 commit comments

Comments
 (0)