Skip to content

Commit 27ee90e

Browse files
committed
reverting behavior of rowXOf functions to follow the type of the values, not the columns
1 parent edbcaea commit 27ee90e

File tree

8 files changed

+34
-33
lines changed

8 files changed

+34
-33
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public fun AnyRow.rowMaxOrNull(): Any? = error(ROW_MAX_OR_NULL)
6464
@Deprecated(ROW_MAX, level = DeprecationLevel.ERROR)
6565
public fun AnyRow.rowMax(): Any = error(ROW_MAX)
6666

67-
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMaxOfOrNull(skipNaN: Boolean = skipNaN_default): T? =
68-
Aggregators.max<T>(skipNaN).aggregateOfRow(this) { colsOf<T>() }
67+
public inline fun <reified T : Comparable<T>> AnyRow.rowMaxOfOrNull(skipNaN: Boolean = skipNaN_default): T? =
68+
Aggregators.max<T>(skipNaN).aggregateOfRow(this) { colsOf<T?>() }
6969

70-
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMaxOf(skipNaN: Boolean = skipNaN_default): T & Any =
70+
public inline fun <reified T : Comparable<T>> AnyRow.rowMaxOf(skipNaN: Boolean = skipNaN_default): T =
7171
rowMaxOfOrNull<T>(skipNaN).suggestIfNull("rowMaxOf")
7272

7373
// endregion
@@ -460,8 +460,7 @@ public inline fun <T, reified R : Comparable<R & Any>?> DataColumn<T>.maxOfOrNul
460460
maxOfOrNull(skipNaN = skipNaN_default, selector = selector)
461461

462462
@Deprecated(MAX_NO_SKIPNAN, level = DeprecationLevel.HIDDEN)
463-
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMaxOfOrNull(): T? =
464-
rowMaxOfOrNull(skipNaN = skipNaN_default)
463+
public inline fun <reified T : Comparable<T>> AnyRow.rowMaxOfOrNull(): T? = rowMaxOfOrNull<T>(skipNaN = skipNaN_default)
465464

