Skip to content

Commit 56b76f4

Browse files
authored
Merge pull request #1462 from CarloMariaProietti/add_none_to_df
adding `.none {}` to `DataFrame`
2 parents 5cf90d6 + 6748efd commit 56b76f4

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

core/api/core.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3471,6 +3471,11 @@ public abstract interface class org/jetbrains/kotlinx/dataframe/api/NoneColumnsS
34713471
public abstract interface class org/jetbrains/kotlinx/dataframe/api/NoneColumnsSelectionDsl$Grammar$PlainDslName {
34723472
}
34733473

3474+
public final class org/jetbrains/kotlinx/dataframe/api/NoneKt {
3475+
public static final fun none (Lorg/jetbrains/kotlinx/dataframe/DataColumn;Lkotlin/jvm/functions/Function1;)Z
3476+
public static final fun none (Lorg/jetbrains/kotlinx/dataframe/DataFrame;Lkotlin/jvm/functions/Function2;)Z
3477+
}
3478+
34743479
public final class org/jetbrains/kotlinx/dataframe/api/NullabilityException : java/lang/Exception {
34753480
public fun <init> ()V
34763481
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
package org.jetbrains.kotlinx.dataframe.api
22

3+
import org.jetbrains.kotlinx.dataframe.DataColumn
34
import org.jetbrains.kotlinx.dataframe.DataFrame
5+
import org.jetbrains.kotlinx.dataframe.DataRow
6+
import org.jetbrains.kotlinx.dataframe.Predicate
7+
import org.jetbrains.kotlinx.dataframe.RowFilter
48
import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
59
import org.jetbrains.kotlinx.dataframe.columns.ColumnsResolver
10+
import org.jetbrains.kotlinx.dataframe.columns.values
611
import org.jetbrains.kotlinx.dataframe.documentation.DslGrammarTemplateColumnsSelectionDsl.DslGrammarTemplate
712
import org.jetbrains.kotlinx.dataframe.impl.columns.ColumnListImpl
813

14+
// region DataColumn
15+
16+
/** Returns `true` if none of the [values] match the given [predicate] */
17+
public fun <T> DataColumn<T>.none(predicate: Predicate<T>): Boolean = values.none(predicate)
18+
19+
// endregion
20+
21+
// region DataFrame
22+
23+
/**
24+
* Returns `true` if none of the rows in this [DataFrame] satisfies the given [predicate].
25+
*
26+
* {@include [org.jetbrains.kotlinx.dataframe.documentation.RowFilterDescription]}
27+
*
28+
* ### Example
29+
* ```kotlin
30+
* // Check if there is not any row where "age" is greater than 18
31+
* val hasNoAdults = df.none { age > 18 }
32+
* ```
33+
*
34+
* @param predicate A [RowFilter] lambda that takes a [DataRow] (as both `this` and `it`)
35+
* and returns `true` if none of the rows should be considered a match.
36+
* @return `true` if none of the rows satisfies the [predicate], `false` otherwise.
37+
* @see [DataFrame.any]
38+
*/
39+
public inline fun <T> DataFrame<T>.none(predicate: RowFilter<T>): Boolean = rows().none { predicate(it, it) }
40+
41+
// endregion
42+
943
// region ColumnsSelectionDsl
1044

1145
/**

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,25 @@ class UtilFunctionsTest : TestBase() {
1818
ageCol.any { it > 90 } shouldBe false
1919
}
2020

21+
@Test
22+
fun `DataColumn none`() {
23+
val ageCol = df["age"] as DataColumn<Int>
24+
ageCol.none { it > 40 } shouldBe false
25+
ageCol.none { it > 90 } shouldBe true
26+
}
27+
2128
@Test
2229
fun `DataFrame any`() {
2330
df.any { "age"<Int>() > 40 && "isHappy"<Boolean>() } shouldBe true
2431
df.any { "city"<String?>() == "Berlin" } shouldBe false
2532
}
2633

34+
@Test
35+
fun `DataFrame none`() {
36+
df.none { "age"<Int>() > 40 && "isHappy"<Boolean>() } shouldBe false
37+
df.none { "city"<String?>() == "Berlin" } shouldBe true
38+
}
39+
2740
@Test
2841
fun `DataColumn between`() {
2942
val ages = listOf(15, 45, 20, 40, 30, 20, 30)

0 commit comments

Comments
 (0)