Skip to content

Commit 50abd69

Browse files
pivot in aggregation kdocs
1 parent d19df01 commit 50abd69

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -828,40 +828,64 @@ public fun <G> GroupBy<*, G>.pivotCounts(vararg columns: KProperty<*>, inward: B
828828

829829

830830
/**
831-
* A special [pivot][GroupBy.pivot] override for usage in [aggregate][Grouped.aggregate] method
832-
* of [GroupBy].
833-
* This allows combining [column pivoting aggregations][PivotGroupByDocs.Aggregation]
834-
* with common [GroupBy] aggregations in [aggregate][Grouped.aggregate].
831+
* Pivots the selected [\columns] within each group for further
832+
* [pivot aggregations][PivotGroupByDocs.Aggregation].
835833
*
836-
* This function itself doesn't affect [aggregate][Grouped.aggregate] result, but
837-
* it allows putting results of [PivotGroupBy aggregations][PivotGroupByDocs.Aggregation] into
838-
* [aggregate][Grouped.aggregate] resulting [DataFrame] by simply calling them.
839-
* See [GroupBy.pivot] and [PivotGroupByDocs.Aggregation] for more information.
834+
* This function itself does not directly affect the [aggregate][Grouped.aggregate] result,
835+
* but instead creates an intermediate [PivotGroupBy].
836+
* The resulting columns of its [aggregations][PivotGroupByDocs.Aggregation] are then
837+
* putted into the final [DataFrame] produced by [aggregate][Grouped.aggregate]
838+
* when those aggregation functions are invoked.
840839
*
841-
* Resulting columns added as common [aggregations][Grouped.aggregate] result columns;
842-
* their structure depends on exact
843-
* [PivotGroupBy aggregations][PivotGroupByDocs.Aggregation] used.
840+
* See [GroupBy.pivot] and [PivotGroupByDocs.Aggregation] for detailed information.
841+
*
842+
* The resulting columns are added as standard [aggregate][Grouped.aggregate] result columns.
843+
* Their structure depends on the specific [PivotGroupBy aggregations][PivotGroupByDocs.Aggregation] used.
844+
*
845+
* For more information: {@include [DocumentationUrls.PivotInsideAggregationStatistics]}
846+
*
847+
* Check out [`PivotGroupBy` Grammar][PivotGroupByDocs.Grammar].
848+
*
849+
* ### This `pivot` overload
844850
*
845851
* ### Example
846852
* ```kotlin
847853
* df.groupBy { name.firstName }.aggregate {
848-
* // Pivot "city" column of each group, resulting into
849-
* // `PivotGroupBy` with "firstName" groping keys and "city" values columns
854+
* // Pivot the "city" column within each group,
855+
* // creating a PivotGroupBy with "firstName" as grouping keys and "city" as pivoted columns
850856
* pivot { city }.aggregate {
851-
* // Aggregate mean of "age" column values of each of
852-
* // `groupBy` x `pivot` group into "meanAge" column
857+
* // Aggregate the mean of "age" column values for each
858+
* // groupBy × pivot combination into the "meanAge" column
853859
* mean { age } into "meanAge"
854-
* // Aggregate size of each `PivotGroupBy` group into "count" column
860+
*
861+
* // Aggregate the size of each PivotGroupBy group into the "count" column
855862
* count() into "count"
856863
* }
857-
* // Shortcut for `count` aggregation in
858-
* // "firstName" x "lastName" groups
859-
* pivot { name.lastName }.count()
860-
* // Common `count` aggregation
864+
*
865+
* // Shortcut for `count` aggregation in "firstName" × "lastName" groups
866+
* // into "namesCount" column
867+
* pivot { name.lastName }.count() into "namesCount"
868+
*
869+
* // Standard `count` aggregation across all rows in each "firstName" group
870+
* // into "total" column
861871
* count() into "total"
862872
* }
863873
* ```
864874
*/
875+
internal interface AggregateGroupedDslPivotDocs
876+
877+
/**
878+
* @include [AggregateGroupedDslPivotDocs]
879+
* Select or express pivot columns using the [PivotDsl].
880+
*
881+
* @param inward If `true` (default), the generated pivoted columns are nested inside the original column;
882+
* otherwise, they are placed at the top level.
883+
* @param columns The [Pivot Columns Selector][PivotColumnsSelector] that defines which columns are used
884+
* as keys for pivoting and in which order.
885+
* @return A new [Pivot] containing the unique values of the selected column as new columns
886+
* (or as [column groups][ColumnGroup] for multiple key columns),
887+
* with their corresponding groups of rows represented as [DataFrame]s.
888+
*/
865889
public fun <T> AggregateGroupedDsl<T>.pivot(
866890
inward: Boolean = true,
867891
columns: PivotColumnsSelector<T, *>,

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
@@ -170,4 +170,7 @@ internal interface DocumentationUrls {
170170

171171
/** [See "`pivot` statistics" on the documentation website.]({@include [Url]}/summarystatistics.html#pivot-statistics) */
172172
interface PivotStatistics
173+
174+
/** [See "`pivot` inside aggregation" on the documentation website.]({@include [Url]}/pivot.html#pivot-inside-aggregate) */
175+
interface PivotInsideAggregationStatistics
173176
}

0 commit comments

Comments
 (0)