Skip to content

Dynamicially Change Column Name Defined in Schema #622

Answered by Jolanrensen
hantsy asked this question in Q&A
Discussion options

You must be logged in to vote

If I understand you correctly, this requires just two DataFrame calls.

Given some list of data, we first create a DataFrame with your @DataSchema class:

import java.time.ZoneId
val data = listOf(
    ExampleDataSchema("John", java.time.LocalDateTime.now()),
    ExampleDataSchema("Jane", java.time.LocalDateTime.now())
)

val df = data.toDataFrame()
df
NAME CREATED AT
John 2024-03-14T11:40:41.609217717
Jane 2024-03-14T11:40:41.609257253

Then, we get the zoneId we want to convert to, like

val zoneId = ZoneId.systemDefault()

You just want to rename the column right? Not update the values? Then it's as simple as:

val df2 = df
    .rename { `CREATED AT` }.into { "CREATED DATE($zoneId)"

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hantsy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants