Skip to content

Commit 94b4e8a

Browse files
committed
Rename depth into maxDepth in toDataFrame and count from 0 instead of 1. Fix bug in implementation.
1 parent 2f1d380 commit 94b4e8a

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import kotlin.reflect.KProperty
2828
// region read DataFrame from objects
2929

3030
public inline fun <reified T> Iterable<T>.toDataFrame(): DataFrame<T> = toDataFrame {
31-
properties(depth = 1)
31+
properties()
3232
}
3333

3434
public inline fun <reified T> Iterable<T>.toDataFrame(noinline body: CreateDataFrameDsl<T>.() -> Unit): DataFrame<T> = createDataFrameImpl(T::class, body)
3535

36-
public inline fun <reified T> Iterable<T>.toDataFrame(vararg props: KProperty<*>, depth: Int = 1): DataFrame<T> =
36+
public inline fun <reified T> Iterable<T>.toDataFrame(vararg props: KProperty<*>, maxDepth: Int = 0): DataFrame<T> =
3737
toDataFrame {
38-
properties(roots = props, depth = depth)
38+
properties(roots = props, maxDepth = maxDepth)
3939
}
4040

4141
public inline fun <reified T> DataColumn<T>.read(): AnyCol = when (kind()) {
@@ -45,7 +45,7 @@ public inline fun <reified T> DataColumn<T>.read(): AnyCol = when (kind()) {
4545
typeClass == File::class -> cast<File?>().mapNotNullValues { DataFrame.read(it) }
4646
typeClass == URL::class -> cast<URL?>().mapNotNullValues { DataFrame.read(it) }
4747
else -> values().createDataFrameImpl(typeClass) {
48-
(this as CreateDataFrameDsl<T>).properties(depth = 1)
48+
(this as CreateDataFrameDsl<T>).properties()
4949
}.asColumnGroup(name()).asDataColumn()
5050
}
5151
}
@@ -172,7 +172,7 @@ public abstract class CreateDataFrameDsl<T> : TraversePropertiesDsl {
172172

173173
public abstract fun properties(
174174
vararg roots: KProperty<*>,
175-
depth: Int = 1,
175+
maxDepth: Int = 0,
176176
body: (TraversePropertiesDsl.() -> Unit)? = null
177177
)
178178

src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/toDataFrame.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ internal class CreateDataFrameDslImpl<T>(
100100
}
101101
}
102102

