Skip to content

Commit f46fd53

Browse files
committed
renamed in tests, greatly simplified and compacted asFrame function for clarity
1 parent 45036c9 commit f46fd53

File tree

2 files changed

+31
-28
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

2 files changed

+31
-28
lines changed

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import org.jetbrains.kotlinx.dataframe.Selector
1111
import org.jetbrains.kotlinx.dataframe.api.AddDataRow
1212
import org.jetbrains.kotlinx.dataframe.api.Update
1313
import org.jetbrains.kotlinx.dataframe.api.asColumnGroup
14+
import org.jetbrains.kotlinx.dataframe.api.asDataFrame
1415
import org.jetbrains.kotlinx.dataframe.api.cast
1516
import org.jetbrains.kotlinx.dataframe.api.indices
1617
import org.jetbrains.kotlinx.dataframe.api.isEmpty
1718
import org.jetbrains.kotlinx.dataframe.api.name
1819
import org.jetbrains.kotlinx.dataframe.api.replace
19-
import org.jetbrains.kotlinx.dataframe.api.rows
2020
import org.jetbrains.kotlinx.dataframe.api.toColumn
2121
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
2222
import org.jetbrains.kotlinx.dataframe.api.with
@@ -53,34 +53,37 @@ internal fun <T, C> Update<T, C>.updateWithValuePerColumnImpl(selector: Selector
5353
internal fun <T, C, R> Update<T, DataRow<C>>.asFrameImpl(expression: DataFrameExpression<C, DataFrame<R>>): DataFrame<T> =
5454
if (df.isEmpty()) df
5555
else df.replace(columns).with {
56-
val src = it.asColumnGroup()
57-
val updatedColumn = expression(src, src).asColumnGroup(src.name())
56+
// First, we create an updated column group with the result of the expression
57+
val srcColumnGroup = it.asColumnGroup()
58+
val updatedColumnGroup = srcColumnGroup
59+
.asDataFrame()
60+
.let { expression(it, it) }
61+
.asColumnGroup(srcColumnGroup.name())
62+
5863
if (filter == null) {
59-
// If there is no filter, we simply replace the selected column groups with the result of the expression
60-
updatedColumn
64+
// If there is no filter, we simply return the updated column group
65+
updatedColumnGroup
6166
} else {
62-
// If there is a filter, we collect the indices of the rows that are inside the filter
63-
val collector = createDataCollector<DataRow<C>>(it.size, it.type)
64-
val indices = buildList {
65-
df.indices().forEach { rowIndex ->
66-
val row = AddDataRowImpl(rowIndex, df, collector.values)
67-
val currentValue = row[src]
67+
// If there is a filter, then we replace the rows of the source column group with the updated column group
68+
// only if they satisfy the filter
69+
srcColumnGroup.replaceRowsIf(from = updatedColumnGroup) {
70+
val srcRow = df[it.index]
71+
val srcValue = srcRow[srcColumnGroup]
6872

69-
if (filter.invoke(row, currentValue)) {
70-
this += rowIndex
71-
collector.add(currentValue)
72-
}
73-
}
73+
filter.invoke(srcRow, srcValue)
7474
}
75-
76-
// Then we only replace the original rows with the updated rows that are inside the filter
77-
src.rows().map {
78-
val index = indices.indexOf(it.index)
79-
if (index == -1) it else updatedColumn[index]
80-
}.toColumn(src.name)
8175
}
8276
}
8377

78+
private fun <C, R> ColumnGroup<C>.replaceRowsIf(
79+
from: ColumnGroup<R>,
80+
condition: (DataRow<C>) -> Boolean = { true },
81+
): ColumnGroup<C> = values()
82+
.map { if (condition(it)) from[it.index] else it }
83+
.toColumn(name)
84+
.asColumnGroup()
85+
.cast()
86+
8487
internal fun <T, C> DataColumn<C>.updateImpl(
8588
df: DataFrame<T>,
8689
filter: RowValueFilter<T, C>?,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ class UpdateTests {
1616
}
1717

1818
@DataSchema
19-
interface AAndB {
19+
interface DataPart {
2020
val a: Int
2121
val b: String
2222
}
2323

2424
@DataSchema
25-
data class Update(
25+
data class Data(
2626
override val a: Int,
2727
override val b: String,
2828
val c: Boolean,
29-
) : AAndB
29+
) : DataPart
3030

3131
@Test
3232
fun `update asFrame`() {
3333
val df = listOf(
34-
Update(1, "a", true),
35-
Update(2, "b", false),
34+
Data(1, "a", true),
35+
Data(2, "b", false),
3636
).toDataFrame()
3737

38-
val group by columnGroup<AAndB>() named "Some Group"
38+
val group by columnGroup<DataPart>() named "Some Group"
3939
val groupedDf = df.group { a and b }.into { group }
4040

4141
val res = groupedDf

0 commit comments

Comments
 (0)