Skip to content

Commit f23c7ed

Browse files
committed
Mark some possibly redundant toType overloads in convert.kt to discuss in a draft PR
1 parent a4a61ae commit f23c7ed

2 files changed

Lines changed: 10 additions & 178 deletions

File tree

  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

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

Lines changed: 1 addition & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -955,32 +955,6 @@ public fun <T> Convert<T, URL?>.toIFrame(
955955
height: Int? = null,
956956
): DataFrame<T> = asColumn { it.map { url -> url?.let { IFRAME(url.toString(), border, width, height) } } }
957957

958-
/**
959-
* Converts values in an [URL] columns previously selected with [convert] to an [IFRAME],
960-
* preserving their original names and positions within the [DataFrame].
961-
*
962-
* For more information: {@include [DocumentationUrls.Convert]}
963-
*
964-
* ### Examples:
965-
* ```kotlin
966-
* df.convert { imgUrl }.toIFrame()
967-
* ```
968-
*
969-
* @param border Whether the iframe should have a border. Defaults to `false`.
970-
* @param width Optional width of the iframe in pixels.
971-
* @param height Optional height of the iframe in pixels.
972-
* @return A new [DataFrame] with the values converted to an [IFRAME].
973-
*/
974-
@JvmName("toIframeFromUrl")
975-
@Refine
976-
@Converter(IFRAME::class, nullable = false)
977-
@Interpretable("ToSpecificType")
978-
public fun <T> Convert<T, URL>.toIFrame(
979-
border: Boolean = false,
980-
width: Int? = null,
981-
height: Int? = null,
982-
): DataFrame<T> = asColumn { it.map { IFRAME(it.toString(), border, width, height) } }
983-
984958
/**
985959
* Converts values in an [URL] columns previously selected with [convert] to an [IMG],
986960
* preserving their original names and positions within the [DataFrame].
@@ -1004,28 +978,6 @@ public fun <T> Convert<T, URL>.toIFrame(
1004978
public fun <T, R : URL?> Convert<T, URL?>.toImg(width: Int? = null, height: Int? = null): DataFrame<T> =
1005979
asColumn { it.map { url -> url?.let { IMG(url.toString(), width, height) } } }
1006980

1007-
/**
1008-
* Converts values in an [URL] columns previously selected with [convert] to an [IMG],
1009-
* preserving their original names and positions within the [DataFrame].
1010-
*
1011-
* For more information: {@include [DocumentationUrls.Convert]}
1012-
*
1013-
* ### Examples:
1014-
* ```kotlin
1015-
* df.convert { avatarUrl }.toImg()
1016-
* ```
1017-
*
1018-
* @param width Optional width of the image in pixels.
1019-
* @param height Optional height of the image in pixels.
1020-
* @return A new [DataFrame] with the values converted to an [IMG].
1021-
*/
1022-
@JvmName("toImgFromUrl")
1023-
@Refine
1024-
@Converter(IMG::class, nullable = false)
1025-
@Interpretable("ToSpecificType")
1026-
public fun <T, R : URL?> Convert<T, URL>.toImg(width: Int? = null, height: Int? = null): DataFrame<T> =
1027-
asColumn { it.map { IMG(it.toString(), width, height) } }
1028-
1029981
// endregion
1030982

