Skip to content

Commit ff139d7

Browse files
authored
Merge pull request #617 from Kotlin/pivot-then
then operation in pivot column selection DSL inside aggregate
2 parents 35792c9 + d3a9d11 commit ff139d7

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public fun <G> GroupBy<*, G>.pivotCounts(vararg columns: KProperty<*>, inward: B
148148

149149
// region pivot
150150

151-
public fun <T> AggregateGroupedDsl<T>.pivot(inward: Boolean = true, columns: ColumnsSelector<T, *>): PivotGroupBy<T> =
151+
public fun <T> AggregateGroupedDsl<T>.pivot(inward: Boolean = true, columns: PivotColumnsSelector<T, *>): PivotGroupBy<T> =
152152
PivotInAggregateImpl(this, columns, inward)
153153

154154
public fun <T> AggregateGroupedDsl<T>.pivot(vararg columns: String, inward: Boolean = true): PivotGroupBy<T> =

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/PivotInAggregateImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import org.jetbrains.kotlinx.dataframe.ColumnsSelector
44
import org.jetbrains.kotlinx.dataframe.DataFrame
55
import org.jetbrains.kotlinx.dataframe.aggregation.AggregateBody
66
import org.jetbrains.kotlinx.dataframe.aggregation.AggregateGroupedDsl
7+
import org.jetbrains.kotlinx.dataframe.api.PivotColumnsSelector
78
import org.jetbrains.kotlinx.dataframe.api.PivotGroupBy
89
import org.jetbrains.kotlinx.dataframe.impl.api.AggregatedPivot
910
import org.jetbrains.kotlinx.dataframe.impl.api.aggregatePivot
1011
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnSet
1112

1213
internal data class PivotInAggregateImpl<T>(
1314
val aggregator: AggregateGroupedDsl<T>,
14-
val columns: ColumnsSelector<T, *>,
15+
val columns: PivotColumnsSelector<T, *>,
1516
val inward: Boolean?,
1617
val default: Any? = null
1718
) : PivotGroupBy<T>, AggregatableInternal<T> {

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/pivot.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,19 @@ class PivotTests {
153153
1, -1, 5
154154
)
155155
}
156+
157+
@Test
158+
fun `pivot then in aggregate`() {
159+
val df = dataFrameOf(
160+
"category1" to List(12) { it % 3 },
161+
"category2" to List(12) { "category2_${it % 2}" },
162+
"category3" to List(12) { "category3_${it % 5}" },
163+
"value" to List(12) { it }
164+
)
165+
166+
val df1 = df.groupBy("category1").aggregate {
167+
pivot { "category2" then "category3" }.count()
168+
}
169+
df1 shouldBe df.pivot { "category2" then "category3" }.groupBy("category1").count()
170+
}
156171
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public fun <G> GroupBy<*, G>.pivotCounts(vararg columns: KProperty<*>, inward: B
148148

149149
// region pivot
150150

151-
public fun <T> AggregateGroupedDsl<T>.pivot(inward: Boolean = true, columns: ColumnsSelector<T, *>): PivotGroupBy<T> =
151+
public fun <T> AggregateGroupedDsl<T>.pivot(inward: Boolean = true, columns: PivotColumnsSelector<T, *>): PivotGroupBy<T> =
152152
PivotInAggregateImpl(this, columns, inward)
153153

154154
public fun <T> AggregateGroupedDsl<T>.pivot(vararg columns: String, inward: Boolean = true): PivotGroupBy<T> =

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/aggregation/PivotInAggregateImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import org.jetbrains.kotlinx.dataframe.ColumnsSelector
44
import org.jetbrains.kotlinx.dataframe.DataFrame
55
import org.jetbrains.kotlinx.dataframe.aggregation.AggregateBody
66
import org.jetbrains.kotlinx.dataframe.aggregation.AggregateGroupedDsl
7+
import org.jetbrains.kotlinx.dataframe.api.PivotColumnsSelector
78
import org.jetbrains.kotlinx.dataframe.api.PivotGroupBy
89
import org.jetbrains.kotlinx.dataframe.impl.api.AggregatedPivot
910
import org.jetbrains.kotlinx.dataframe.impl.api.aggregatePivot
1011
import org.jetbrains.kotlinx.dataframe.impl.columns.toColumnSet
1112

1213
internal data class PivotInAggregateImpl<T>(
1314
val aggregator: AggregateGroupedDsl<T>,
14-
val columns: ColumnsSelector<T, *>,
15+
val columns: PivotColumnsSelector<T, *>,
1516
val inward: Boolean?,
1617
val default: Any? = null
1718
) : PivotGroupBy<T>, AggregatableInternal<T> {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,19 @@ class PivotTests {
153153
1, -1, 5
154154
)
155155
}
156+
157+
@Test
158+
fun `pivot then in aggregate`() {
159+
val df = dataFrameOf(
160+
"category1" to List(12) { it % 3 },
161+
"category2" to List(12) { "category2_${it % 2}" },
162+
"category3" to List(12) { "category3_${it % 5}" },
163+
"value" to List(12) { it }
164+
)
165+
166+
val df1 = df.groupBy("category1").aggregate {
167+
pivot { "category2" then "category3" }.count()
168+
}
169+
df1 shouldBe df.pivot { "category2" then "category3" }.groupBy("category1").count()
170+
}
156171
}

0 commit comments

Comments
 (0)