Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making GroupBy extensible #872

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
import org.jetbrains.kotlinx.dataframe.annotations.Refine
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.impl.GroupByImpl
import org.jetbrains.kotlinx.dataframe.impl.aggregation.PivotImpl
import org.jetbrains.kotlinx.dataframe.impl.api.getPivotColumnPaths
import org.jetbrains.kotlinx.dataframe.impl.api.groupByImpl
Expand Down Expand Up @@ -85,24 +84,17 @@ public interface GroupBy<out T, out G> : Grouped<G> {

public fun filter(predicate: GroupedRowFilter<T, G>): GroupBy<T, G>

@Refine
@Interpretable("GroupByToDataFrame")
public fun toDataFrame(groupedColumnName: String? = null): DataFrame<T>

public data class Entry<T, G>(val key: DataRow<T>, val group: DataFrame<G>)

public companion object {
internal val groupedColumnAccessor = column<AnyFrame>("group")
}
}

@Refine
@Interpretable("GroupByToDataFrame")
public fun <T, G> GroupBy<T, G>.toDataFrame(groupedColumnName: String? = null): DataFrame<T> =
if (groupedColumnName == null || groupedColumnName == groups.name()) {
internal().df
} else {
internal().df.rename(groups).into(groupedColumnName)
}

internal fun <T, G> GroupBy<T, G>.internal(): GroupByImpl<T, G> = this as GroupByImpl<T, G>

public interface Grouped<out T> : Aggregatable<T>

public class ReducedGroupBy<T, G>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import org.jetbrains.kotlinx.dataframe.api.concat
import org.jetbrains.kotlinx.dataframe.api.convert
import org.jetbrains.kotlinx.dataframe.api.getColumn
import org.jetbrains.kotlinx.dataframe.api.getColumnsWithPaths
import org.jetbrains.kotlinx.dataframe.api.into
import org.jetbrains.kotlinx.dataframe.api.isColumnGroup
import org.jetbrains.kotlinx.dataframe.api.minus
import org.jetbrains.kotlinx.dataframe.api.pathOf
import org.jetbrains.kotlinx.dataframe.api.rename
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
import org.jetbrains.kotlinx.dataframe.impl.aggregation.AggregatableInternal
import org.jetbrains.kotlinx.dataframe.impl.aggregation.GroupByReceiverImpl
Expand Down Expand Up @@ -56,6 +58,13 @@ internal class GroupByImpl<T, G>(
}
return df[indices].asGroupBy(groups)
}

override fun toDataFrame(groupedColumnName: String?): DataFrame<T> =
if (groupedColumnName == null || groupedColumnName == groups.name()) {
df
} else {
df.rename(groups).into(groupedColumnName)
}
}

internal fun <T, G, R> aggregateGroupBy(
Expand Down
Loading