@@ -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
@@ -31,6 +32,7 @@ import org.jetbrains.kotlinx.dataframe.kind
3132import org.jetbrains.kotlinx.dataframe.type
3233import org.junit.Test
3334import java.io.File
35+ import kotlin.random.Random as KotlinRandom
3436import kotlin.reflect.typeOf
3537
3638class Create : TestBase () {
@@ -222,6 +224,70 @@ class Create : TestBase() {
222224 // SampleEnd
223225 }
224226
227+ @Test
228+ @TransformDataFrameExpressions
229+ fun createRandomDataFrame () {
230+ // stable random + clean examples
231+ @Suppress(" LocalVariableName" )
232+ val Random = KotlinRandom (42 )
233+ fun <T > List<T>.random () = this .random(Random )
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+ // stable random + clean examples
250+ @Suppress(" LocalVariableName" )
251+ val Random = KotlinRandom (42 )
252+ fun <T > List<T>.random () = this .random(Random )
253+ // SampleStart
254+ val categories = listOf (" Electronics" , " Books" , " Clothing" )
255+ // DataFrame with 5 columns and 7 rows
256+ (0 until 7 ).toDataFrame {
257+ " productId" from { " P${1000 + it} " }
258+ " category" from { categories.random() }
259+ " price" from { Random .nextDouble(10.0 , 500.0 ) }
260+
261+ // Column Group
262+ " manufacturer" {
263+ " country" from { listOf (" USA" , " China" , " Germany" , " Japan" ).random() }
264+ " yearEstablished" from { Random .nextInt(1950 , 2020 ) }
265+ }
266+
267+ // Frame Column
268+ " reviews" from {
269+ val reviewCount = Random .nextInt(0 , 8 )
270+ (0 until reviewCount).toDataFrame {
271+ val ratings: DataColumn <Int > = expr { Random .nextInt(1 , 6 ) }
272+ val comments = ratings.map {
273+ when (it) {
274+ 5 -> listOf (" Amazing quality!" , " Best purchase ever!" , " Highly recommend!" , " Absolutely perfect!" )
275+ 4 -> listOf (" Great product!" , " Very satisfied" , " Good value for money" , " Would buy again" )
276+ 3 -> listOf (" It's okay" , " Does the job" , " Average quality" , " Neither good nor bad" )
277+ 2 -> listOf (" Could be better" , " Disappointed" , " Not what I expected" , " Poor quality" )
278+ else -> listOf (" Terrible!" , " Not worth the price" , " Complete waste of money" , " Do not buy!" )
279+ }.random()
280+ }
281+
282+ " author" from { " User${Random .nextInt(1000 , 9999 )} " }
283+ ratings into " rating"
284+ comments into " comment"
285+ }
286+ }
287+ }
288+ // SampleEnd
289+ }
290+
225291 @Test
226292 @TransformDataFrameExpressions
227293 fun createDataFrameOfPairs () {
@@ -254,7 +320,11 @@ class Create : TestBase() {
254320 fun createDataFrameWithFill () {
255321 // SampleStart
256322 // Multiplication table
257- dataFrameOf(1 .. 10 ) { x -> (1 .. 10 ).map { x * it } }
323+ (1 .. 10 ).toDataFrame {
324+ (1 .. 10 ).forEach { x ->
325+ " $x " from { x * it }
326+ }
327+ }
258328 // SampleEnd
259329 }
260330
@@ -330,10 +400,6 @@ class Create : TestBase() {
330400
331401 val df = persons.toDataFrame()
332402 // 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 >()
337403 }
338404
339405 @Test
0 commit comments