Skip to content

Commit 4b1f21e

Browse files
authored
Merge pull request #1837 from Kotlin/rename-to
API change: Rename to
2 parents 2e4a2f4 + ce0c183 commit 4b1f21e

27 files changed

Lines changed: 195 additions & 93 deletions

File tree

core/api/core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,6 +3937,8 @@ public final class org/jetbrains/kotlinx/dataframe/api/RenameKt {
39373937
public static final fun rename (Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;Lorg/jetbrains/kotlinx/dataframe/columns/ColumnAccessor;)Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;
39383938
public static final fun renameToCamelCase (Lorg/jetbrains/kotlinx/dataframe/DataFrame;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
39393939
public static final fun renameToCamelCase (Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;)Lorg/jetbrains/kotlinx/dataframe/columns/ColumnReference;
3940+
public static final fun to (Lorg/jetbrains/kotlinx/dataframe/api/RenameClause;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
3941+
public static final fun to (Lorg/jetbrains/kotlinx/dataframe/api/RenameClause;[Ljava/lang/String;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
39403942
public static final fun toCamelCase (Lorg/jetbrains/kotlinx/dataframe/api/RenameClause;)Lorg/jetbrains/kotlinx/dataframe/DataFrame;
39413943
}
39423944

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

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.jetbrains.kotlinx.dataframe.impl.columnName
2727
import org.jetbrains.kotlinx.dataframe.impl.columns.renamedColumn
2828
import org.jetbrains.kotlinx.dataframe.impl.toCamelCaseByDelimiters
2929
import org.jetbrains.kotlinx.dataframe.util.DEPRECATED_ACCESS_API
30+
import org.jetbrains.kotlinx.dataframe.util.RENAME_INTO
3031
import kotlin.reflect.KProperty
3132

3233
// region DataFrame
@@ -38,8 +39,8 @@ import kotlin.reflect.KProperty
3839
* returns a [RenameClause],
3940
* which serves as an intermediate step.
4041
* The [RenameClause] object provides methods to rename selected columns using:
41-
* - [into(name)][RenameClause.into] - renames selected columns to the specified names.
42-
* - [into { nameExpression }][RenameClause.into] - renames selected columns using a provided
42+
* - [to(name)][RenameClause.to] - renames selected columns to the specified names.
43+
* - [to { nameExpression }][RenameClause.to] - renames selected columns using a provided
4344
* expression assuming column with its path and returning a new name.
4445
* - [toCamelCase()][RenameClause.toCamelCase] - renames all selected columns to "camelCase".
4546
*
@@ -72,10 +73,10 @@ internal interface RenameDocs {
7273
* [**`rename`**][rename]**` { `**`columnsSelector: `[`ColumnsSelector`][ColumnsSelector]` `**`}`**
7374
*
7475
* {@include [Indent]}
75-
* `| `__`.`__[**`into`**][RenameClause.into]**`(`**`name: `[`String`][String]**`)`**
76+
* `| `__`.`__[**`to`**][RenameClause.to]**`(`**`newNames: `[`String`][String]**`, ..)`**
7677
*
7778
* {@include [Indent]}
78-
* `| `__`.`__[**`into`**][RenameClause.into]**` { `**`nameExpression: (`[`ColumnWithPath`][ColumnWithPath]`) -> `[String]` `**`}`**
79+
* `| `__`.`__[**`to`**][RenameClause.to]**` { `**`nameExpression: (`[`ColumnWithPath`][ColumnWithPath]`) -> `[String]` `**`}`**
7980
*
8081
* {@include [Indent]}
8182
* `| `__`.`__[**`toCamelCase`**][RenameClause.toCamelCase]**`()`**
@@ -115,18 +116,18 @@ private typealias CommonRenameDocs = Nothing
115116
@Interpretable("RenameMapping")
116117
public fun <T> DataFrame<T>.rename(vararg mappings: Pair<String, String>): DataFrame<T> =
117118
rename { mappings.map { it.first.toColumnAccessor() }.toColumnSet() }
118-
.into(*mappings.map { it.second }.toTypedArray())
119+
.to(*mappings.map { it.second }.toTypedArray())
119120

120121
/**
121122
* @include [CommonRenameDocs]
122123
* @include [SelectingColumns.ColumnsSelectionDsl] {@include [SetRenameOperationArg]}
123124
* ### Examples:
124125
* ```kotlin
125126
* // Rename "col1" to "width" and "col2" to "length"
126-
* df.rename { col1 and col2 }.into("width", "length")
127+
* df.rename { col1 and col2 }.to("width", "length")
127128
*
128129
* // Rename all columns using their full path, delimited by "->"
129-
* df.rename { colsAtAnyDepth() }.into { it.path.joinToString("->") }
130+
* df.rename { colsAtAnyDepth() }.to { it.path.joinToString("->") }
130131
*
131132
* // Renames all numeric columns to "camelCase"
132133
* df.rename { colsOf<Number>() }.toCamelCase()
@@ -151,7 +152,7 @@ public fun <T, C> DataFrame<T>.rename(vararg cols: KProperty<C>): RenameClause<T
151152
* ### Examples:
152153
* ```kotlin
153154
* // Rename "col1" to "width" and "col2" to "length"
154-
* df.rename("col1", "col2").into("width", "length")
155+
* df.rename("col1", "col2").to("width", "length")
155156
*
156157
* // Renames "arrival_date" and "passport-ID" columns to "camelCase"
157158
* df.rename("arrival_date", "passport-ID").toCamelCase()
@@ -172,8 +173,8 @@ public fun <T> DataFrame<T>.rename(vararg cols: String): RenameClause<T, Any?> =
172173
* in the [DataFrame], but their names will be changed.
173174
*
174175
* Use the following methods to perform the conversion:
175-
* - [into(name)][RenameClause.into] — renames selected columns to the specified names.
176-
* - [into { nameExpression }][RenameClause.into] — renames selected columns using a custom expression,
176+
* - [to(name)][RenameClause.to] — renames selected columns to the specified names.
177+
* - [to { nameExpression }][RenameClause.to] — renames selected columns using a custom expression,
177178
* which takes the column and its path and returns a new name.
178179
* - [toCamelCase()][RenameClause.toCamelCase] — renames all selected columns to `camelCase`.
179180
*
@@ -226,9 +227,12 @@ public fun <T> DataFrame<T>.renameToCamelCase(): DataFrame<T> =
226227
@Deprecated(DEPRECATED_ACCESS_API)
227228
@AccessApiOverload
228229
public fun <T, C> RenameClause<T, C>.into(vararg newColumns: ColumnReference<*>): DataFrame<T> =
229-
into(*newColumns.map { it.name() }.toTypedArray())
230+
to(*newColumns.map { it.name() }.toTypedArray())
230231

231232
/**
233+
* __NOTE:__ While you can keep using 'into', we recommend using [to][RenameClause.to] for
234+
* * better readability and more natural English.
235+
*
232236
* Renames the columns selected with [rename] to the specified [newNames],
233237
* preserving their values and positions within the [DataFrame].
234238
*
@@ -254,14 +258,46 @@ public fun <T, C> RenameClause<T, C>.into(vararg newColumns: ColumnReference<*>)
254258
*/
255259
@Refine
256260
@Interpretable("RenameInto")
261+
@Deprecated(message = RENAME_INTO, replaceWith = ReplaceWith("to(*newNames)"))
257262
public fun <T, C> RenameClause<T, C>.into(vararg newNames: String): DataFrame<T> = renameImpl(newNames)
258263

264+
/**
265+
* Renames the columns selected with [rename] to the specified [newNames],
266+
* preserving their values and positions within the [DataFrame].
267+
*
268+
* The mapping is positional: [newNames] are applied in the order
269+
* the columns were selected — the first selected column is renamed to the first name,
270+
* the second to the second, and so on.
271+
*
272+
* For more information: {@include [DocumentationUrls.Rename]}
273+
*
274+
* Check out [Grammar][RenameDocs.Grammar].
275+
*
276+
* ### Examples:
277+
* ```kotlin
278+
* // Rename "col1" to "width" and "col2" to "length"
279+
* df.rename("col1", "col2").to("width", "length")
280+
*
281+
* // Rename "col1" to "width" and "col2" to "length"
282+
* df.rename { col1 and col2 }.to("width", "length")
283+
* ```
284+
*
285+
* @param newNames The new names for the selected columns, applied in order of selecting.
286+
* @return A new [DataFrame] with the columns renamed.
287+
*/
288+
@Refine
289+
@Interpretable("RenameInto")
290+
public fun <T, C> RenameClause<T, C>.to(vararg newNames: String): DataFrame<T> = renameImpl(newNames)
291+
259292
@Deprecated(DEPRECATED_ACCESS_API)
260293
@AccessApiOverload
261294
public fun <T, C> RenameClause<T, C>.into(vararg newNames: KProperty<*>): DataFrame<T> =
262-
into(*newNames.map { it.name }.toTypedArray())
295+
to(*newNames.map { it.name }.toTypedArray())
263296

264297
/**
298+
* __NOTE:__ While you can keep using 'into', we recommend using [to][RenameClause.to] for
299+
* better readability and more natural English.
300+
*
265301
* Renames the columns selected with [rename] by applying the [transform] expression
266302
* to each of them. This expression receives the column together with its full path
267303
* (as [ColumnWithPath]) and must return the new name for that column.
@@ -286,9 +322,37 @@ public fun <T, C> RenameClause<T, C>.into(vararg newNames: KProperty<*>): DataFr
286322
*/
287323
@Refine
288324
@Interpretable("RenameIntoLambda")
325+
@Deprecated(message = RENAME_INTO, replaceWith = ReplaceWith("to(transform)"))
289326
public fun <T, C> RenameClause<T, C>.into(transform: (ColumnWithPath<C>) -> String): DataFrame<T> =
290327
renameImpl(transform)
291328

329+
/**
330+
* Renames the columns selected with [rename] by applying the [transform] expression
331+
* to each of them. This expression receives the column together with its full path
332+
* (as [ColumnWithPath]) and must return the new name for that column.
333+
* The operation preserves the original columns’ values and their positions within the [DataFrame].
334+
*
335+
* For more information: {@include [DocumentationUrls.Rename]}
336+
*
337+
* Check out [Grammar][RenameDocs.Grammar] for more details.
338+
*
339+
* ### Examples:
340+
* ```kotlin
341+
* // Rename all columns using their full path, delimited by "->"
342+
* df.rename { colsAtAnyDepth() }.to { it.path.joinToString("->") }
343+
*
344+
* // Rename all `String` columns with uppercase
345+
* df.rename { colsOf<String>() }.to { it.name.uppercase() }
346+
* ```
347+
*
348+
* @param transform A function that takes a [ColumnWithPath] for each selected column
349+
* and returns the new column name.
350+
* @return A new [DataFrame] with the columns renamed.
351+
*/
352+
@Refine
353+
@Interpretable("RenameIntoLambda")
354+
public fun <T, C> RenameClause<T, C>.to(transform: (ColumnWithPath<C>) -> String): DataFrame<T> = renameImpl(transform)
355+
292356
/**
293357
* Renames the columns, previously selected with [rename] to "camelCase" format.
294358
*
@@ -321,7 +385,7 @@ public fun <T, C> RenameClause<T, C>.into(transform: (ColumnWithPath<C>) -> Stri
321385
*/
322386
@Refine
323387
@Interpretable("RenameToCamelCaseClause")
324-
public fun <T, C> RenameClause<T, C>.toCamelCase(): DataFrame<T> = into { it.renameToCamelCase().name() }
388+
public fun <T, C> RenameClause<T, C>.toCamelCase(): DataFrame<T> = to { it.renameToCamelCase().name() }
325389

326390
// endregion
327391

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.jetbrains.kotlinx.dataframe.api.isColumnGroup
1818
import org.jetbrains.kotlinx.dataframe.api.pathOf
1919
import org.jetbrains.kotlinx.dataframe.api.remove
2020
import org.jetbrains.kotlinx.dataframe.api.rename
21+
import org.jetbrains.kotlinx.dataframe.api.to
2122
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
2223
import org.jetbrains.kotlinx.dataframe.impl.aggregation.AggregatableInternal
2324
import org.jetbrains.kotlinx.dataframe.impl.aggregation.GroupByReceiverImpl
@@ -63,7 +64,7 @@ internal class GroupByImpl<T, G>(
6364
if (groupedColumnName == null || groupedColumnName == groups.name()) {
6465
df
6566
} else {
66-
df.rename(groups).into(groupedColumnName)
67+
df.rename(groups).to(groupedColumnName)
6768
}
6869
}
6970

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/util/deprecationMessages.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,8 @@ internal const val ALL_COLS_EXCEPT_REPLACE = "this.allColsExcept { other }"
304304
internal const val ALL_COLS_EXCEPT_REPLACE_VARARG = "this.allColsExcept { others.toColumnSet() }"
305305
internal const val EXCEPT_REPLACE = "this.except { other }"
306306
internal const val EXCEPT_REPLACE_VARARG = "this.except { others.toColumnSet() }"
307+
308+
internal const val RENAME_INTO =
309+
"'into' has been replaced with 'to', because 'rename to' is more common in English. You can keep using 'into', but we advise using 'to'."
310+
307311
// endregion

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AllExceptTests : ColumnsSelectionDslTests() {
1818

1919
@Test
2020
fun `issue 761`() {
21-
val renamed = df.rename { colsAtAnyDepth() except name.firstName }.into { it.name.uppercase() }
21+
val renamed = df.rename { colsAtAnyDepth() except name.firstName }.to { it.name.uppercase() }
2222
renamed.columnNames() shouldBe listOf("NAME", "AGE", "CITY", "WEIGHT", "ISHAPPY")
2323
renamed.getColumnGroup("NAME").columnNames() shouldBe listOf("firstName", "LASTNAME")
2424

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class ConstructorsTests {
304304

305305
else -> error("Unexpected column name: $it")
306306
}
307-
}.rename { all() }.into { names[it.name.toInt() - 1] }
307+
}.rename { all() }.to { names[it.name.toInt() - 1] }
308308

309309
val df4 = dataFrameOf(names).invoke {
310310
when (it) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.dataframe.api
33
import io.kotest.matchers.shouldBe
44
import org.jetbrains.kotlinx.dataframe.DataRow
55
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
6+
import org.jetbrains.kotlinx.dataframe.api.to
67
import org.junit.Test
78

89
@Suppress("ktlint:standard:argument-list-wrapping")
@@ -72,7 +73,7 @@ class FlattenTests {
7273
val grouped = df
7374
.group("a", "b").into("e")
7475
.group("e", "c").into("f")
75-
.rename { "f"["e"] }.into("a")
76+
.rename { "f"["e"] }.to("a")
7677
val flattened = grouped.flatten { "f"["a"] }
7778
flattened.getColumnGroup("f").columnNames() shouldBe listOf("a", "b", "c")
7879
flattened.ungroup("f") shouldBe df

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.kotest.matchers.shouldNotBe
55
import org.jetbrains.kotlinx.dataframe.api.FormattingDsl.blue
66
import org.jetbrains.kotlinx.dataframe.api.FormattingDsl.red
77
import org.jetbrains.kotlinx.dataframe.api.FormattingDsl.rgb
8+
import org.jetbrains.kotlinx.dataframe.api.to
89
import org.jetbrains.kotlinx.dataframe.io.DisplayConfiguration
910
import org.jetbrains.kotlinx.dataframe.samples.api.TestBase
1011
import org.jetbrains.kotlinx.dataframe.samples.api.age
@@ -329,7 +330,7 @@ class FormatTests : TestBase() {
329330
.groupBy("city").toDataFrame()
330331
.add("cityCopy") { "city"<String>() }
331332
.group("city").into("cityGroup")
332-
.rename("cityCopy").into("city")
333+
.rename("cityCopy").to("city")
333334

334335
val formatted = df.format("city").with { bold and italic and textColor(green) }
335336
val html = formatted.toHtml().toString()

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.dataframe.api
33
import io.kotest.assertions.asClue
44
import io.kotest.assertions.throwables.shouldNotThrowAny
55
import io.kotest.matchers.shouldBe
6+
import org.jetbrains.kotlinx.dataframe.api.to
67
import org.jetbrains.kotlinx.dataframe.impl.columns.asAnyFrameColumn
78
import org.jetbrains.kotlinx.dataframe.samples.api.age
89
import org.junit.Test
@@ -27,8 +28,8 @@ class RenameTests : ColumnsSelectionDslTests() {
2728
4, 5, 6,
2829
)
2930

30-
simpleDf.rename { all() }.into { it.name + "_renamed" } shouldBe renamedDf
31-
simpleDf.rename { all() }.into("a_renamed", "b_renamed", "c_renamed") shouldBe renamedDf
31+
simpleDf.rename { all() }.to { it.name + "_renamed" } shouldBe renamedDf
32+
simpleDf.rename { all() }.to("a_renamed", "b_renamed", "c_renamed") shouldBe renamedDf
3233
}
3334

3435
@Test
@@ -54,7 +55,7 @@ class RenameTests : ColumnsSelectionDslTests() {
5455

5556
groupedDf
5657
.rename { "group" and "group"["a"] }
57-
.into { it.name + "_renamed" } shouldBe renamedDf
58+
.to { it.name + "_renamed" } shouldBe renamedDf
5859
}
5960

6061
@Test
@@ -66,7 +67,7 @@ class RenameTests : ColumnsSelectionDslTests() {
6667

6768
groupedDf
6869
.rename { colsAtAnyDepth() }
69-
.into { it.name + "_renamed" } shouldBe renamedDf
70+
.to { it.name + "_renamed" } shouldBe renamedDf
7071
}
7172

7273
@Test
@@ -80,7 +81,7 @@ class RenameTests : ColumnsSelectionDslTests() {
8081

8182
doubleGroupedDf
8283
.rename { colsAtAnyDepth() }
83-
.into { it.name + "_renamed" } shouldBe renamedDf
84+
.to { it.name + "_renamed" } shouldBe renamedDf
8485
}
8586

8687
interface Person2 {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.jetbrains.kotlinx.dataframe.api.leftJoin
1616
import org.jetbrains.kotlinx.dataframe.api.rename
1717
import org.jetbrains.kotlinx.dataframe.api.rightJoin
1818
import org.jetbrains.kotlinx.dataframe.api.select
19+
import org.jetbrains.kotlinx.dataframe.api.to
1920
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
2021
import org.jetbrains.kotlinx.dataframe.explainer.TransformDataFrameExpressions
2122
import org.junit.Test
@@ -55,7 +56,7 @@ class Join : TestBase() {
5556
@Test
5657
@TransformDataFrameExpressions
5758
fun joinWithMatch_properties() {
58-
val other = other.rename { name }.into("fullName").cast<Right>()
59+
val other = other.rename { name }.to("fullName").cast<Right>()
5960
val joined =
6061
// SampleStart
6162
df.join(other) { name match right.fullName }

0 commit comments

Comments
 (0)