Skip to content

Commit 4cb86ae

Browse files
committed
Update createDataFrame.md with compiler-plugin friendly examples
1 parent a34302f commit 4cb86ae

7 files changed

+2487
-31
lines changed

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

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package org.jetbrains.kotlinx.dataframe.samples.api
44

55
import io.kotest.matchers.shouldBe
66
import org.jetbrains.kotlinx.dataframe.AnyFrame
7+
import org.jetbrains.kotlinx.dataframe.DataColumn
78
import org.jetbrains.kotlinx.dataframe.DataFrame
89
import org.jetbrains.kotlinx.dataframe.api.DynamicDataFrameBuilder
910
import org.jetbrains.kotlinx.dataframe.api.Infer
@@ -31,6 +32,7 @@ import org.jetbrains.kotlinx.dataframe.kind
3132
import org.jetbrains.kotlinx.dataframe.type
3233
import org.junit.Test
3334
import java.io.File
35+
import kotlin.random.Random as KotlinRandom
3436
import kotlin.reflect.typeOf
3537

3638
class Create : TestBase() {
@@ -222,6 +224,66 @@ class Create : TestBase() {
222224
// SampleEnd
223225
}
224226

227+
228+
@Suppress("PrivatePropertyName")
229+
private val Random: KotlinRandom = KotlinRandom(42)
230+
231+
@Test
232+
@TransformDataFrameExpressions
233+
fun createRandomDataFrame() {
234+
// SampleStart
235+
val categories = listOf("Electronics", "Books", "Clothing")
236+
// DataFrame with 4 columns and 7 rows
237+
(0 until 7).toDataFrame {
238+
"productId" from { "P${1000 + it}" }
239+
"category" from { categories.random() }
240+
"price" from { Random.nextDouble(10.0, 500.0) }
241+
"inStock" from { Random.nextInt(0, 100) }
242+
}
243+
// SampleEnd
244+
}
245+
246+
@Test
247+
@TransformDataFrameExpressions
248+
fun createNestedRandomDataFrame() {
249+
// SampleStart
250+
val categories = listOf("Electronics", "Books", "Clothing")
251+
// DataFrame with 5 columns and 7 rows
252+
(0 until 7).toDataFrame {
253+
"productId" from { "P${1000 + it}" }
254+
"category" from { categories.random() }
255+
"price" from { Random.nextDouble(10.0, 500.0) }
256+
257+
// Column Group
258+
"manufacturer" {
259+
"country" from { listOf("USA", "China", "Germany", "Japan").random() }
260+
"yearEstablished" from { Random.nextInt(1950, 2020) }
261+
}
262+
263+
// Frame Column
264+
"reviews" from {
265+
val reviewCount = Random.nextInt(0, 8)
266+
(0 until reviewCount).toDataFrame {
267+
val ratings: DataColumn<Int> = expr { Random.nextInt(1, 6) }
268+
val comments = ratings.map {
269+
when (it) {
270+
5 -> listOf("Amazing quality!", "Best purchase ever!", "Highly recommend!", "Absolutely perfect!")
271+
4 -> listOf("Great product!", "Very satisfied", "Good value for money", "Would buy again")
272+
3 -> listOf("It's okay", "Does the job", "Average quality", "Neither good nor bad")
273+
2 -> listOf("Could be better", "Disappointed", "Not what I expected", "Poor quality")
274+
else -> listOf("Terrible!", "Not worth the price", "Complete waste of money", "Do not buy!")
275+
}.random()
276+
}
277+
278+
"author" from { "User${Random.nextInt(1000, 9999)}" }
279+
ratings into "rating"
280+
comments into "comment"
281+
}
282+
}
283+
}
284+
// SampleEnd
285+
}
286+
225287
@Test
226288
@TransformDataFrameExpressions
227289
fun createDataFrameOfPairs() {
@@ -254,7 +316,11 @@ class Create : TestBase() {
254316
fun createDataFrameWithFill() {
255317
// SampleStart
256318
// Multiplication table
257-
dataFrameOf(1..10) { x -> (1..10).map { x * it } }
319+
(1..10).toDataFrame {
320+
(1..10).forEach { x ->
321+
"$x" from { x * it }
322+
}
323+
}
258324
// SampleEnd
259325
}
260326

@@ -330,10 +396,6 @@ class Create : TestBase() {
330396

331397
val df = persons.toDataFrame()
332398
// SampleEnd
333-
df.columnsCount() shouldBe 2
334-
df.rowsCount() shouldBe 3
335-
df["name"].type() shouldBe typeOf<String>()
336-
df["age"].type() shouldBe typeOf<Int>()
337399
}
338400

339401
@Test

0 commit comments

Comments
 (0)