@@ -100,7 +100,11 @@ public fun <T, C> Convert<T, C>.to(columnConverter: DataFrame<T>.(DataColumn<C>)
100
100
df.replace(columns).with { columnConverter(df, it) }
101
101
102
102
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
+ }
104
108
105
109
@JvmName(" convertToLocalDateTimeFromT" )
106
110
public fun <T : Any > DataColumn<T>.convertToLocalDateTime (): DataColumn <LocalDateTime > = convertTo()
@@ -133,18 +137,7 @@ public fun <T : Any> DataColumn<T?>.convertToDouble(): DataColumn<Double?> = con
133
137
*/
134
138
@JvmName(" convertToDoubleFromString" )
135
139
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()
148
141
}
149
142
150
143
/* *
@@ -154,16 +147,16 @@ public fun DataColumn<String>.convertToDouble(locale: Locale? = null): DataColum
154
147
*/
155
148
@JvmName(" convertToDoubleFromStringNullable" )
156
149
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 >() ) } }
160
153
} else {
161
154
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 >() ) } }
167
160
}
168
161
}
169
162
}
0 commit comments