Skip to content

Commit 2a90628

Browse files
committed
Rename DataRow.near function to relative and change its return type to DataFrame.
1 parent c76386c commit 2a90628

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

docs/StardustDocs/topics/DataRow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* `namedValues(): List<NameValuePair<Any?>>` — list of name-value pairs where `name` is a column name and `value` is cell value
1818
* `namedValuesOf<T>(): List<NameValuePair<T>>` — list of name-value pairs where value has given type
1919
* `getRow(Int): DataRow` — row from `DataFrame` by row index
20-
* `near(Iterable<Int>): Sequence<DataRow>`sequence of the nearest rows by relative index: `near(-1..1)` will return previous, current and next row. Requested indices will be coerced to valid range and invalid indices will be skipped
21-
* `rows(Iterable<Int>): Sequence<DataRow>`sequence of the rows by absolute index. Requested indices are not coerced to valid boundaries and you should care about it
20+
* `getRows(Iterable<Int>): DataFrame`dataframe with subset of rows selected by absolute row index.
21+
* `relative(Iterable<Int>): DataFrame`dataframe with subset of rows selected by relative row index: `relative(-1..1)` will return previous, current and next row. Requested indices will be coerced to the valid range and invalid indices will be skipped
2222
* `get(column): T` — cell value by this row and given `column`
2323
* `df()``DataFrame` that current row belongs to
2424

examples/jupyter-notebooks/puzzles/40 puzzles.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@
24082408
"source": [
24092409
"df.add(\"id\"){ index() }\n",
24102410
" .groupBy { groups }.add(\"res\") {\n",
2411-
" near(-2..0).map { it.value }.filter { !it.isNaN() }.average()\n",
2411+
" relative(-2..0).value.filter { !it.isNaN() }.mean()\n",
24122412
" }.concat()\n",
24132413
" .sortBy(\"id\")\n",
24142414
" .remove(\"id\")"
@@ -3118,4 +3118,4 @@
31183118
},
31193119
"nbformat": 4,
31203120
"nbformat_minor": 1
3121-
}
3121+
}

src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/DataRowApi.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
1111
import org.jetbrains.kotlinx.dataframe.impl.columnName
1212
import org.jetbrains.kotlinx.dataframe.impl.owner
1313
import org.jetbrains.kotlinx.dataframe.index
14+
import org.jetbrains.kotlinx.dataframe.indices
1415
import org.jetbrains.kotlinx.dataframe.ncol
1516
import org.jetbrains.kotlinx.dataframe.nrow
1617
import org.jetbrains.kotlinx.dataframe.type
@@ -55,6 +56,9 @@ public fun AnyRow.columnTypes(): List<KType> = df().columnTypes()
5556

5657
public fun <T> DataRow<T>.getRow(index: Int): DataRow<T> = getRowOrNull(index)!!
5758

59+
public fun <T> DataRow<T>.getRows(indices: Iterable<Int>): DataFrame<T> = df().getRows(indices)
60+
public fun <T> DataRow<T>.getRows(indices: IntRange): DataFrame<T> = df().getRows(indices)
61+
5862
public fun <T> DataRow<T>.getRowOrNull(index: Int): DataRow<T>? {
5963
val df = df()
6064
return if (index >= 0 && index < df.nrow) df[index] else null
@@ -71,11 +75,11 @@ public fun <T> DataRow<T>.next(): DataRow<T>? {
7175
return if (index < df.nrow - 1) df[index + 1] else null
7276
}
7377

74-
public fun <T> DataRow<T>.rows(absoluteIndices: Iterable<Int>): Sequence<DataRow<T>> =
75-
absoluteIndices.asSequence().map { getRow(index + it) }
78+
public fun <T> DataRow<T>.relative(relativeIndices: Iterable<Int>): DataFrame<T> =
79+
getRows(relativeIndices.mapNotNull { (index + it).let { if (it >= 0 && it < df().rowsCount()) it else null } })
7680

77-
public fun <T> DataRow<T>.near(relativeIndices: Iterable<Int>): Sequence<DataRow<T>> =
78-
relativeIndices.asSequence().mapNotNull { getRowOrNull(index + it) }
81+
public fun <T> DataRow<T>.relative(relativeIndices: IntRange): DataFrame<T> =
82+
getRows((relativeIndices.start + index).coerceIn(df().indices)..(relativeIndices.endInclusive + index).coerceIn(df().indices))
7983

8084
internal fun <T> DataRow<T>.movingAverage(k: Int, expression: RowExpression<T, Number>): Double {
8185
var count = 0

src/test/kotlin/org/jetbrains/kotlinx/dataframe/puzzles/HardTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class HardTests {
115115

116116
df.add("id") { index() }
117117
.groupBy { groups }.add("res") {
118-
round(near(-2..0).map { it[value] }.filter { !it.isNaN() }.average())
118+
round(relative(-2..0)[value].filter { !it.isNaN() }.mean())
119119
}.concat()
120120
.sortBy("id")
121121
.remove("id") shouldBe expected

src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ import org.jetbrains.kotlinx.dataframe.api.moveToLeft
102102
import org.jetbrains.kotlinx.dataframe.api.moveToRight
103103
import org.jetbrains.kotlinx.dataframe.api.name
104104
import org.jetbrains.kotlinx.dataframe.api.named
105-
import org.jetbrains.kotlinx.dataframe.api.near
106105
import org.jetbrains.kotlinx.dataframe.api.notNull
107106
import org.jetbrains.kotlinx.dataframe.api.nullable
108107
import org.jetbrains.kotlinx.dataframe.api.parse
@@ -1876,11 +1875,6 @@ class DataFrameTests : BaseTest() {
18761875
stringCols.columnNames() shouldBe listOf("name", "city")
18771876
}
18781877

1879-
@Test
1880-
fun neighbours() {
1881-
typed[2].near(-1..1).toList() shouldBe listOf(typed[1], typed[2], typed[3])
1882-
}
1883-
18841878
@Test
18851879
fun `get row value by expression`() {
18861880
val expression: RowExpression<Person, Int> = { it.age * 2 }

src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataRowTests.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.jetbrains.kotlinx.dataframe.api.namedValues
1414
import org.jetbrains.kotlinx.dataframe.api.namedValuesOf
1515
import org.jetbrains.kotlinx.dataframe.api.next
1616
import org.jetbrains.kotlinx.dataframe.api.prev
17+
import org.jetbrains.kotlinx.dataframe.api.relative
1718
import org.jetbrains.kotlinx.dataframe.api.rowMean
1819
import org.jetbrains.kotlinx.dataframe.api.rowStd
1920
import org.jetbrains.kotlinx.dataframe.api.rowSum
@@ -85,4 +86,11 @@ class DataRowTests : BaseTest() {
8586
if (firstNullIndex == -1) null else columnNames()[firstNullIndex]
8687
}
8788
}
89+
90+
@Test
91+
fun relativeTest() {
92+
typed[1].relative(0..0) shouldBe typed[1..1]
93+
typed[1].relative(-2..2) shouldBe typed[0..3]
94+
typed[1].relative(listOf(2, -1, -3, 0)) shouldBe typed[3, 0, 1]
95+
}
8896
}

0 commit comments

Comments
 (0)