Skip to content

Commit 3cc2006

Browse files
Merge pull request #1402 from Kotlin/rename_kdocs
Rename kdocs
2 parents e96604e + 6bc29f3 commit 3cc2006

File tree

3 files changed

+223
-10
lines changed

3 files changed

+223
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ internal interface ConvertDocs {
141141
* {@include [DslGrammarLink]}
142142
* {@include [LineBreak]}
143143
*
144-
* **[`convert`][convert]**` { columnsSelector: `[`ColumnsSelector`][ColumnsSelector]` }`
144+
* **[`convert`][DataFrame.convert]**` { columnsSelector: `[`ColumnsSelector`][ColumnsSelector]` }`
145145
*
146146
* {@include [Indent]}
147147
* __`.`__[**`with`**][Convert.with]`(infer: `[`Infer`][Infer]`, rowExpression: `[`RowValueExpression`][RowValueExpression]`)`

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

Lines changed: 219 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
1414
import org.jetbrains.kotlinx.dataframe.columns.renamedReference
1515
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
1616
import org.jetbrains.kotlinx.dataframe.documentation.AccessApiLink
17+
import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls
18+
import org.jetbrains.kotlinx.dataframe.documentation.DslGrammarLink
1719
import org.jetbrains.kotlinx.dataframe.documentation.DslGrammarTemplateColumnsSelectionDsl.DslGrammarTemplate
20+
import org.jetbrains.kotlinx.dataframe.documentation.ExcludeFromSources
21+
import org.jetbrains.kotlinx.dataframe.documentation.Indent
22+
import org.jetbrains.kotlinx.dataframe.documentation.LineBreak
23+
import org.jetbrains.kotlinx.dataframe.documentation.SelectingColumns
1824
import org.jetbrains.kotlinx.dataframe.impl.api.renameImpl
1925
import org.jetbrains.kotlinx.dataframe.impl.columnName
2026
import org.jetbrains.kotlinx.dataframe.impl.toCamelCaseByDelimiters
@@ -23,12 +29,108 @@ import kotlin.reflect.KProperty
2329

2430
// region DataFrame
2531

32+
/**
33+
* Renames the specified [columns\] keeping their original values and location within the [DataFrame].
34+
*
35+
* This function does not immediately rename the columns but instead selects columns to rename and
36+
* returns a [RenameClause],
37+
* which serves as an intermediate step.
38+
* The [RenameClause] object provides methods to rename selected columns using:
39+
* - [into(name)][RenameClause.into] - renames selected columns to the specified names.
40+
* - [into { nameExpression }][RenameClause.into] - renames selected columns using a provided
41+
* expression assuming column with its path and returning a new name.
42+
* - [toCamelCase()][RenameClause.toCamelCase] - renames all selected columns to "camelCase".
43+
*
44+
* Each method returns a new [DataFrame] with the renamed columns.
45+
*
46+
* Check out [Grammar].
47+
*
48+
* @include [SelectingColumns.ColumnGroupsAndNestedColumnsMention]
49+
*
50+
* See [Selecting Columns][RenameSelectingOptions].
51+
*
52+
* For more information: {@include [DocumentationUrls.Rename]}
53+
*
54+
* See also [renameToCamelCase] which renames all columns to "camelCase" format.
55+
*/
56+
internal interface RenameDocs {
57+
58+
/**
59+
* {@comment Version of [SelectingColumns] with correctly filled in examples}
60+
* @include [SelectingColumns] {@include [SetRenameOperationArg]}
61+
*/
62+
interface RenameSelectingOptions
63+
64+
/**
65+
* ## Rename Operation Grammar
66+
* {@include [LineBreak]}
67+
* {@include [DslGrammarLink]}
68+
* {@include [LineBreak]}
69+
*
70+
* [**`rename`**][rename]**` { `**`columnsSelector: `[`ColumnsSelector`][ColumnsSelector]` `**`}`**
71+
*
72+
* {@include [Indent]}
73+
* `| `__`.`__[**`into`**][RenameClause.into]**`(`**`name: `[`String`][String]**`)`**
74+
*
75+
* {@include [Indent]}
76+
* `| `__`.`__[**`into`**][RenameClause.into]**` { `**`nameExpression: (`[`ColumnWithPath`][ColumnWithPath]`) -> `[String]` `**`}`**
77+
*
78+
* {@include [Indent]}
79+
* `| `__`.`__[**`toCamelCase`**][RenameClause.toCamelCase]**`()`**
80+
*/
81+
interface Grammar
82+
}
83+
84+
/** {@set [SelectingColumns.OPERATION] [rename][rename]} */
85+
@ExcludeFromSources
86+
private interface SetRenameOperationArg
87+
88+
/**
89+
* {@include [RenameDocs]}
90+
* ### This Rename Overload
91+
*/
92+
@ExcludeFromSources
93+
private interface CommonRenameDocs
94+
95+
/**
96+
* Renames columns in the [DataFrame].
97+
*
98+
* This function allows renaming multiple columns in a single call by supplying a list of name pairs.
99+
* Each pair consists of the current column name and the desired new name.
100+
*
101+
* See also [renameToCamelCase] which renames all columns to "camelCase" format.
102+
*
103+
* Example:
104+
* ```kotlin
105+
* df.rename("oldName1" to "newName1", "oldName2" to "newName2")
106+
* ```
107+
*
108+
* @param mappings A vararg of pairs where each pair consists of the original column name (`first`)
109+
* and the new column name (`second`).
110+
* @return A new [DataFrame] with the renamed columns.
111+
*/
26112
@Refine
27113
@Interpretable("RenameMapping")
28114
public fun <T> DataFrame<T>.rename(vararg mappings: Pair<String, String>): DataFrame<T> =
29115
rename { mappings.map { it.first.toColumnAccessor() }.toColumnSet() }
30116
.into(*mappings.map { it.second }.toTypedArray())
31117

118+
/**
119+
* @include [CommonRenameDocs]
120+
* @include [SelectingColumns.Dsl] {@include [SetRenameOperationArg]}
121+
* ### Examples:
122+
* ```kotlin
123+
* // Rename "col1" to "width" and "col2" to "length"
124+
* df.rename { col1 and col2 }.into("width", "length")
125+
*
126+
* // Rename all columns using their full path, delimited by "->"
127+
* df.rename { colsAtAnyDepth() }.into { it.path.joinToString("->") }
128+
*
129+
* // Renames all numeric columns to "camelCase"
130+
* df.rename { colsOf<Number>() }.toCamelCase()
131+
* ```
132+
* @param [columns\] The [Columns Selector][ColumnsSelector] used to select the columns of this [DataFrame] to group.
133+
*/
32134
@Interpretable("Rename")
33135
public fun <T, C> DataFrame<T>.rename(columns: ColumnsSelector<T, C>): RenameClause<T, C> = RenameClause(this, columns)
34136

@@ -41,17 +143,47 @@ public fun <T, C> DataFrame<T>.rename(vararg cols: ColumnReference<C>): RenameCl
41143
@AccessApiOverload
42144
public fun <T, C> DataFrame<T>.rename(vararg cols: KProperty<C>): RenameClause<T, C> = rename { cols.toColumnSet() }
43145

146+
/**
147+
* @include [CommonRenameDocs]
148+
* @include [SelectingColumns.ColumnNames] {@include [SetRenameOperationArg]}
149+
* ### Examples:
150+
* ```kotlin
151+
* // Rename "col1" to "width" and "col2" to "length"
152+
* df.rename("col1", "col2").into("width", "length")
153+
*
154+
* // Renames "arrival_date" and "passport-ID" columns to "camelCase"
155+
* df.rename("arrival_date", "passport-ID").toCamelCase()
156+
* ```
157+
* @param [columns\] The [Columns Names][String] used to select the columns of this [DataFrame] to group.
158+
*/
44159
public fun <T> DataFrame<T>.rename(vararg cols: String): RenameClause<T, Any?> = rename { cols.toColumnSet() }
45160

161+
/**
162+
* An intermediate class used in the [rename] operation.
163+
*
164+
* This class itself does not perform any renaming — it is a transitional step
165+
* before specifying how to rename the selected columns.
166+
* It must be followed by one of the renaming methods
167+
* to produce a new [DataFrame] with renamed columns.
168+
*
169+
* The resulting columns will keep their original values and positions
170+
* in the [DataFrame], but their names will be changed.
171+
*
172+
* Use the following methods to perform the conversion:
173+
* - [into(name)][RenameClause.into] — renames selected columns to the specified names.
174+
* - [into { nameExpression }][RenameClause.into] — renames selected columns using a custom expression,
175+
* which takes the column and its path and returns a new name.
176+
* - [toCamelCase()][RenameClause.toCamelCase] — renames all selected columns to `camelCase`.
177+
*
178+
* See [Grammar][RenameDocs.Grammar] for more details.
179+
*/
46180
@HasSchema(schemaArg = 0)
47181
public class RenameClause<T, C>(internal val df: DataFrame<T>, internal val columns: ColumnsSelector<T, C>) {
48182
override fun toString(): String = "RenameClause(df=$df, columns=$columns)"
49183
}
50184

51185
/**
52-
* ## Rename to "camelCase"
53-
*
54-
* This function renames all columns in this [DataFrame] to the "camelCase" format.
186+
* Renames all columns in this [DataFrame] to the "camelCase" format.
55187
*
56188
* Removes all delimiters between words and capitalizes each word except the first one.
57189
* Adds an underscore between consecutive numbers.
@@ -64,14 +196,15 @@ public class RenameClause<T, C>(internal val df: DataFrame<T>, internal val colu
64196
*
65197
* Returns a [DataFrame] with updated column names.
66198
*
67-
* ### Examples:
199+
* ### Renaming Examples
68200
* ```
69201
* "snake_case_name" -> "snakeCaseName"
70202
* "PascalCaseName" -> "pascalCaseName"
71203
* "doner-case-name" -> "donerCaseName"
72204
* "UPPER_CASE_NAME -> upperCaseName"
73205
* ```
74206
*
207+
* @see [rename]
75208
* @return a [DataFrame] with column names converted to "camelCase" format.
76209
*/
77210
@Refine
@@ -93,6 +226,30 @@ public fun <T> DataFrame<T>.renameToCamelCase(): DataFrame<T> =
93226
public fun <T, C> RenameClause<T, C>.into(vararg newColumns: ColumnReference<*>): DataFrame<T> =
94227
into(*newColumns.map { it.name() }.toTypedArray())
95228

229+
/**
230+
* Renames the columns selected with [rename] to the specified [newNames],
231+
* preserving their values and positions within the [DataFrame].
232+
*
233+
* The mapping is positional: [newNames] are applied in the order
234+
* the columns were selected — the first selected column is renamed to the first name,
235+
* the second to the second, and so on.
236+
*
237+
* For more information: {@include [DocumentationUrls.Rename]}
238+
*
239+
* Check out [Grammar][RenameDocs.Grammar].
240+
*
241+
* ### Examples:
242+
* ```kotlin
243+
* // Rename "col1" to "width" and "col2" to "length"
244+
* df.rename("col1", "col2").into("width", "length")
245+
*
246+
* // Rename "col1" to "width" and "col2" to "length"
247+
* df.rename { col1 and col2 }.into("width", "length")
248+
* ```
249+
*
250+
* @param newNames The new names for the selected columns, applied in order of selecting.
251+
* @return A new [DataFrame] with the columns renamed.
252+
*/
96253
@Refine
97254
@Interpretable("RenameInto")
98255
public fun <T, C> RenameClause<T, C>.into(vararg newNames: String): DataFrame<T> = renameImpl(newNames)
@@ -102,13 +259,35 @@ public fun <T, C> RenameClause<T, C>.into(vararg newNames: String): DataFrame<T>
102259
public fun <T, C> RenameClause<T, C>.into(vararg newNames: KProperty<*>): DataFrame<T> =
103260
into(*newNames.map { it.name }.toTypedArray())
104261

262+
/**
263+
* Renames the columns selected with [rename] by applying the [transform] expression
264+
* to each of them. This expression receives the column together with its full path
265+
* (as [ColumnWithPath]) and must return the new name for that column.
266+
* The operation preserves the original columns’ values and their positions within the [DataFrame].
267+
*
268+
* For more information: {@include [DocumentationUrls.Rename]}
269+
*
270+
* Check out [Grammar][RenameDocs.Grammar] for more details.
271+
*
272+
* ### Examples:
273+
* ```kotlin
274+
* // Rename all columns using their full path, delimited by "->"
275+
* df.rename { colsAtAnyDepth() }.into { it.path.joinToString("->") }
276+
*
277+
* // Rename all `String` columns with uppercase
278+
* df.rename { colsOf<String>() }.into { it.name.uppercase() }
279+
* ```
280+
*
281+
* @param transform A function that takes a [ColumnWithPath] for each selected column
282+
* and returns the new column name.
283+
* @return A new [DataFrame] with the columns renamed.
284+
*/
105285
public fun <T, C> RenameClause<T, C>.into(transform: (ColumnWithPath<C>) -> String): DataFrame<T> =
106286
renameImpl(transform)
107287

108288
/**
109-
* ## Rename to "camelCase"
110-
*
111289
* Renames the columns, previously selected with [rename] to "camelCase" format.
290+
*
112291
* All delimiters between words are removed, words are capitalized except for the first one.
113292
* Places underscore between numbers.
114293
* If the string does not contain any letters or numbers, it remains unchanged.
@@ -118,7 +297,15 @@ public fun <T, C> RenameClause<T, C>.into(transform: (ColumnWithPath<C>) -> Stri
118297
* This function supports converting names from `snake_case`, `PascalCase`, and other delimited formats
119298
* into a consistent "camelCase" representation.
120299
*
121-
* ### Examples:
300+
* ### Examples
301+
* ```kotlin
302+
* // Renames "arrival_date" and "passport-ID" columns to "camelCase"
303+
* df.rename("arrival_date", "passport-ID").toCamelCase()
304+
* // Renames all numeric columns to "camelCase"
305+
* df.rename { colsOf<Number>() }.toCamelCase()
306+
* ```
307+
*
308+
* #### Renaming Examples
122309
* ```
123310
* "snake_case_name" -> "snakeCaseName"
124311
* "PascalCaseName" -> "pascalCaseName"
@@ -137,7 +324,6 @@ public fun <T, C> RenameClause<T, C>.toCamelCase(): DataFrame<T> = into { it.ren
137324
// region DataColumn
138325

139326
/**
140-
* ## Rename to camelCase
141327
*
142328
* Renames this column to "camelCase" format.
143329
* All delimiters between words are removed, words are capitalized except for the first one.
@@ -149,7 +335,7 @@ public fun <T, C> RenameClause<T, C>.toCamelCase(): DataFrame<T> = into { it.ren
149335
* This function supports converting names from `snake_case`, `PascalCase`, and other delimited formats
150336
* into a consistent "camelCase" representation.
151337
*
152-
* ### Examples:
338+
* #### Renaming Examples
153339
* ```
154340
* "snake_case_name" -> "snakeCaseName"
155341
* "PascalCaseName" -> "pascalCaseName"
@@ -165,20 +351,44 @@ public fun <T, C : ColumnReference<T>> C.renameToCamelCase(): C =
165351
) as C
166352

167353
@Suppress("UNCHECKED_CAST")
354+
@AccessApiOverload
355+
@Deprecated(DEPRECATED_ACCESS_API)
168356
public fun <T, C : ColumnReference<T>> C.rename(column: KProperty<T>): C = rename(column.columnName) as C
169357

170358
@Suppress("UNCHECKED_CAST")
359+
@AccessApiOverload
360+
@Deprecated(DEPRECATED_ACCESS_API)
171361
public fun <T, C : ColumnReference<T>> C.rename(column: ColumnAccessor<T>): C = rename(column.name()) as C
172362

173363
// endregion
174364

175365
// region named
176366

367+
/**
368+
* Returns a new column reference with the original column values but a new [name].
369+
*
370+
* This is useful when you want to specify an existing column
371+
* (for example, in `select`, `update`, or `rename` operations)
372+
* but give it a different name in the resulting [DataFrame].
373+
*
374+
* ### Example:
375+
* ```kotlin
376+
* // Select "size" column as "dimensions"
377+
* df.select { size named "dimensions" }
378+
* ```
379+
*
380+
* @param name The new name to assign to the column.
381+
* @return A new column with the original structure and values but with the specified [name].
382+
*/
177383
@Suppress("UNCHECKED_CAST")
178384
public infix fun <T, C : ColumnReference<T>> C.named(name: String): C = rename(name) as C
179385

386+
@AccessApiOverload
387+
@Deprecated(DEPRECATED_ACCESS_API)
180388
public infix fun <T, C : ColumnReference<T>> C.named(name: KProperty<*>): C = rename(name)
181389

390+
@AccessApiOverload
391+
@Deprecated(DEPRECATED_ACCESS_API)
182392
public infix fun <T, C : ColumnReference<T>> C.named(name: ColumnAccessor<*>): C = rename(name)
183393

184394
// endregion

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/documentation/DocumentationUrls.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,7 @@ internal interface DocumentationUrls {
134134

135135
/** [See `format` on the documentation website.]({@include [Url]}/format.html) */
136136
interface Format
137+
138+
/** [See `rename` on the documentation website.]({@include [Url]}/rename.html) */
139+
interface Rename
137140
}

0 commit comments

Comments
 (0)