@@ -49,7 +49,6 @@ private typealias ValueCountsParams = Nothing
4949 * - the distinct combinations of these values,
5050 * - a new [Int] column with the number of occurrences of each combination.
5151 * This column is called `"count"` by default unless it is overridden by [resultColumn\].
52- * If no rows are counted, the result still contains the selected top-level columns and the count column.
5352 *
5453 * By default, the result is sorted by count in descending order (the most frequent combination first),
5554 * and a row is not counted at all if any of its selected values is {@include [NALink]}.
@@ -100,7 +99,6 @@ internal val defaultCountColumnName: String = ValueCount::count.name
10099 * - the column with the distinct values of the original [DataColumn]
101100 * - a new [Int] column with the number of occurrences of each value.
102101 * This column is called `"count"` by default unless it is overridden by [resultColumn].
103- * If no values are counted, the result still contains the observed-value column and the count column.
104102 *
105103 * By default, the result is sorted by count in descending order (the most frequent value first),
106104 * and {@include [NALink]} values (`null`s and `NaN`s) are not counted.
@@ -150,11 +148,14 @@ public fun <T> DataColumn<T>.valueCounts(
150148 }
151149 if (dropNA) grouped = grouped.filter { ! it.first.isNA }
152150 val nulls = if (dropNA) false else hasNulls()
153- val values = if (grouped.isEmpty()) {
154- get(emptyList<Int >())
155- } else {
156- DataColumn .createByType(name(), grouped.map { it.first }, type().withNullability(nulls))
157- }
151+ val values =
152+ if (grouped.isEmpty()) {
153+ // #2034, keep the original name and type of the column even when the input is empty
154+ // nullability is irrelevant, but let's keep it for consistency
155+ take(0 ).setNullable(nulls)
156+ } else {
157+ DataColumn .createByType(name(), grouped.map { it.first }, type().withNullability(nulls))
158+ }
158159 val countName = if (resultColumn == name()) resultColumn + " 1" else resultColumn
159160 val counts = DataColumn .createByType(countName, grouped.map { it.second }, typeOf<Int >())
160161 return dataFrameOf(values, counts).cast()
0 commit comments