Skip to content

Added required lowercase conversion for all the columns #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,26 @@ public fun <T, C> DataFrame<T>.rename(cols: Iterable<ColumnReference<C>>): Renam
public data class RenameClause<T, C>(val df: DataFrame<T>, val columns: ColumnsSelector<T, C>)

public fun <T> DataFrame<T>.renameToCamelCase(): DataFrame<T> {
// recursively rename all column groups to camel case
return rename {
dfs { it.isColumnGroup() && it.name() matches DELIMITED_STRING_REGEX }
}.toCamelCase()
// recursively rename all other columns to camel case
.rename {
dfs { !it.isColumnGroup() && it.name() matches DELIMITED_STRING_REGEX }
}.toCamelCase()

// take all frame columns recursively and call renameToCamelCase() on all dataframes inside
.update {
dfsOf<AnyFrame>()
}.with { it.renameToCamelCase() }

// convert all first chars of all columns to the lowercase
.rename {
allDfs()
}.into {
it.name.replaceFirstChar { it.lowercaseChar() }
}
}

public fun <T, C> RenameClause<T, C>.into(vararg newColumns: ColumnReference<*>): DataFrame<T> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ class RenameTests {
}
}
}

@Test
fun `rename to camelCase`() {
val dfWithUpperCaseColumnNames = dataFrameOf("First_Column", "second_column", "ThirdColumn")(1, 2, 3)
val df = dfWithUpperCaseColumnNames.renameToCamelCase()
df.columnNames() shouldBe listOf("firstColumn", "secondColumn", "thirdColumn")
}
}
11 changes: 11 additions & 0 deletions core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/rename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,26 @@ public fun <T, C> DataFrame<T>.rename(cols: Iterable<ColumnReference<C>>): Renam
public data class RenameClause<T, C>(val df: DataFrame<T>, val columns: ColumnsSelector<T, C>)

public fun <T> DataFrame<T>.renameToCamelCase(): DataFrame<T> {
// recursively rename all column groups to camel case
return rename {
dfs { it.isColumnGroup() && it.name() matches DELIMITED_STRING_REGEX }
}.toCamelCase()
// recursively rename all other columns to camel case
.rename {
dfs { !it.isColumnGroup() && it.name() matches DELIMITED_STRING_REGEX }
}.toCamelCase()

// take all frame columns recursively and call renameToCamelCase() on all dataframes inside
.update {
dfsOf<AnyFrame>()
}.with { it.renameToCamelCase() }

// convert all first chars of all columns to the lowercase
.rename {
allDfs()
}.into {
it.name.replaceFirstChar { it.lowercaseChar() }
}
}

public fun <T, C> RenameClause<T, C>.into(vararg newColumns: ColumnReference<*>): DataFrame<T> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ class RenameTests {
}
}
}

@Test
fun `rename to camelCase`() {
val dfWithUpperCaseColumnNames = dataFrameOf("First_Column", "second_column", "ThirdColumn")(1, 2, 3)
val df = dfWithUpperCaseColumnNames.renameToCamelCase()
df.columnNames() shouldBe listOf("firstColumn", "secondColumn", "thirdColumn")
}
}