Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -6,10 +6,7 @@ import org.jetbrains.kotlinx.dataframe.api.add
import org.jetbrains.kotlinx.dataframe.api.after
import org.jetbrains.kotlinx.dataframe.api.chunked
import org.jetbrains.kotlinx.dataframe.api.colsOf
import org.jetbrains.kotlinx.dataframe.api.column
import org.jetbrains.kotlinx.dataframe.api.columnOf
import org.jetbrains.kotlinx.dataframe.api.countDistinct
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.dataframe.api.distinct
import org.jetbrains.kotlinx.dataframe.api.distinctBy
import org.jetbrains.kotlinx.dataframe.api.drop
Expand Down Expand Up @@ -37,13 +34,11 @@ import org.jetbrains.kotlinx.dataframe.api.maxByOrNull
import org.jetbrains.kotlinx.dataframe.api.minBy
import org.jetbrains.kotlinx.dataframe.api.minus
import org.jetbrains.kotlinx.dataframe.api.move
import org.jetbrains.kotlinx.dataframe.api.named
import org.jetbrains.kotlinx.dataframe.api.notNull
import org.jetbrains.kotlinx.dataframe.api.remove
import org.jetbrains.kotlinx.dataframe.api.rows
import org.jetbrains.kotlinx.dataframe.api.select
import org.jetbrains.kotlinx.dataframe.api.single
import org.jetbrains.kotlinx.dataframe.api.sortBy
import org.jetbrains.kotlinx.dataframe.api.take
import org.jetbrains.kotlinx.dataframe.api.takeLast
import org.jetbrains.kotlinx.dataframe.api.takeWhile
Expand Down Expand Up @@ -407,37 +402,6 @@ class Access : TestBase() {
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun namedAndRenameCol() {
// SampleStart
val unnamedCol = columnOf("Alice", "Bob")
val colRename = unnamedCol.rename("name")
val colNamed = columnOf("Alice", "Bob") named "name"
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun namedColumnWithoutValues() {
// SampleStart
val name by column<String>()
val col = column<String>("name")
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun colRefForTypedAccess() {
val df = dataFrameOf("name")("Alice", "Bob")
val name by column<String>()
val col = column<String>("name")
// SampleStart
df.filter { it[name].startsWith("A") }
df.sortBy { col }
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun iterableApi() {
Expand Down Expand Up @@ -570,41 +534,6 @@ class Access : TestBase() {
// SampleEnd
}

@Test
fun columnSelectors_kproperties() {
// SampleStart
// by column name
df.select { it[Person::name] }
df.select { (Person::name)() }
df.select { col(Person::name) }

// by column path
df.select { it[Person::name][Name::firstName] }
df.select { Person::name[Name::firstName] }

// with a new name
df.select { Person::name named "Full Name" }

// converted
df.select { Person::name[Name::firstName].map { it.lowercase() } }

// column arithmetics
df.select { 2021 - (Person::age)() }

// two columns
df.select { Person::name and Person::age }

// range of columns
df.select { Person::name..Person::age }

// all columns of ColumnGroup
df.select { Person::name.allCols() }

// traversal of columns at any depth from here excluding ColumnGroups
df.select { Person::name.colsAtAnyDepth().filter { !it.isColumnGroup() } }
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun columnSelectors_strings() {
Expand Down Expand Up @@ -688,19 +617,14 @@ class Access : TestBase() {
colGroup("name").lastCol { it.name().endsWith("Name") }
}

// find the single column inside a column group satisfying the condition
df.select {
Person::name.singleCol { it.name().startsWith("first") }
}

// traversal of columns at any depth from here excluding ColumnGroups
df.select { colsAtAnyDepth().filter { !it.isColumnGroup() } }

// traversal of columns at any depth from here including ColumnGroups
df.select { colsAtAnyDepth() }

// traversal of columns at any depth with condition
df.select { colsAtAnyDepth().filter() { it.name().contains(":") } }
df.select { colsAtAnyDepth().filter { it.name().contains(":") } }

// traversal of columns at any depth to find columns of given type
df.select { colsAtAnyDepth().colsOf<String>() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.jetbrains.kotlinx.dataframe.api.asGroupBy
import org.jetbrains.kotlinx.dataframe.api.asNumbers
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.colsOf
import org.jetbrains.kotlinx.dataframe.api.column
import org.jetbrains.kotlinx.dataframe.api.columnGroup
import org.jetbrains.kotlinx.dataframe.api.columnOf
import org.jetbrains.kotlinx.dataframe.api.concat
import org.jetbrains.kotlinx.dataframe.api.count
Expand Down Expand Up @@ -445,31 +443,6 @@ class Analyze : TestBase() {
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun columnsFor_accessors() {
// SampleStart
val name by columnGroup()
val firstName by name.column<String>()
val lastName by name.column<String>()
val age by column<Int>()
val weight by column<Int?>()

df.minFor { colsOf<Int>() }

df.maxFor { firstName and age }
// or
df.maxFor(firstName, lastName)

df.sumFor { age and weight }
// or
df.sum(age, weight)

df.mean { cols(1, 3).asNumbers() }
df.median<_, String> { name.allCols().cast() }
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun columnsFor_strings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
package org.jetbrains.kotlinx.dataframe.samples.api

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.add
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.column
import org.jetbrains.kotlinx.dataframe.api.dropNulls
import org.jetbrains.kotlinx.dataframe.api.filter
import org.jetbrains.kotlinx.dataframe.api.toListOf
import org.jetbrains.kotlinx.dataframe.explainer.TransformDataFrameExpressions
import org.jetbrains.kotlinx.dataframe.io.read
import org.junit.Ignore
Expand All @@ -34,91 +31,6 @@ class ApiLevels {
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun accessors1() {
// SampleStart
val survived by column<Boolean>() // accessor for Boolean column with name 'survived'
val home by column<String>()
val age by column<Int?>()
val name by column<String>()
val lastName by column<String>()
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun accessors2() {
val survived by column<Boolean>()
val home by column<String>()
val age by column<Int?>()
val name by column<String>()
val lastName by column<String>()
// SampleStart

DataFrame.read("titanic.csv")
.add(lastName) { name().split(",").last() }
.dropNulls { age }
.filter { survived() && home().endsWith("NY") && age()!! in 10..20 }
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun accessors3() {
// SampleStart
val survived by column<Boolean>()
val home by column<String>()
val age by column<Int?>()
val name by column<String>()
val lastName by column<String>()

DataFrame.read("titanic.csv")
.add(lastName) { name().split(",").last() }
.dropNulls { age }
.filter { survived() && home().endsWith("NY") && age()!! in 10..20 }
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun kproperties1() {
// SampleStart
data class Passenger(
val survived: Boolean,
val home: String,
val age: Int,
val lastName: String
)

val passengers = DataFrame.read("titanic.csv")
.add(Passenger::lastName) { "name"<String>().split(",").last() }
.dropNulls(Passenger::age)
.filter {
it[Passenger::survived] &&
it[Passenger::home].endsWith("NY") &&
it[Passenger::age] in 10..20
}
.toListOf<Passenger>()
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun kproperties2() {
// SampleStart
data class Passenger(
@ColumnName("survived") val isAlive: Boolean,
@ColumnName("home") val city: String,
val name: String
)

val passengers = DataFrame.read("titanic.csv")
.filter { it[Passenger::city].endsWith("NY") }
.toListOf<Passenger>()
// SampleEnd
}

@DataSchema
interface TitanicPassenger {
val survived: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,6 @@ class Create : TestBase() {
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun columnAccessorsUsage() {
// SampleStart
val age by column<Int>()

// Access fourth cell in the "age" column of dataframe `df`.
// This expression returns `Int` because variable `age` has `ColumnAccessor<Int>` type.
// If dataframe `df` has no column "age" or column "age" has type which is incompatible with `Int`,
// runtime exception will be thrown.
df[age][3] + 5

// Access first cell in the "age" column of dataframe `df`.
df[0][age] * 2

// Returns new dataframe sorted by age column (ascending)
df.sortBy(age)

// Returns new dataframe with the column "year of birth" added
df.add("year of birth") { 2021 - age }

// Returns new dataframe containing only rows with age > 30
df.filter { age > 30 }
// SampleEnd
}

@Test
@TransformDataFrameExpressions
fun columnAccessorMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class JoinWith : TestBase() {
cellRenderer = renderer,
configuration = SamplesDisplayConfiguration.copy(
cellFormatter = { row, col ->
val value = row[col]
val value = col[row]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xD

if (value is ColoredValue<*>) {
background(value.backgroundColor) and textColor(value.textColor)
} else {
Expand Down
Loading
Loading