By nature data frames are dynamic objects, column labels depend on the input source and also new columns could be added or deleted while wrangling. Kotlin, in contrast, is a statically typed language and all types are defined and verified ahead of execution. That's why creating a flexible, handy, and, at the same time, safe API to a data frame is tricky.
In the Kotlin DataFrame library we provide four different ways to access columns, and, while they are essentially different, they look pretty similar in the data wrangling DSL.
List of Access APIs
Here's a list of all APIs in order of increasing safety.
Columns are accessed by
stringrepresenting their name. Type-checking is done at runtime, name-checking too.
Column Accessors API
Every column has a descriptor; a variable that represents its name and type.
Columns accessed by the
KPropertyof some class. The name and type of column should match the name and type of property, respectively.
Extension Properties API Extension access properties are generated based on the dataframe schema. The name and type of properties are inferred from the name and type of the corresponding columns.
Here's an example of how the same operations can be performed via different Access APIs:
The String API is the simplest and unsafest of them all. The main advantage of it is that it can be used at any time, including when accessing new columns in chain calls. So we can write something like:
We don't need to interrupt a function call chain and declare a column accessor or generate new properties.
In contrast, generated extension properties are the most convenient and the safest API. Using it, you can always be sure that you work with correct data and types. But its bottleneck is the moment of generation. To get new extension properties you have to run a cell in a notebook, which could lead to unnecessary variable declarations. Currently, we are working on compiler a plugin that generates these properties on the fly while typing!
The Column Accessors API is a kind of trade-off between safety and needs to be written ahead of the execution type declaration. It was designed to better be able to write code in an IDE without a notebook experience. It provides type-safe access to columns but doesn't ensure that the columns really exist in a particular data frame.
The KProperties API is useful when you already have declared classed in your application business logic with fields that correspond columns of a data frame.
Column names checking
Column existence checking
Column Accessors API
Extension Properties API