Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

describe() fixes #937

Merged
merged 18 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6b4e9d0
expanded cumSum with Short, Byte, and BigInteger
Jolanrensen Oct 29, 2024
c97a973
added BigInteger to mean()
Jolanrensen Oct 29, 2024
6e028f1
Merge branch 'master' into statistics-fixes
Jolanrensen Nov 6, 2024
ba00f0f
fixup! expanded cumSum with Short, Byte, and BigInteger
Jolanrensen Nov 7, 2024
4d4ebda
expanded sum with float, short, byte, biginteger and some warnings fo…
Jolanrensen Nov 7, 2024
2ef56b4
expanded std with BigInteger and Number
Jolanrensen Nov 7, 2024
ac3eb9a
Fixed ColumnDescription.type type
Jolanrensen Nov 7, 2024
563d367
added describe support for incomparable number types (converting them…
Jolanrensen Nov 7, 2024
966447d
renamed isComparable to isInterComparable to better reflect what it d…
Jolanrensen Nov 8, 2024
63ee929
fixup! added describe support for incomparable number types (converti…
Jolanrensen Nov 8, 2024
b2bdb4a
added nothing type to cumsum for empty/null-filled columns
Jolanrensen Nov 8, 2024
05e0993
adding tests and comments, missing types for median, expanding cumSum
Jolanrensen Nov 13, 2024
c8de339
bumping ktlint engine version to 1.4.1, disabled new when-entry-braci…
Jolanrensen Nov 12, 2024
860ea39
Merge branch 'master' into statistics-fixes
Jolanrensen Nov 15, 2024
ea4d035
fixup! adding tests and comments, missing types for median, expanding…
Jolanrensen Nov 15, 2024
b64791f
fixed tests
Jolanrensen Nov 19, 2024
8889e13
Merge branch 'master' into statistics-fixes
Jolanrensen Nov 21, 2024
36fd883
renamed isInterComparable to valuesAreComparable based on feedback
Jolanrensen Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2691,7 +2691,7 @@ public abstract interface class org/jetbrains/kotlinx/dataframe/api/ColumnDescri
public abstract fun getPath ()Lorg/jetbrains/kotlinx/dataframe/columns/ColumnPath;
public abstract fun getStd ()D
public abstract fun getTop ()Ljava/lang/Object;
public abstract fun getType ()Lkotlin/reflect/KType;
public abstract fun getType ()Ljava/lang/String;
public abstract fun getUnique ()I
}

Expand Down Expand Up @@ -2719,7 +2719,7 @@ public final class org/jetbrains/kotlinx/dataframe/api/ColumnDescription_Extensi
public static final fun ColumnDescription_top (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
public static final fun ColumnDescription_top (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
public static final fun ColumnDescription_type (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
public static final fun ColumnDescription_type (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Lkotlin/reflect/KType;
public static final fun ColumnDescription_type (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
Jolanrensen marked this conversation as resolved.
Show resolved Hide resolved
public static final fun ColumnDescription_unique (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
public static final fun ColumnDescription_unique (Lorg/jetbrains/kotlinx/dataframe/DataRow;)I
public static final fun NullableColumnDescription_count (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
Expand All @@ -2745,7 +2745,7 @@ public final class org/jetbrains/kotlinx/dataframe/api/ColumnDescription_Extensi
public static final fun NullableColumnDescription_top (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
public static final fun NullableColumnDescription_top (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Object;
public static final fun NullableColumnDescription_type (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
public static final fun NullableColumnDescription_type (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Lkotlin/reflect/KType;
public static final fun NullableColumnDescription_type (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/String;
public static final fun NullableColumnDescription_unique (Lorg/jetbrains/kotlinx/dataframe/ColumnsContainer;)Lorg/jetbrains/kotlinx/dataframe/DataColumn;
public static final fun NullableColumnDescription_unique (Lorg/jetbrains/kotlinx/dataframe/DataRow;)Ljava/lang/Integer;
}
Expand Down Expand Up @@ -4146,9 +4146,11 @@ public final class org/jetbrains/kotlinx/dataframe/api/DataColumnArithmeticsKt {
}

public final class org/jetbrains/kotlinx/dataframe/api/DataColumnTypeKt {
public static final fun isBigNumber (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isColumnGroup (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isComparable (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isFrameColumn (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isInterComparable (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isList (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isNumber (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
public static final fun isPrimitive (Lorg/jetbrains/kotlinx/dataframe/DataColumn;)Z
Expand Down Expand Up @@ -10771,6 +10773,7 @@ public final class org/jetbrains/kotlinx/dataframe/math/BasicStats {

public final class org/jetbrains/kotlinx/dataframe/math/MeanKt {
public static final fun bigDecimalMean (Ljava/lang/Iterable;)D
public static final fun bigIntegerMean (Ljava/lang/Iterable;)D
public static final fun byteMean (Ljava/lang/Iterable;)D
public static final fun doubleMean (Ljava/lang/Iterable;Z)D
public static synthetic fun doubleMean$default (Ljava/lang/Iterable;ZILjava/lang/Object;)D
Expand All @@ -10794,6 +10797,8 @@ public final class org/jetbrains/kotlinx/dataframe/math/MedianKt {
public final class org/jetbrains/kotlinx/dataframe/math/StdKt {
public static final fun bigDecimalStd (Ljava/lang/Iterable;I)D
public static synthetic fun bigDecimalStd$default (Ljava/lang/Iterable;IILjava/lang/Object;)D
public static final fun bigIntegerStd (Ljava/lang/Iterable;I)D
public static synthetic fun bigIntegerStd$default (Ljava/lang/Iterable;IILjava/lang/Object;)D
public static final fun doubleStd (Ljava/lang/Iterable;ZI)D
public static synthetic fun doubleStd$default (Ljava/lang/Iterable;ZIILjava/lang/Object;)D
public static final fun floatStd (Ljava/lang/Iterable;ZI)D
Expand All @@ -10808,6 +10813,7 @@ public final class org/jetbrains/kotlinx/dataframe/math/StdKt {

public final class org/jetbrains/kotlinx/dataframe/math/StdMeanKt {
public static final fun bigDecimalVarianceAndMean (Ljava/lang/Iterable;)Lorg/jetbrains/kotlinx/dataframe/math/BasicStats;
public static final fun bigIntegerVarianceAndMean (Ljava/lang/Iterable;)Lorg/jetbrains/kotlinx/dataframe/math/BasicStats;
public static final fun doubleVarianceAndMean (Ljava/lang/Iterable;Z)Lorg/jetbrains/kotlinx/dataframe/math/BasicStats;
public static synthetic fun doubleVarianceAndMean$default (Ljava/lang/Iterable;ZILjava/lang/Object;)Lorg/jetbrains/kotlinx/dataframe/math/BasicStats;
public static final fun floatVarianceAndMean (Ljava/lang/Iterable;Z)Lorg/jetbrains/kotlinx/dataframe/math/BasicStats;
Expand All @@ -10818,8 +10824,10 @@ public final class org/jetbrains/kotlinx/dataframe/math/StdMeanKt {

public final class org/jetbrains/kotlinx/dataframe/math/SumKt {
public static final fun sum (Ljava/lang/Iterable;)Ljava/math/BigDecimal;
public static final fun sum (Ljava/lang/Iterable;)Ljava/math/BigInteger;
public static final fun sum (Ljava/lang/Iterable;Lkotlin/reflect/KType;)Ljava/lang/Number;
public static final fun sum (Lkotlin/sequences/Sequence;)Ljava/math/BigDecimal;
public static final fun sum (Lkotlin/sequences/Sequence;)Ljava/math/BigInteger;
public static final fun sumNullableT (Ljava/lang/Iterable;Lkotlin/reflect/KType;)Ljava/lang/Number;
public static final fun sumOf (Ljava/lang/Iterable;Lkotlin/reflect/KType;Lkotlin/jvm/functions/Function1;)Ljava/lang/Number;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import org.jetbrains.kotlinx.dataframe.impl.isNothing
import org.jetbrains.kotlinx.dataframe.impl.projectTo
import org.jetbrains.kotlinx.dataframe.type
import org.jetbrains.kotlinx.dataframe.typeClass
import org.jetbrains.kotlinx.dataframe.util.IS_COMPARABLE
import org.jetbrains.kotlinx.dataframe.util.IS_COMPARABLE_REPLACE
import org.jetbrains.kotlinx.dataframe.util.IS_INTER_COMPARABLE_IMPORT
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down Expand Up @@ -44,13 +49,26 @@ public inline fun <reified T> AnyCol.isType(): Boolean = type() == typeOf<T>()

public fun AnyCol.isNumber(): Boolean = isSubtypeOf<Number?>()

public fun AnyCol.isBigNumber(): Boolean = isSubtypeOf<BigInteger?>() || isSubtypeOf<BigDecimal?>()

public fun AnyCol.isList(): Boolean = typeClass == List::class

/** @include [isInterComparable] */
@Deprecated(
message = IS_COMPARABLE,
replaceWith = ReplaceWith(IS_COMPARABLE_REPLACE, IS_INTER_COMPARABLE_IMPORT),
level = DeprecationLevel.WARNING,
)
public fun AnyCol.isComparable(): Boolean = isInterComparable()

/**
* Returns `true` if [this] column is comparable, i.e. its type is a subtype of [Comparable] and its
* type argument is not [Nothing].
* Returns `true` if [this] column is inter-comparable, i.e.
* its elements can be compared with each other.
*
* Technically, this means the elements' common type is a subtype of [Comparable] with
* the type argument not being [Nothing].
*/
public fun AnyCol.isComparable(): Boolean =
public fun AnyCol.isInterComparable(): Boolean =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isInterComparable is a new / not widespread term, for me it raises some questions like what's the difference (if i were to see it for the first time)
For users isComparable can be described as "filter columns for which max(), min(), median() and other (we actually only have 3) order operations make sense"
You wouldn't expect to find max() value for mixed column of String and Double values, so it should make things clearer

Copy link
Collaborator Author

@Jolanrensen Jolanrensen Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's actually the reason I changed it, because for me isComparable can be interpreted in two ways:

  • "is comparable": so can call max(), min(), etc., not true for mixed number columns
  • "is Comparable"; implements the interface, which would be true for mixed number columns

you need KDocs to explain what it actually means (and past-me didn't do a good job at that either). inter-comparable means "comparable inside" (or something, I came up with it too), which narrows down the meaning to the first. Of course, other name suggestions are very welcome if you can think of any :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With suggestion of ChatGPT I'll rename it to .valuesAreComparable(). Would that be okay?

isSubtypeOf<Comparable<*>?>() &&
type().projectTo(Comparable::class).arguments[0].let {
it != KTypeProjection.STAR &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import org.jetbrains.kotlinx.dataframe.ColumnsSelector
import org.jetbrains.kotlinx.dataframe.DataColumn
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.impl.nothingType
import org.jetbrains.kotlinx.dataframe.impl.nullableNothingType
import org.jetbrains.kotlinx.dataframe.math.cumSum
import org.jetbrains.kotlinx.dataframe.math.defaultCumSumSkipNA
import org.jetbrains.kotlinx.dataframe.typeClass
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.reflect.KProperty
import kotlin.reflect.typeOf

Expand All @@ -17,20 +20,59 @@ import kotlin.reflect.typeOf
public fun <T : Number?> DataColumn<T>.cumSum(skipNA: Boolean = defaultCumSumSkipNA): DataColumn<T> =
when (type()) {
typeOf<Double>() -> cast<Double>().cumSum(skipNA).cast()

typeOf<Double?>() -> cast<Double?>().cumSum(skipNA).cast()

typeOf<Float>() -> cast<Float>().cumSum(skipNA).cast()

typeOf<Float?>() -> cast<Float?>().cumSum(skipNA).cast()

typeOf<Int>() -> cast<Int>().cumSum().cast()

// TODO cumSum for Byte returns Int but is converted back to T: Byte, Issue #558
typeOf<Byte>() -> cast<Byte>().cumSum().map { it.toByte() }.cast()

// TODO cumSum for Short returns Int but is converted back to T: Short, Issue #558
typeOf<Short>() -> cast<Short>().cumSum().map { it.toShort() }.cast()

typeOf<Int?>() -> cast<Int?>().cumSum(skipNA).cast()

// TODO cumSum for Byte? returns Int? but is converted back to T: Byte?, Issue #558
typeOf<Byte?>() -> cast<Byte?>().cumSum(skipNA).map { it?.toByte() }.cast()

// TODO cumSum for Short? returns Int? but is converted back to T: Short?, Issue #558
typeOf<Short?>() -> cast<Short?>().cumSum(skipNA).map { it?.toShort() }.cast()

typeOf<Long>() -> cast<Long>().cumSum().cast()

typeOf<Long?>() -> cast<Long?>().cumSum(skipNA).cast()

typeOf<BigInteger>() -> cast<BigInteger>().cumSum().cast()

typeOf<BigInteger?>() -> cast<BigInteger?>().cumSum(skipNA).cast()

typeOf<BigDecimal>() -> cast<BigDecimal>().cumSum().cast()

typeOf<BigDecimal?>() -> cast<BigDecimal?>().cumSum(skipNA).cast()

typeOf<Number?>(), typeOf<Number>() -> convertToDouble().cumSum(skipNA).cast()

// Cumsum for empty column or column with just null is itself
nothingType, nullableNothingType -> this

else -> error("Cumsum for type ${type()} is not supported")
}

private val supportedClasses = setOf(Double::class, Float::class, Int::class, Long::class, BigDecimal::class)
private val supportedClasses = setOf(
Double::class,
Float::class,
Int::class,
Byte::class,
Short::class,
Long::class,
BigInteger::class,
BigDecimal::class,
)

// endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.impl.api.describeImpl
import kotlin.reflect.KProperty
import kotlin.reflect.KType

// region DataSchema
@DataSchema
public interface ColumnDescription {
public val name: String
public val path: ColumnPath
public val type: KType
public val type: String
public val count: Int
public val unique: Int
public val nulls: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.columns.values
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Aggregators
import org.jetbrains.kotlinx.dataframe.impl.aggregation.comparableColumns
import org.jetbrains.kotlinx.dataframe.impl.aggregation.interComparableColumns
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateAll
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateFor
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateOfDelegated
Expand Down Expand Up @@ -52,7 +52,7 @@ public inline fun <reified T : Comparable<T>> AnyRow.rowMaxOf(): T = rowMaxOfOrN

// region DataFrame

public fun <T> DataFrame<T>.max(): DataRow<T> = maxFor(comparableColumns())
public fun <T> DataFrame<T>.max(): DataRow<T> = maxFor(interComparableColumns())

public fun <T, C : Comparable<C>> DataFrame<T>.maxFor(columns: ColumnsForAggregateSelector<T, C?>): DataRow<T> =
Aggregators.max.aggregateFor(this, columns)
Expand Down Expand Up @@ -121,7 +121,7 @@ public fun <T, C : Comparable<C>> DataFrame<T>.maxByOrNull(column: KProperty<C?>

// region GroupBy

public fun <T> Grouped<T>.max(): DataFrame<T> = maxFor(comparableColumns())
public fun <T> Grouped<T>.max(): DataFrame<T> = maxFor(interComparableColumns())

public fun <T, C : Comparable<C>> Grouped<T>.maxFor(columns: ColumnsForAggregateSelector<T, C?>): DataFrame<T> =
Aggregators.max.aggregateFor(this, columns)
Expand Down Expand Up @@ -218,7 +218,7 @@ public fun <T, C : Comparable<C>> Pivot<T>.maxBy(column: KProperty<C?>): Reduced

// region PivotGroupBy

public fun <T> PivotGroupBy<T>.max(separate: Boolean = false): DataFrame<T> = maxFor(separate, comparableColumns())
public fun <T> PivotGroupBy<T>.max(separate: Boolean = false): DataFrame<T> = maxFor(separate, interComparableColumns())

public fun <T, R : Comparable<R>> PivotGroupBy<T>.maxFor(
separate: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Aggregators
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.cast
import org.jetbrains.kotlinx.dataframe.impl.aggregation.comparableColumns
import org.jetbrains.kotlinx.dataframe.impl.aggregation.interComparableColumns
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateAll
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateFor
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateOf
Expand Down Expand Up @@ -53,7 +53,7 @@ public inline fun <reified T : Comparable<T>> AnyRow.rowMedianOf(): T =

// region DataFrame

public fun <T> DataFrame<T>.median(): DataRow<T> = medianFor(comparableColumns())
public fun <T> DataFrame<T>.median(): DataRow<T> = medianFor(interComparableColumns())

public fun <T, C : Comparable<C>> DataFrame<T>.medianFor(columns: ColumnsForAggregateSelector<T, C?>): DataRow<T> =
Aggregators.median.aggregateFor(this, columns)
Expand Down Expand Up @@ -96,7 +96,7 @@ public inline fun <T, reified R : Comparable<R>> DataFrame<T>.medianOf(

// region GroupBy

public fun <T> Grouped<T>.median(): DataFrame<T> = medianFor(comparableColumns())
public fun <T> Grouped<T>.median(): DataFrame<T> = medianFor(interComparableColumns())

public fun <T, C : Comparable<C>> Grouped<T>.medianFor(columns: ColumnsForAggregateSelector<T, C?>): DataFrame<T> =
Aggregators.median.aggregateFor(this, columns)
Expand Down Expand Up @@ -134,7 +134,7 @@ public inline fun <T, reified R : Comparable<R>> Grouped<T>.medianOf(

// region Pivot

public fun <T> Pivot<T>.median(separate: Boolean = false): DataRow<T> = medianFor(separate, comparableColumns())
public fun <T> Pivot<T>.median(separate: Boolean = false): DataRow<T> = medianFor(separate, interComparableColumns())

public fun <T, C : Comparable<C>> Pivot<T>.medianFor(
separate: Boolean = false,
Expand Down Expand Up @@ -174,7 +174,7 @@ public inline fun <T, reified R : Comparable<R>> Pivot<T>.medianOf(
// region PivotGroupBy

public fun <T> PivotGroupBy<T>.median(separate: Boolean = false): DataFrame<T> =
medianFor(separate, comparableColumns())
medianFor(separate, interComparableColumns())

public fun <T, C : Comparable<C>> PivotGroupBy<T>.medianFor(
separate: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
import org.jetbrains.kotlinx.dataframe.columns.values
import org.jetbrains.kotlinx.dataframe.impl.aggregation.aggregators.Aggregators
import org.jetbrains.kotlinx.dataframe.impl.aggregation.comparableColumns
import org.jetbrains.kotlinx.dataframe.impl.aggregation.interComparableColumns
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateAll
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateFor
import org.jetbrains.kotlinx.dataframe.impl.aggregation.modes.aggregateOfDelegated
Expand Down Expand Up @@ -52,7 +52,7 @@ public inline fun <reified T : Comparable<T>> AnyRow.rowMinOf(): T = rowMinOfOrN

// region DataFrame

public fun <T> DataFrame<T>.min(): DataRow<T> = minFor(comparableColumns())
public fun <T> DataFrame<T>.min(): DataRow<T> = minFor(interComparableColumns())

public fun <T, C : Comparable<C>> DataFrame<T>.minFor(columns: ColumnsForAggregateSelector<T, C?>): DataRow<T> =
Aggregators.min.aggregateFor(this, columns)
Expand Down Expand Up @@ -121,7 +121,7 @@ public fun <T, C : Comparable<C>> DataFrame<T>.minByOrNull(column: KProperty<C?>

// region GroupBy

public fun <T> Grouped<T>.min(): DataFrame<T> = minFor(comparableColumns())
public fun <T> Grouped<T>.min(): DataFrame<T> = minFor(interComparableColumns())

public fun <T, C : Comparable<C>> Grouped<T>.minFor(columns: ColumnsForAggregateSelector<T, C?>): DataFrame<T> =
Aggregators.min.aggregateFor(this, columns)
Expand Down Expand Up @@ -219,7 +219,7 @@ public fun <T, C : Comparable<C>> Pivot<T>.minBy(column: KProperty<C?>): Reduced

// region PivotGroupBy

public fun <T> PivotGroupBy<T>.min(separate: Boolean = false): DataFrame<T> = minFor(separate, comparableColumns())
public fun <T> PivotGroupBy<T>.min(separate: Boolean = false): DataFrame<T> = minFor(separate, interComparableColumns())

public fun <T, R : Comparable<R>> PivotGroupBy<T>.minFor(
separate: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public fun DataColumn<Any>.asNumbers(): ValueColumn<Number> {
}

public fun <T> DataColumn<T>.asComparable(): DataColumn<Comparable<T>> {
require(isComparable())
require(isInterComparable())
return this as DataColumn<Comparable<T>>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import org.jetbrains.kotlinx.dataframe.DataRow
import org.jetbrains.kotlinx.dataframe.api.Infer
import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnGuessingType
import org.jetbrains.kotlinx.dataframe.util.GUESS_VALUE_TYPE
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.KTypeParameter
Expand All @@ -29,6 +31,7 @@ import kotlin.reflect.full.superclasses
import kotlin.reflect.full.withNullability
import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.typeOf
import kotlin.toBigDecimal as toBigDecimalKotlin

internal inline fun <reified T> KClass<*>.createTypeUsing() = typeOf<T>().projectTo(this)

Expand Down Expand Up @@ -546,12 +549,10 @@ internal fun guessValueType(
internal val KType.isNothing: Boolean
get() = classifier == Nothing::class

internal fun nothingType(nullable: Boolean): KType =
if (nullable) {
typeOf<List<Nothing?>>()
} else {
typeOf<List<Nothing>>()
}.arguments.first().type!!
internal val nothingType: KType = typeOf<List<Nothing>>().arguments.first().type!!
internal val nullableNothingType: KType = typeOf<List<Nothing?>>().arguments.first().type!!

internal fun nothingType(nullable: Boolean): KType = if (nullable) nullableNothingType else nothingType

@OptIn(ExperimentalUnsignedTypes::class)
private val primitiveArrayClasses = setOf(
Expand Down Expand Up @@ -646,3 +647,18 @@ internal fun Any.asArrayAsListOrNull(): List<*>? =
is Array<*> -> asList()
else -> null
}

internal fun Any.isBigNumber(): Boolean = this is BigInteger || this is BigDecimal

internal fun Number.toBigDecimal(): BigDecimal =
when (this) {
is BigDecimal -> this
is BigInteger -> this.toBigDecimalKotlin()
is Int -> this.toBigDecimalKotlin()
is Byte -> this.toInt().toBigDecimalKotlin()
is Short -> this.toInt().toBigDecimalKotlin()
is Long -> this.toBigDecimalKotlin()
is Float -> this.toBigDecimalKotlin()
is Double -> this.toBigDecimalKotlin()
else -> BigDecimal(this.toString())
}
Loading