Kotlin / Kotlin/dataframe

Support additional Number types (like Percentage)

Open
#610 3 comments 1 reaction 0 assignees View on GitHub
enhancement research
Dominant language
Kotlin
Stars
1.1k
Forks
83
Avg merge
4d 12h
Merged PRs (30d)
30

Description

I was experimenting with adding a wrapper around Doubles for percentages, since that's something many other table solutions support.

Something like this works for basic usage:
```kotlin
data class Percentage(val value: Double, val noDecimals: Int = 2) : Number(), Comparable {
override fun toByte(): Byte = value.toInt().toByte()
override fun toDouble(): Double = value
override fun toFloat(): Float = value.toFloat()
override fun toInt(): Int = value.toInt()
override fun toLong(): Long = value.toLong()
override fun toShort(): Short = value.toInt().toShort()
override fun compareTo(other: Percentage): Int = value.compareTo(other.value)
override fun toChar(): Char = value.toInt().toChar()

private fun Double.roundToNoDecimals(noDecimals: Int): Double =
round(this * 10.0.pow(noDecimals)) / 10.0.pow(noDecimals)

override fun toString(): String {
val percentage = value * 100.0
val double =
if (noDecimals <= 0) {
round(percentage).toLong()
} else {
percentage.roundToNoDecimals(noDecimals)
}
return "$double%"
}
}

fun Number.toPercentage(noDecimals: Int = 2): Percentage = Percentage(this.toDouble(), noDecimals)
```
![image_720](https://github.com/Kotlin/dataframe/assets/17594275/8b38b036-7712-43fe-900c-71a5d0e454ff)

However, since this is a `Number`, I was under the impression we supported converting it (among other things) right out of the gate. Unfortunately I was wrong:

![image_720](https://github.com/Kotlin/dataframe/assets/17594275/1c230a55-f29c-425a-a552-a52883b6a6c6)

To fully support any new `Number` implementation, we need:
- Decide on a "default" number type to convert unknowns to. I suggest `Double`.
- filled in `Number` in impl/convert.kt::createConverter
- Check statistics for `Number` cases: https://github.com/Kotlin/dataframe/issues/558
- Column Arithmatics for `Number` columns

Contributor guide

Open the contributing guide

Research direction

Start in impl/convert.kt::createConverter and determine how unknown Number implementations should convert, using Double as the proposed default. Review the statistics cases tracked in issue #558, then inspect column arithmetic for Number columns. Done means conversion, statistics, and arithmetic support the new Number types such as Percentage.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.