Skip to content

Commit fff2b98

Browse files
committed
Call convertToDouble from convertTo<Double> for DataColumn<String>
1 parent 4301051 commit fff2b98

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ public fun <T, C> Convert<T, C>.to(columnConverter: DataFrame<T>.(DataColumn<C>)
100100
df.replace(columns).with { columnConverter(df, it) }
101101

102102
public inline fun <reified C> AnyCol.convertTo(): DataColumn<C> = convertTo(typeOf<C>()) as DataColumn<C>
103-
public fun AnyCol.convertTo(newType: KType): AnyCol = convertToTypeImpl(newType)
103+
public fun AnyCol.convertTo(newType: KType): AnyCol {
104+
if (this.type() == typeOf<String>() && newType == typeOf<Double>()) return (this as DataColumn<String>).convertToDouble()
105+
if (this.type() == typeOf<String?>() && newType == typeOf<Double?>()) return (this as DataColumn<String?>).convertToDouble()
106+
return convertToTypeImpl(newType)
107+
}
104108

105109
@JvmName("convertToLocalDateTimeFromT")
106110
public fun <T : Any> DataColumn<T>.convertToLocalDateTime(): DataColumn<LocalDateTime> = convertTo()
@@ -133,18 +137,7 @@ public fun <T : Any> DataColumn<T?>.convertToDouble(): DataColumn<Double?> = con
133137
*/
134138
@JvmName("convertToDoubleFromString")
135139
public fun DataColumn<String>.convertToDouble(locale: Locale? = null): DataColumn<Double> {
136-
if (locale is Locale) {
137-
val explicitConverter = Parsers.getDoubleConverter(locale) as (String) -> Double?
138-
return map { explicitConverter(it.trim()) ?: error("Can't convert `$it` to Double") }
139-
} else {
140-
return try {
141-
val defaultConverter = Parsers.getDoubleConverter() as (String) -> Double?
142-
map { defaultConverter(it.trim()) ?: error("Can't convert `$it` to Double") }
143-
} catch (e: TypeConversionException) {
144-
val posixConverter = Parsers.getDoubleConverter(Locale.forLanguageTag("C.UTF-8")) as (String) -> Double?
145-
map { posixConverter(it.trim()) ?: error("Can't convert `$it` to Double") }
146-
}
147-
}
140+
return this.castToNullable().convertToDouble(locale).castToNotNullable()
148141
}
149142

150143
/**
@@ -154,16 +147,16 @@ public fun DataColumn<String>.convertToDouble(locale: Locale? = null): DataColum
154147
*/
155148
@JvmName("convertToDoubleFromStringNullable")
156149
public fun DataColumn<String?>.convertToDouble(locale: Locale? = null): DataColumn<Double?> {
157-
if (locale is Locale) {
158-
val explicitConverter = Parsers.getDoubleConverter(locale) as (String) -> Double?
159-
return map { it?.let { explicitConverter(it.trim()) ?: error("Can't convert `$it` to Double") } }
150+
if (locale != null) {
151+
val explicitParser = Parsers.getDoubleParser(locale)
152+
return map { it?.let { explicitParser(it.trim()) ?: throw TypeConversionException(it, typeOf<String>(), typeOf<Double>()) } }
160153
} else {
161154
return try {
162-
val defaultConverter = Parsers.getDoubleConverter() as (String) -> Double?
163-
map { it?.let { defaultConverter(it.trim()) ?: error("Can't convert `$it` to Double") } }
164-
} catch (e: IllegalStateException) {
165-
val posixConverter = Parsers.getDoubleConverter(Locale.forLanguageTag("C.UTF-8")) as (String) -> Double?
166-
map { it?.let { posixConverter(it.trim()) ?: error("Can't convert `$it` to Double") } }
155+
val defaultParser = Parsers.getDoubleParser()
156+
map { it?.let { defaultParser(it.trim()) ?: throw TypeConversionException(it, typeOf<String>(), typeOf<Double>()) } }
157+
} catch (e: TypeConversionException) {
158+
val posixParser = Parsers.getDoubleParser(Locale.forLanguageTag("C.UTF-8"))
159+
map { it?.let { posixParser(it.trim()) ?: throw TypeConversionException(it, typeOf<String>(), typeOf<Double>()) } }
167160
}
168161
}
169162
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/parse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ internal object Parsers : GlobalParserOptions {
273273
return parser.applyOptions(options)
274274
}
275275

276-
internal fun getDoubleConverter(locale: Locale? = null): TypeConverter {
276+
internal fun getDoubleParser(locale: Locale? = null): (String) -> Double? {
277277
val options = if (locale != null) ParserOptions(
278278
locale = locale
279279
) else null
280-
return parserToDoubleWithOptions.toConverter(options)
280+
return parserToDoubleWithOptions.applyOptions(options)
281281
}
282282
}
283283

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/ParserTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class ParserTests {
132132
Locale.setDefault(Locale.forLanguageTag("ru-RU"))
133133

134134
columnDot.convertTo<Double>().shouldBe(columnOf(12.345, 67.89))
135-
columnComma.convertTo<Double>().shouldBe(columnOf(12345.0, 67890.0))
135+
columnComma.convertTo<Double>().shouldBe(columnOf(12.345, 67.89))
136136
columnMixed.convertTo<Double>().shouldBe(columnOf(12.345, 67890.0))
137137

138138
columnDot.convertToDouble(parsingLocaleNotDefined).shouldBe(columnOf(12.345, 67.89))

0 commit comments

Comments
 (0)