466465
@Deprecated(MAX_NO_SKIPNAN, level = DeprecationLevel.HIDDEN)
467466
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMaxOf(): T & Any = rowMaxOf(skipNaN = skipNaN_default)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public inline fun <T, reified R : Number?> DataColumn<T>.meanOf(
5050
public fun AnyRow.rowMean(skipNaN: Boolean = skipNaN_default): Double =
5151
Aggregators.mean(skipNaN).aggregateOfRow(this, primitiveOrMixedNumberColumns())
5252

53-
public inline fun <reified T : Number?> AnyRow.rowMeanOf(skipNaN: Boolean = skipNaN_default): Double {
53+
public inline fun <reified T : Number> AnyRow.rowMeanOf(skipNaN: Boolean = skipNaN_default): Double {
5454
require(typeOf<T>().isPrimitiveOrMixedNumber()) {
5555
"Type ${T::class.simpleName} is not a primitive number type. Mean only supports primitive number types."
5656
}
57-
return Aggregators.mean(skipNaN).aggregateOfRow(this) { colsOf<T>() }
57+
return Aggregators.mean(skipNaN).aggregateOfRow(this) { colsOf<T?>() }
5858
}
5959

6060
// endregion
@@ -291,7 +291,7 @@ public inline fun <T, reified R : Number?> DataColumn<T>.meanOf(crossinline expr
291291
public fun AnyRow.rowMean(): Double = rowMean(skipNaN = skipNaN_default)
292292

293293
@Deprecated(MEAN_NO_SKIPNAN, level = DeprecationLevel.HIDDEN)
294-
public inline fun <reified T : Number?> AnyRow.rowMeanOf(): Double = rowMeanOf<T>(skipNaN = skipNaN_default)
294+
public inline fun <reified T : Number> AnyRow.rowMeanOf(): Double = rowMeanOf<T>(skipNaN = skipNaN_default)
295295

296296
@Deprecated(MEAN_NO_SKIPNAN, level = DeprecationLevel.HIDDEN)
297297
public fun <T> DataFrame<T>.mean(): DataRow<T> = mean(skipNaN = skipNaN_default)

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public fun AnyRow.rowMinOrNull(): Any? = error(ROW_MIN_OR_NULL)
6464
@Deprecated(ROW_MIN, level = DeprecationLevel.ERROR)
6565
public fun AnyRow.rowMin(): Any = error(ROW_MIN)
6666

67-
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMinOfOrNull(skipNaN: Boolean = skipNaN_default): T? =
68-
Aggregators.min<T>(skipNaN).aggregateOfRow(this) { colsOf<T>() }
67+
public inline fun <reified T : Comparable<T>> AnyRow.rowMinOfOrNull(skipNaN: Boolean = skipNaN_default): T? =
68+
Aggregators.min<T>(skipNaN).aggregateOfRow(this) { colsOf<T?>() }
6969

70-
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMinOf(skipNaN: Boolean = skipNaN_default): T & Any =
70+
public inline fun <reified T : Comparable<T>> AnyRow.rowMinOf(skipNaN: Boolean = skipNaN_default): T =
7171
rowMinOfOrNull<T>(skipNaN).suggestIfNull("rowMinOf")
7272

7373
// endregion
@@ -460,8 +460,7 @@ public inline fun <T, reified R : Comparable<R & Any>?> DataColumn<T>.minOfOrNul
460460
minOfOrNull(skipNaN = skipNaN_default, selector = selector)
461461

462462
@Deprecated(MIN_NO_SKIPNAN, level = DeprecationLevel.HIDDEN)
463-
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMinOfOrNull(): T? =
464-
rowMinOfOrNull(skipNaN = skipNaN_default)
463+
public inline fun <reified T : Comparable<T>> AnyRow.rowMinOfOrNull(): T? = rowMinOfOrNull<T>(skipNaN = skipNaN_default)
465464

466465
@Deprecated(MIN_NO_SKIPNAN, level = DeprecationLevel.HIDDEN)
467466
public inline fun <reified T : Comparable<T & Any>?> AnyRow.rowMinOf(): T & Any = rowMinOf(skipNaN = skipNaN_default)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import kotlin.reflect.KProperty
2727
import kotlin.reflect.typeOf
2828

2929
/* TODO KDocs:
30-
* Calculating the std is supported for all primitive numebr types.
30+
* Calculating the std is supported for all primitive number types.
3131
* Nulls are filtered out.
3232
* The return type is always Double, Double.NaN for empty input, never null.
3333
* (May introduce loss of precision for Longs).

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import kotlin.experimental.ExperimentalTypeInference
2929
import kotlin.reflect.KClass
3030
import kotlin.reflect.KProperty
3131
import kotlin.reflect.KType
32+
import kotlin.reflect.full.withNullability
3233
import kotlin.reflect.typeOf
3334

3435
/* TODO KDocs
@@ -78,30 +79,36 @@ public inline fun <C, reified V : Number?> DataColumn<C>.sumOf(
7879
public fun AnyRow.rowSum(skipNaN: Boolean = skipNaN_default): Number =
7980
Aggregators.sum(skipNaN).aggregateOfRow(this, primitiveOrMixedNumberColumns())
8081

82+
@Suppress("FINAL_UPPER_BOUND")
8183
@JvmName("rowSumOfShort")
82-
public inline fun <reified T : Short?> AnyRow.rowSumOf(_kClass: KClass<Short> = Short::class): Int =
84+
public inline fun <reified T : Short> AnyRow.rowSumOf(_kClass: KClass<Short> = Short::class): Int =
8385
rowSumOf(typeOf<T>(), false) as Int
8486

87+
@Suppress("FINAL_UPPER_BOUND")
8588
@JvmName("rowSumOfByte")
86-
public inline fun <reified T : Byte?> AnyRow.rowSumOf(_kClass: KClass<Byte> = Byte::class): Int =
89+
public inline fun <reified T : Byte> AnyRow.rowSumOf(_kClass: KClass<Byte> = Byte::class): Int =
8790
rowSumOf(typeOf<T>(), false) as Int
8891

92+
@Suppress("FINAL_UPPER_BOUND")
8993
@JvmName("rowSumOfInt")
90-
public inline fun <reified T : Int?> AnyRow.rowSumOf(_kClass: KClass<Int> = Int::class): Int =
94+
public inline fun <reified T : Int> AnyRow.rowSumOf(_kClass: KClass<Int> = Int::class): Int =
9195
rowSumOf(typeOf<T>(), false) as Int
9296

97+
@Suppress("FINAL_UPPER_BOUND")
9398
@JvmName("rowSumOfLong")
94-
public inline fun <reified T : Long?> AnyRow.rowSumOf(_kClass: KClass<Long> = Long::class): Long =
99+
public inline fun <reified T : Long> AnyRow.rowSumOf(_kClass: KClass<Long> = Long::class): Long =
95100
rowSumOf(typeOf<T>(), false) as Long
96101

102+
@Suppress("FINAL_UPPER_BOUND")
97103
@JvmName("rowSumOfFloat")
98-
public inline fun <reified T : Float?> AnyRow.rowSumOf(
104+
public inline fun <reified T : Float> AnyRow.rowSumOf(
99105
skipNaN: Boolean = skipNaN_default,
100106
_kClass: KClass<Float> = Float::class,
101107
): Float = rowSumOf(typeOf<T>(), skipNaN) as Float
102108

109+
@Suppress("FINAL_UPPER_BOUND")
103110
@JvmName("rowSumOfDouble")
104-
public inline fun <reified T : Double?> AnyRow.rowSumOf(
111+
public inline fun <reified T : Double> AnyRow.rowSumOf(
105112
skipNaN: Boolean = skipNaN_default,
106113
_kClass: KClass<Double> = Double::class,
107114
): Double = rowSumOf(typeOf<T>(), skipNaN) as Double
@@ -111,7 +118,9 @@ public fun AnyRow.rowSumOf(type: KType, skipNaN: Boolean = skipNaN_default): Num
111118
require(type.isPrimitiveOrMixedNumber()) {
112119
"Type $type is not a primitive number type. Mean only supports primitive number types."
113120
}
114-
return Aggregators.sum(skipNaN).aggregateOfRow(this) { colsOf(type) }
121+
return Aggregators.sum(skipNaN).aggregateOfRow(this) {
122+
colsOf(type.withNullability(true))
123+
}
115124
}
116125
// endregion
117126

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/statistics/max.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,11 @@ class MaxTests {
171171

172172
// Find maximum value in each row
173173
df[0].rowMaxOf<Int>() shouldBe 3
174-
df[0].rowMaxOf<Int?>() shouldBe 3 // TODO?
175174

176175
df[1].rowMaxOf<Float>() shouldBe 4f
177176
df[1].rowMaxOf<Int>() shouldBe 6
178-
df[1].rowMaxOf<Int?>() shouldBe 6
179177

180178
df[2].rowMaxOf<Int>() shouldBe 9
181-
df[2].rowMaxOf<Int?>() shouldBe 9 // TODO?
182179
}
183180

184181
@[Test Suppress("ktlint:standard:argument-list-wrapping")]

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/statistics/min.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,12 @@ class MinTests {
170170
)
171171

172172
// Find minimum value in each row
173-
df[0].rowMinOf<Int>() shouldBe 3
174-
df[0].rowMinOf<Int?>() shouldBe 2 // TODO?
173+
df[0].rowMinOf<Int>() shouldBe 2
175174

176175
df[1].rowMinOf<Float>() shouldBe 4f
177176
df[1].rowMinOf<Int>() shouldBe 6
178-
df[1].rowMinOf<Int?>() shouldBe 6
179177

180-
df[2].rowMinOf<Int>() shouldBe 9
181-
df[2].rowMinOf<Int?>() shouldBe 8 // TODO?
178+
df[2].rowMinOf<Int>() shouldBe 8
182179
}
183180

184181
@[Test Suppress("ktlint:standard:argument-list-wrapping")]

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/statistics/sum.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ class SumTests {
161161
(row2.rowSum() as Double).shouldBeNaN()
162162

163163
// Test rowSumOf
164-
row1.rowSumOf<Double?>(skipNaN = true) shouldBe 6.0
165-
row2.rowSumOf<Double?>(skipNaN = true) shouldBe 1.0
166-
row1.rowSumOf<Double?>() shouldBe 6.0
167-
row2.rowSumOf<Double?>().shouldBeNaN()
164+
row1.rowSumOf<Double>(skipNaN = true) shouldBe 6.0
165+
row2.rowSumOf<Double>(skipNaN = true) shouldBe 1.0
166+
row1.rowSumOf<Double>() shouldBe 6.0
167+
row2.rowSumOf<Double>().shouldBeNaN()
168168
}
169169

170170
@Test

0 commit comments

Comments
 (0)