1031983
// region toURL
@@ -1051,7 +1003,7 @@ public fun DataColumn<String?>.convertToURL(): DataColumn<URL?> = convertToUrl()
10511003
*/
10521004
@JvmName("convertToUrlFromStringNullable")
10531005
public fun DataColumn<String?>.convertToUrl(): DataColumn<URL?> = map { it?.let { URI(it).toURL() } }
1054-
1006+
// TODO: This function creates a name conflict with toUrl() with the same signature
10551007
@Deprecated(TO_URL, ReplaceWith(TO_URL_REPLACE), DeprecationLevel.ERROR)
10561008
@JvmName("toURLFromStringNullable")
10571009
@Refine
@@ -1079,32 +1031,6 @@ public fun <T> Convert<T, String?>.toURL(): DataFrame<T> = asColumn { it.convert
10791031
@Interpretable("ToSpecificType")
10801032
public fun <T> Convert<T, String?>.toUrl(): DataFrame<T> = asColumn { it.convertToUrl() }
10811033

1082-
@Deprecated(TO_URL, ReplaceWith(TO_URL_REPLACE), DeprecationLevel.ERROR)
1083-
@JvmName("toURLFromString")
1084-
@Refine
1085-
@Converter(URL::class, nullable = false)
1086-
@Interpretable("ToSpecificType")
1087-
public fun <T> Convert<T, String>.toURL(): DataFrame<T> = toUrl()
1088-
1089-
/**
1090-
* Converts values in the [String] columns previously selected with [convert] to an [URL],
1091-
* preserving their original names and positions within the [DataFrame].
1092-
*
1093-
* For more information: {@include [DocumentationUrls.Convert]}
1094-
*
1095-
* ### Examples:
1096-
* ```kotlin
1097-
* df.convert { webAddress }.toUrl()
1098-
* ```
1099-
*
1100-
* @return A new [DataFrame] with the values converted to an [URL].
1101-
*/
1102-
@JvmName("toUrlFromString")
1103-
@Refine
1104-
@Converter(URL::class, nullable = false)
1105-
@Interpretable("ToSpecificType")
1106-
public fun <T> Convert<T, String>.toUrl(): DataFrame<T> = asColumn { it.convertToUrl() }
1107-
11081034
// endregion
11091035

11101036
// region toInstant
@@ -1245,20 +1171,6 @@ public fun DataColumn<DateTimeComponents?>.convertToStdlibInstant(): DataColumn<
12451171
@Deprecated(message = TO_INSTANT, replaceWith = ReplaceWith(TO_INSTANT_REPLACE), level = DeprecationLevel.ERROR)
12461172
public fun <T> Convert<T, String?>.toInstant(): DataFrame<T> = asColumn { it.convertToDeprecatedInstant() }
12471173

1248-
/**
1249-
* __Deprecated__:
1250-
*
1251-
* [kotlinx.datetime.Instant] is deprecated in favor of [kotlin.time.Instant].
1252-
* Either migrate to [kotlin.time.Instant] and use [toStdlibInstant] or use [toDeprecatedInstant].
1253-
* This function will be migrated to [kotlin.time.Instant] in 1.1.
1254-
*/
1255-
@JvmName("toInstantFromString")
1256-
@Refine
1257-
@Converter(DeprecatedInstant::class, nullable = false)
1258-
@Interpretable("ToSpecificType")
1259-
@Deprecated(message = TO_INSTANT, replaceWith = ReplaceWith(TO_INSTANT_REPLACE), level = DeprecationLevel.ERROR)
1260-
public fun <T> Convert<T, String>.toInstant(): DataFrame<T> = asColumn { it.convertToDeprecatedInstant() }
1261-
12621174
/**
12631175
* Converts values in the [String] columns previously selected with [convert] to [kotlinx.datetime.Instant],
12641176
* preserving their original names and positions within the [DataFrame].
@@ -1286,32 +1198,6 @@ public fun <T> Convert<T, String>.toInstant(): DataFrame<T> = asColumn { it.conv
12861198
)
12871199
public fun <T> Convert<T, String?>.toDeprecatedInstant(): DataFrame<T> = asColumn { it.convertToDeprecatedInstant() }
12881200

1289-
/**
1290-
* Converts values in the [String] columns previously selected with [convert] to [kotlinx.datetime.Instant],
1291-
* preserving their original names and positions within the [DataFrame].
1292-
*
1293-
* For more information: {@include [DocumentationUrls.Convert]}
1294-
*
1295-
* ### Examples:
1296-
* ```kotlin
1297-
* df.convert { timestamp }.toDeprecatedInstant()
1298-
* ```
1299-
*
1300-
* Migrate to [kotlin.time.Instant] and use [convertToStdlibInstant] at your own pace.
1301-
*
1302-
* @return A new [DataFrame] with the values converted to [kotlinx.datetime.Instant].
1303-
*/
1304-
@JvmName("toDeprecatedInstantFromString")
1305-
@Refine
1306-
@Converter(DeprecatedInstant::class, nullable = false)
1307-
@Interpretable("ToSpecificType")
1308-
@Deprecated(
1309-
message = TO_DEPRECATED_INSTANT,
1310-
replaceWith = ReplaceWith(TO_DEPRECATED_INSTANT_REPLACE),
1311-
level = DeprecationLevel.WARNING,
1312-
)
1313-
public fun <T> Convert<T, String>.toDeprecatedInstant(): DataFrame<T> = asColumn { it.convertToDeprecatedInstant() }
1314-
13151201
/**
13161202
* Converts values in the [String] columns previously selected with [convert] to [kotlin.time.Instant],
13171203
* preserving their original names and positions within the [DataFrame].
@@ -1334,27 +1220,6 @@ public fun <T> Convert<T, String>.toDeprecatedInstant(): DataFrame<T> = asColumn
13341220
@Interpretable("ToSpecificType")
13351221
public fun <T> Convert<T, String?>.toStdlibInstant(): DataFrame<T> = asColumn { it.convertToStdlibInstant() }
13361222

1337-
/**
1338-
* Converts values in the [String] columns previously selected with [convert] to [kotlin.time.Instant],
1339-
* preserving their original names and positions within the [DataFrame].
1340-
*
1341-
* For more information: {@include [DocumentationUrls.Convert]}
1342-
*
1343-
* ### Examples:
1344-
* ```kotlin
1345-
* df.convert { timestamp }.toStdlibInstant()
1346-
* ```
1347-
*
1348-
* This function will be renamed to `.toInstant()` in 1.1.
1349-
*
1350-
* @return A new [DataFrame] with the values converted to [kotlin.time.Instant].
1351-
*/
1352-
@JvmName("toStdlibInstantFromString")
1353-
@Refine
1354-
@Converter(StdlibInstant::class, nullable = false)
1355-
@Interpretable("ToSpecificType")
1356-
public fun <T> Convert<T, String>.toStdlibInstant(): DataFrame<T> = asColumn { it.convertToStdlibInstant() }
1357-
13581223
/**
13591224
* Converts values in the [kotlinx.datetime.Instant] columns previously selected with [convert] to [kotlin.time.Instant],
13601225
* preserving their original names and positions within the [DataFrame].
@@ -3656,48 +3521,6 @@ public fun DataColumn<String?>.convertToJavaLocalDateTime(
36563521
@Interpretable("ToSpecificType")
36573522
public fun <T> Convert<T, Any?>.toJavaLocalDateTime(): DataFrame<T> = to<JavaLocalDateTime?>()
36583523

3659-
/**
3660-
* Converts values in the [String] columns previously selected with [convert] to [JavaLocalDateTime],
3661-
* preserving their original names and positions within the [DataFrame].
3662-
*
3663-
* Trims each string and attempts to parse it using the specified [formatter] and [locale].
3664-
* Fails with an exception if a value cannot be parsed.
3665-
*
3666-
* For more information: {@include [DocumentationUrls.Convert]}
3667-
*
3668-
* @param formatter An optional [DateTimeFormatter] to use for parsing. If `null`, default parsers are used.
3669-
* @param locale An optional [Locale] for parsing. If `null`, the default locale is used.
3670-
* @return A new [DataFrame] with the values converted to [JavaLocalDateTime].
3671-
*/
3672-
@JvmName("toJavaLocalDateTimeFromString")
3673-
@Refine
3674-
@Converter(JavaLocalDateTime::class, nullable = false)
3675-
@Interpretable("ToSpecificTypePattern")
3676-
public fun <T> Convert<T, String>.toJavaLocalDateTime(
3677-
formatter: DateTimeFormatter? = null,
3678-
locale: Locale? = null,
3679-
): DataFrame<T> = asColumn { it.convertToJavaLocalDateTime(formatter, locale) }
3680-
3681-
/**
3682-
* Converts values in the [String] columns previously selected with [convert] to [JavaLocalDateTime],
3683-
* preserving their original names and positions within the [DataFrame].
3684-
*
3685-
* Trims each string and attempts to parse it using the specified [pattern] and [locale].
3686-
* Fails with an exception if a value cannot be parsed.
3687-
*
3688-
* For more information: {@include [DocumentationUrls.Convert]}
3689-
*
3690-
* @param pattern A date-time pattern to use for parsing (e.g., `"yyyy-MM-dd HH:mm:ss"`).
3691-
* @param locale An optional [Locale] for parsing. If `null`, the default locale is used.
3692-
* @return A new [DataFrame] with the values converted to [JavaLocalDateTime].
3693-
*/
3694-
@JvmName("toJavaLocalDateTimeFromStringPattern")
3695-
@Refine
3696-
@Converter(JavaLocalDateTime::class, nullable = false)
3697-
@Interpretable("ToSpecificTypePattern")
3698-
public fun <T> Convert<T, String>.toJavaLocalDateTime(pattern: String, locale: Locale? = null): DataFrame<T> =
3699-
asColumn { it.convertToJavaLocalDateTime(pattern, locale) }
3700-
37013524
/**
37023525
* Converts values in the nullable [String] columns previously selected with [convert] to [JavaLocalDateTime],
37033526
* preserving their original names and positions within the [DataFrame].

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import kotlin.math.roundToLong
3333
import kotlin.random.Random
3434
import kotlin.reflect.full.starProjectedType
3535
import kotlin.reflect.typeOf
36+
import kotlin.test.assertEquals
3637
import kotlin.time.Duration
3738
import kotlin.time.Duration.Companion.hours
3839
import kotlin.time.Duration.Companion.milliseconds
@@ -613,4 +614,12 @@ class ConvertTests {
613614
dataFrameOf("a")(1, 2, 3).cast<Marker>().convert { a }.with { it }
614615
}
615616
}
617+
618+
@Test
619+
fun `convert to Double preserves nullability of the column type`() {
620+
val df = dataFrameOf("a" to columnOf(123, 322), "b" to columnOf(1.0, null))
621+
val converted = df.convert { "a"<Int>() and "b"<Double?>() }.toDouble()
622+
converted["a"].type() shouldBe typeOf<Double>()
623+
converted["b"].type() shouldBe typeOf<Double?>()
624+
}
616625
}

0 commit comments

Comments
 (0)