11package org.jetbrains.kotlinx.dataframe.api
22
33import io.kotest.matchers.shouldBe
4+ import org.jetbrains.kotlinx.dataframe.DataColumn
45import org.jetbrains.kotlinx.dataframe.nrow
56import org.junit.Test
67import kotlin.reflect.typeOf
@@ -104,6 +105,30 @@ class ValueCountsTests {
104105 result.rowsCount() shouldBe 0
105106 }
106107
108+ @Test
109+ fun `value counts of an empty nullable value column preserves type when NA values are included` () {
110+ val input = DataColumn .createByType(" city" , emptyList<String ?>(), typeOf<String ?>())
111+ val result = input.valueCounts(dropNA = false )
112+
113+ result.columnNames() shouldBe listOf (" city" , " count" )
114+ result.nrow shouldBe 0
115+ result[" city" ].isValueColumn() shouldBe true
116+ result[" city" ].type() shouldBe typeOf<String ?>()
117+ result[" count" ].type() shouldBe typeOf<Int >()
118+ }
119+
120+ @Test
121+ fun `value counts of an empty nullable value column drops nullability when NA values are excluded` () {
122+ val input = DataColumn .createByType(" city" , emptyList<String ?>(), typeOf<String ?>())
123+ val result = input.valueCounts()
124+
125+ result.columnNames() shouldBe listOf (" city" , " count" )
126+ result.nrow shouldBe 0
127+ result[" city" ].isValueColumn() shouldBe true
128+ result[" city" ].type() shouldBe typeOf<String >()
129+ result[" count" ].type() shouldBe typeOf<Int >()
130+ }
131+
107132 @Test
108133 fun `value counts of a column with a result column name clashing with the column name` () {
109134 val count by columnOf(" a" , " b" , " a" )
@@ -288,12 +313,12 @@ class ValueCountsTests {
288313
289314 @Test
290315 fun `value counts retains selected columns when every row is dropped` () {
291- val input = dataFrameOf(" city" to columnOf< String ?>( null , null ))
316+ val input = dataFrameOf(" city" to columnOf( " London " , null )).drop( 1 )
292317 val result = input.valueCounts(" city" )
293318
294319 result.columnNames() shouldBe listOf (" city" , " count" )
295320 result.nrow shouldBe 0
296- result[" city" ].type() shouldBe typeOf<String ? >() // TODO #2034
321+ result[" city" ].type() shouldBe typeOf<String >()
297322 result[" count" ].type() shouldBe typeOf<Int >()
298323 }
299324
@@ -305,8 +330,8 @@ class ValueCountsTests {
305330 result.columnNames() shouldBe listOf (" details" , " count" )
306331 result[" details" ].asColumnGroup().columnNames() shouldBe listOf (" name" , " city" )
307332 result.nrow shouldBe 0
308- result[" details" ].asColumnGroup()[" name" ].type() shouldBe typeOf<String ? >() // TODO #2034
309- result[" details" ].asColumnGroup()[" city" ].type() shouldBe typeOf<String ? >() // TODO #2034
333+ result[" details" ].asColumnGroup()[" name" ].type() shouldBe typeOf<String >()
334+ result[" details" ].asColumnGroup()[" city" ].type() shouldBe typeOf<String >()
310335 result[" count" ].type() shouldBe typeOf<Int >()
311336 }
312337
0 commit comments