Hi everyone! I am starting with Kotlin DataFrame. ...
# datascience
d
Hi everyone! I am starting with Kotlin DataFrame. I have a table with a
kotlinx.datetime.Instant
column
timestamp
and would like to add a new column with the difference between the timestamps of the current row and the previous row. It seems I would need something similar to Panda's
diff
operation but I can't find anything in Kotlin DataFrame documentation. How can I do this?
n
Hi!
Copy code
val timestamp by column<Instant>()
val df = dataFrameOf(timestamp)(
    Instant.fromEpochMilliseconds(1000),
    Instant.fromEpochMilliseconds(3000),
    Instant.fromEpochMilliseconds(5000),
    Instant.fromEpochMilliseconds(3000),
)

val diff  = df.add("diff") { prev()?.let { prev -> it[timestamp] - prev[timestamp] }  }
It seems we missing an overload for Instant, otherwise it should have worked like this:
Copy code
val timestamp by column<Int>()
val df = dataFrameOf(timestamp)(
    1000,
    3000,
    5000,
    3000,
)

val diff  = df.add("diff") { diff { timestamp() }  }
diff.print()
d
Thanks @Nikita Klimenko [JB].