@@ -4,6 +4,7 @@ package org.jetbrains.kotlinx.dataframe.samples.api
44
55import io.kotest.matchers.shouldBe
66import org.jetbrains.kotlinx.dataframe.AnyFrame
7+ import org.jetbrains.kotlinx.dataframe.DataColumn
78import org.jetbrains.kotlinx.dataframe.DataFrame
89import org.jetbrains.kotlinx.dataframe.api.DynamicDataFrameBuilder
910import org.jetbrains.kotlinx.dataframe.api.Infer
@@ -25,13 +26,11 @@ import org.jetbrains.kotlinx.dataframe.api.toColumn
2526import org.jetbrains.kotlinx.dataframe.api.toColumnOf
2627import org.jetbrains.kotlinx.dataframe.api.toDataFrame
2728import org.jetbrains.kotlinx.dataframe.api.value
28- import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
2929import org.jetbrains.kotlinx.dataframe.explainer.TransformDataFrameExpressions
30- import org.jetbrains.kotlinx.dataframe.kind
31- import org.jetbrains.kotlinx.dataframe.type
3230import org.junit.Test
3331import java.io.File
34- import kotlin.reflect.typeOf
32+ import kotlin.random.nextInt
33+ import kotlin.random.Random as KotlinRandom
3534
3635class Create : TestBase () {
3736
@@ -222,6 +221,70 @@ class Create : TestBase() {
222221 // SampleEnd
223222 }
224223
224+ @Test
225+ @TransformDataFrameExpressions
226+ fun createRandomDataFrame () {
227+ // stable random + clean examples
228+ @Suppress(" LocalVariableName" )
229+ val Random = KotlinRandom (42 )
230+ fun <T > List<T>.random () = this .random(Random )
231+ // SampleStart
232+ val categories = listOf (" Electronics" , " Books" , " Clothing" )
233+ // DataFrame with 4 columns and 7 rows
234+ (0 until 7 ).toDataFrame {
235+ " productId" from { " P${1000 + it} " }
236+ " category" from { categories.random() }
237+ " price" from { Random .nextDouble(10.0 , 500.0 ) }
238+ " inStock" from { Random .nextInt(0 .. 100 ) }
239+ }
240+ // SampleEnd
241+ }
242+
243+ @Test
244+ @TransformDataFrameExpressions
245+ fun createNestedRandomDataFrame () {
246+ // stable random + clean examples
247+ @Suppress(" LocalVariableName" )
248+ val Random = KotlinRandom (42 )
249+ fun <T > List<T>.random () = this .random(Random )
250+ // SampleStart
251+ val categories = listOf (" Electronics" , " Books" , " Clothing" )
252+ // DataFrame with 5 columns and 7 rows
253+ (0 until 7 ).toDataFrame {
254+ " productId" from { " P${1000 + it} " }
255+ " category" from { categories.random() }
256+ " price" from { Random .nextDouble(10.0 , 500.0 ) }
257+
258+ // Column Group
259+ " manufacturer" {
260+ " country" from { listOf (" USA" , " China" , " Germany" , " Japan" ).random() }
261+ " yearEstablished" from { Random .nextInt(1950 .. 2020 ) }
262+ }
263+
264+ // Frame Column
265+ " reviews" from {
266+ val reviewCount = Random .nextInt(0 .. 7 )
267+ (0 until reviewCount).toDataFrame {
268+ val ratings: DataColumn <Int > = expr { Random .nextInt(1 .. 5 ) }
269+ val comments = ratings.map {
270+ when (it) {
271+ 5 -> listOf (" Amazing quality!" , " Best purchase ever!" , " Highly recommend!" , " Absolutely perfect!" )
272+ 4 -> listOf (" Great product!" , " Very satisfied" , " Good value for money" , " Would buy again" )
273+ 3 -> listOf (" It's okay" , " Does the job" , " Average quality" , " Neither good nor bad" )
274+ 2 -> listOf (" Could be better" , " Disappointed" , " Not what I expected" , " Poor quality" )
275+ else -> listOf (" Terrible!" , " Not worth the price" , " Complete waste of money" , " Do not buy!" )
276+ }.random()
277+ }
278+
279+ " author" from { " User${Random .nextInt(1000 .. 10000 )} " }
280+ ratings into " rating"
281+ comments into " comment"
282+ }
283+ }
284+ }
285+ // SampleEnd
286+ }
287+
225288 @Test
226289 @TransformDataFrameExpressions
227290 fun createDataFrameOfPairs () {
@@ -254,7 +317,11 @@ class Create : TestBase() {
254317 fun createDataFrameWithFill () {
255318 // SampleStart
256319 // Multiplication table
257- dataFrameOf(1 .. 10 ) { x -> (1 .. 10 ).map { x * it } }
320+ (1 .. 10 ).toDataFrame {
321+ (1 .. 10 ).forEach { x ->
322+ " $x " from { x * it }
323+ }
324+ }
258325 // SampleEnd
259326 }
260327
@@ -330,10 +397,6 @@ class Create : TestBase() {
330397
331398 val df = persons.toDataFrame()
332399 // 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 >()
337400 }
338401
339402 @Test
@@ -353,11 +416,6 @@ class Create : TestBase() {
353416
354417 val df = students.toDataFrame(maxDepth = 1 )
355418 // SampleEnd
356- df.columnsCount() shouldBe 3
357- df.rowsCount() shouldBe 2
358- df[" name" ].kind shouldBe ColumnKind .Group
359- df[" name" ][" firstName" ].type() shouldBe typeOf<String >()
360- df[" scores" ].kind shouldBe ColumnKind .Frame
361419 }
362420
363421 @Test
@@ -392,12 +450,6 @@ class Create : TestBase() {
392450 }
393451 }
394452 // SampleEnd
395- df.columnsCount() shouldBe 5
396- df.rowsCount() shouldBe 2
397- df[" name" ].kind shouldBe ColumnKind .Value
398- df[" name" ].type shouldBe typeOf<Name >()
399- df[" scores" ].kind shouldBe ColumnKind .Frame
400- df[" summary" ][" min score" ].values() shouldBe listOf (3 , 5 )
401453 }
402454
403455 @Test
0 commit comments