103-
override fun properties(vararg roots: KProperty<*>, depth: Int, body: (TraversePropertiesDsl.() -> Unit)?) {
103+
override fun properties(vararg roots: KProperty<*>, maxDepth: Int, body: (TraversePropertiesDsl.() -> Unit)?) {
104104
val dsl = configuration.clone()
105105
if (body != null) {
106106
body(dsl)
107107
}
108-
val df = convertToDataFrame(source, clazz, roots.toList(), dsl.excludeProperties, dsl.preserveClasses, dsl.preserveProperties, depth)
108+
val df = convertToDataFrame(source, clazz, roots.toList(), dsl.excludeProperties, dsl.preserveClasses, dsl.preserveProperties, maxDepth)
109109
df.columns().forEach {
110110
add(it)
111111
}
@@ -127,7 +127,7 @@ internal fun convertToDataFrame(
127127
excludes: Set<KProperty<*>>,
128128
preserveClasses: Set<KClass<*>>,
129129
preserveProperties: Set<KProperty<*>>,
130-
depth: Int
130+
maxDepth: Int
131131
): AnyFrame {
132132
val order = getPropertiesOrder(clazz)
133133

@@ -167,7 +167,7 @@ internal fun convertToDataFrame(
167167
val kclass = (type.classifier as KClass<*>)
168168
when {
169169
hasExceptions -> DataColumn.createWithTypeInference(it.columnName, values, nullable)
170-
preserveClasses.contains(kclass) || preserveProperties.contains(property) || (depth == 1 && !type.shouldBeConvertedToFrameColumn() && !type.shouldBeConvertedToColumnGroup()) || kclass.isValueType ->
170+
preserveClasses.contains(kclass) || preserveProperties.contains(property) || (maxDepth <= 0 && !type.shouldBeConvertedToFrameColumn() && !type.shouldBeConvertedToColumnGroup()) || kclass.isValueType ->
171171
DataColumn.createValueColumn(it.columnName, values, property.returnType.withNullability(nullable))
172172
kclass == DataFrame::class && !nullable -> DataColumn.createFrameColumn(it.columnName, values as List<AnyFrame>)
173173
kclass == DataRow::class -> DataColumn.createColumnGroup(it.columnName, (values as List<AnyRow>).concat())
@@ -191,15 +191,15 @@ internal fun convertToDataFrame(
191191
if (it == null) DataFrame.empty()
192192
else {
193193
require(it is Iterable<*>)
194-
convertToDataFrame(it, elementClass, emptyList(), excludes, preserveClasses, preserveProperties, depth - 1)
194+
convertToDataFrame(it, elementClass, emptyList(), excludes, preserveClasses, preserveProperties, maxDepth - 1)
195195
}
196196
}
197197
DataColumn.createFrameColumn(it.columnName, frames)
198198
}
199199
}
200200
}
201201
else -> {
202-
val df = convertToDataFrame(values, kclass, emptyList(), excludes, preserveClasses, preserveProperties, depth - 1)
202+
val df = convertToDataFrame(values, kclass, emptyList(), excludes, preserveClasses, preserveProperties, maxDepth - 1)
203203
DataColumn.createColumnGroup(it.columnName, df)
204204
}
205205
}

src/test/kotlin/org/jetbrains/kotlinx/dataframe/rendering/html/Browsing.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Browsing {
2424
val df = students.toDataFrame {
2525
"year of birth" from { 2021 - it.age }
2626

27-
properties(depth = 2) {
27+
properties(maxDepth = 1) {
2828
// exclude(Score::subject) // `subject` property will be skipped from object graph traversal
2929
// preserve<Name>() // `Name` objects will be stored as-is without transformation into DataFrame
3030
}

src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ class DataFrameTests : BaseTest() {
22022202
val list = grouped.toListOf<Target>()
22032203
list shouldBe grouped.convertTo<Target>().toList()
22042204

2205-
val listDf = list.toDataFrame(depth = 2)
2205+
val listDf = list.toDataFrame(maxDepth = 2)
22062206
listDf shouldBe grouped.update { getFrameColumn("students") }.with { it.remove("city") }
22072207

22082208
listDf.toList() shouldBe list
@@ -2229,7 +2229,7 @@ class DataFrameTests : BaseTest() {
22292229
val list = grouped.toListOf<Target>()
22302230
list shouldBe grouped.convertTo<Target>().toList()
22312231

2232-
val listDf = list.toDataFrame(depth = 2)
2232+
val listDf = list.toDataFrame(maxDepth = 2)
22332233
listDf shouldBe grouped
22342234
listDf.toList() shouldBe list
22352235
}

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CreateDataFrameTests {
111111
fun `don't convert value types`() {
112112
data class Entry(val a: Int, val b: String, val c: Boolean, val e: DummyEnum)
113113

114-
val df = listOf(Entry(1, "s", true, DummyEnum.A)).toDataFrame(depth = 100)
114+
val df = listOf(Entry(1, "s", true, DummyEnum.A)).toDataFrame(maxDepth = 100)
115115
df.columns().forEach {
116116
it.kind shouldBe ColumnKind.Value
117117
}
@@ -122,11 +122,31 @@ class CreateDataFrameTests {
122122
class Child
123123
class Entry(val a: Int, val child: Child)
124124

125-
val df = listOf(Entry(1, Child())).toDataFrame(depth = 100)
125+
val df = listOf(Entry(1, Child())).toDataFrame(maxDepth = 100)
126126
df.rowsCount() shouldBe 1
127127

128128
val childCol = df[Entry::child]
129129
childCol.kind() shouldBe ColumnKind.Group
130130
childCol.asColumnGroup().columnsCount() shouldBe 0
131131
}
132+
133+
@Test
134+
fun `convert child schemas`() {
135+
class Child2(val s: String)
136+
137+
@DataSchema
138+
class Child1(val child: Child2)
139+
140+
@DataSchema
141+
class Entry(val a: Int, val child: Child1)
142+
143+
val df = listOf(Entry(1, Child1(Child2("s")))).toDataFrame()
144+
df.rowsCount() shouldBe 1
145+
146+
val child1 = df[Entry::child]
147+
child1.kind shouldBe ColumnKind.Group
148+
149+
val child2 = child1.asColumnGroup()[Child1::child]
150+
child2.kind shouldBe ColumnKind.Value
151+
}
132152
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class Create : TestBase() {
331331
Student(Name("Bob", "Marley"), 20, listOf(Score("music", 5)))
332332
)
333333

334-
val df = students.toDataFrame(depth = 2)
334+
val df = students.toDataFrame(maxDepth = 1)
335335
// SampleEnd
336336
df.columnsCount() shouldBe 3
337337
df.rowsCount() shouldBe 2
@@ -357,7 +357,7 @@ class Create : TestBase() {
357357
"year of birth" from { 2021 - it.age }
358358

359359
// scan all properties
360-
properties(depth = 2) {
360+
properties(maxDepth = 1) {
361361
exclude(Score::subject) // `subject` property will be skipped from object graph traversal
362362
preserve<Name>() // `Name` objects will be stored as-is without transformation into DataFrame
363363
}

0 commit comments

Comments
 (0)