File tree Expand file tree Collapse file tree 8 files changed +66
-8
lines changed
main/kotlin/org/jetbrains/kotlinx/dataframe/api
test/kotlin/org/jetbrains/kotlinx/dataframe
main/kotlin/org/jetbrains/kotlinx/dataframe/api
test/kotlin/org/jetbrains/kotlinx/dataframe Expand file tree Collapse file tree 8 files changed +66
-8
lines changed Original file line number Diff line number Diff line change @@ -174,8 +174,8 @@ public class AddDsl<T>(@PublishedApi internal val df: DataFrame<T>) : ColumnsCon
174
174
noinline expression : RowExpression <T , R >
175
175
): Boolean = add(df.mapToColumn(name, infer, expression))
176
176
177
- public inline fun <reified R > expr (noinline expression : RowExpression <T , R >): DataColumn <R > {
178
- return df.mapToColumn(" " , Infer . Nulls , expression)
177
+ public inline fun <reified R > expr (infer : Infer = Infer . Nulls , noinline expression : RowExpression <T , R >): DataColumn <R > {
178
+ return df.mapToColumn(" " , infer , expression)
179
179
}
180
180
181
181
public inline infix fun <reified R > String.from (noinline expression : RowExpression <T , R >): Boolean =
Original file line number Diff line number Diff line change @@ -153,8 +153,8 @@ public abstract class CreateDataFrameDsl<T> : TraversePropertiesDsl {
153
153
body : (TraversePropertiesDsl .() -> Unit )? = null,
154
154
)
155
155
156
- public inline fun <reified R > expr (noinline expression : (T ) -> R ): DataColumn <R > =
157
- source.map { expression(it) }.toColumn()
156
+ public inline fun <reified R > expr (infer : Infer = Infer . Nulls , noinline expression : (T ) -> R ): DataColumn <R > =
157
+ source.map { expression(it) }.toColumn(infer = infer )
158
158
159
159
public inline fun <reified R > add (name : String , noinline expression : (T ) -> R ): Unit =
160
160
add(source.map { expression(it) }.toColumn(name, Infer .Nulls ))
@@ -165,6 +165,9 @@ public abstract class CreateDataFrameDsl<T> : TraversePropertiesDsl {
165
165
public inline infix fun <reified R > KProperty<R>.from (noinline expression : (T ) -> R ): Unit =
166
166
add(columnName, expression)
167
167
168
+ public inline infix fun <reified R > String.from (inferType : InferType <T , R >): Unit =
169
+ add(DataColumn .createWithTypeInference(this , source.map { inferType.expression(it) }))
170
+
168
171
public inline infix fun <reified R > KProperty<R>.from (inferType : InferType <T , R >): Unit =
169
172
add(DataColumn .createWithTypeInference(columnName, source.map { inferType.expression(it) }))
170
173
Original file line number Diff line number Diff line change @@ -60,6 +60,21 @@ class CreateDataFrameTests {
60
60
res[" e" ].type() shouldBe typeOf<Int >()
61
61
}
62
62
63
+ @Test
64
+ fun `create column with infer type` () {
65
+ val data: List <Any > = listOf (1 , 2 , 3 )
66
+ val res = data.toDataFrame {
67
+ " e" from inferType { it }
68
+ expr(infer = Infer .Type ) { it } into " d"
69
+ }
70
+
71
+ res[" e" ].type() shouldBe typeOf<Int >()
72
+ res[" e" ].kind() shouldBe ColumnKind .Value
73
+
74
+ res[" d" ].type() shouldBe typeOf<Int >()
75
+ res[" d" ].kind() shouldBe ColumnKind .Value
76
+ }
77
+
63
78
@Test
64
79
fun `preserve fields order` () {
65
80
class B (val x : Int , val c : String , d : Double ) {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
15
15
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
16
16
import org.jetbrains.kotlinx.dataframe.api.ExcessiveColumns
17
17
import org.jetbrains.kotlinx.dataframe.api.GroupBy
18
+ import org.jetbrains.kotlinx.dataframe.api.Infer
18
19
import org.jetbrains.kotlinx.dataframe.api.ParserOptions
19
20
import org.jetbrains.kotlinx.dataframe.api.add
20
21
import org.jetbrains.kotlinx.dataframe.api.addAll
@@ -182,6 +183,7 @@ import org.jetbrains.kotlinx.dataframe.size
182
183
import org.jetbrains.kotlinx.dataframe.type
183
184
import org.jetbrains.kotlinx.dataframe.typeClass
184
185
import org.junit.Test
186
+ import java.lang.reflect.Type
185
187
import java.math.BigDecimal
186
188
import java.time.LocalDate
187
189
import kotlin.reflect.jvm.jvmErasure
@@ -919,6 +921,15 @@ class DataFrameTests : BaseTest() {
919
921
}
920
922
}
921
923
924
+ @Test
925
+ fun `add several columns with type inference` () {
926
+ val f: Any = 123
927
+ val df = typed.add {
928
+ expr(infer = Infer .Type ) { f } into " f"
929
+ }
930
+ df[" f" ].type() shouldBe typeOf<Int >()
931
+ }
932
+
922
933
@Test
923
934
fun `remove one column` () {
924
935
val expected = listOf (" name" , " city" , " weight" )
Original file line number Diff line number Diff line change @@ -174,8 +174,8 @@ public class AddDsl<T>(@PublishedApi internal val df: DataFrame<T>) : ColumnsCon
174
174
noinline expression : RowExpression <T , R >
175
175
): Boolean = add(df.mapToColumn(name, infer, expression))
176
176
177
- public inline fun <reified R > expr (noinline expression : RowExpression <T , R >): DataColumn <R > {
178
- return df.mapToColumn(" " , Infer . Nulls , expression)
177
+ public inline fun <reified R > expr (infer : Infer = Infer . Nulls , noinline expression : RowExpression <T , R >): DataColumn <R > {
178
+ return df.mapToColumn(" " , infer , expression)
179
179
}
180
180
181
181
public inline infix fun <reified R > String.from (noinline expression : RowExpression <T , R >): Boolean =
Original file line number Diff line number Diff line change @@ -153,8 +153,8 @@ public abstract class CreateDataFrameDsl<T> : TraversePropertiesDsl {
153
153
body : (TraversePropertiesDsl .() -> Unit )? = null,
154
154
)
155
155
156
- public inline fun <reified R > expr (noinline expression : (T ) -> R ): DataColumn <R > =
157
- source.map { expression(it) }.toColumn()
156
+ public inline fun <reified R > expr (infer : Infer = Infer . Nulls , noinline expression : (T ) -> R ): DataColumn <R > =
157
+ source.map { expression(it) }.toColumn(infer = infer )
158
158
159
159
public inline fun <reified R > add (name : String , noinline expression : (T ) -> R ): Unit =
160
160
add(source.map { expression(it) }.toColumn(name, Infer .Nulls ))
@@ -165,6 +165,9 @@ public abstract class CreateDataFrameDsl<T> : TraversePropertiesDsl {
165
165
public inline infix fun <reified R > KProperty<R>.from (noinline expression : (T ) -> R ): Unit =
166
166
add(columnName, expression)
167
167
168
+ public inline infix fun <reified R > String.from (inferType : InferType <T , R >): Unit =
169
+ add(DataColumn .createWithTypeInference(this , source.map { inferType.expression(it) }))
170
+
168
171
public inline infix fun <reified R > KProperty<R>.from (inferType : InferType <T , R >): Unit =
169
172
add(DataColumn .createWithTypeInference(columnName, source.map { inferType.expression(it) }))
170
173
Original file line number Diff line number Diff line change @@ -60,6 +60,21 @@ class CreateDataFrameTests {
60
60
res[" e" ].type() shouldBe typeOf<Int >()
61
61
}
62
62
63
+ @Test
64
+ fun `create column with infer type` () {
65
+ val data: List <Any > = listOf (1 , 2 , 3 )
66
+ val res = data.toDataFrame {
67
+ " e" from inferType { it }
68
+ expr(infer = Infer .Type ) { it } into " d"
69
+ }
70
+
71
+ res[" e" ].type() shouldBe typeOf<Int >()
72
+ res[" e" ].kind() shouldBe ColumnKind .Value
73
+
74
+ res[" d" ].type() shouldBe typeOf<Int >()
75
+ res[" d" ].kind() shouldBe ColumnKind .Value
76
+ }
77
+
63
78
@Test
64
79
fun `preserve fields order` () {
65
80
class B (val x : Int , val c : String , d : Double ) {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
15
15
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
16
16
import org.jetbrains.kotlinx.dataframe.api.ExcessiveColumns
17
17
import org.jetbrains.kotlinx.dataframe.api.GroupBy
18
+ import org.jetbrains.kotlinx.dataframe.api.Infer
18
19
import org.jetbrains.kotlinx.dataframe.api.ParserOptions
19
20
import org.jetbrains.kotlinx.dataframe.api.add
20
21
import org.jetbrains.kotlinx.dataframe.api.addAll
@@ -182,6 +183,7 @@ import org.jetbrains.kotlinx.dataframe.size
182
183
import org.jetbrains.kotlinx.dataframe.type
183
184
import org.jetbrains.kotlinx.dataframe.typeClass
184
185
import org.junit.Test
186
+ import java.lang.reflect.Type
185
187
import java.math.BigDecimal
186
188
import java.time.LocalDate
187
189
import kotlin.reflect.jvm.jvmErasure
@@ -919,6 +921,15 @@ class DataFrameTests : BaseTest() {
919
921
}
920
922
}
921
923
924
+ @Test
925
+ fun `add several columns with type inference` () {
926
+ val f: Any = 123
927
+ val df = typed.add {
928
+ expr(infer = Infer .Type ) { f } into " f"
929
+ }
930
+ df[" f" ].type() shouldBe typeOf<Int >()
931
+ }
932
+
922
933
@Test
923
934
fun `remove one column` () {
924
935
val expected = listOf (" name" , " city" , " weight" )
You can’t perform that action at this time.
0 commit comments