-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupportedAPI.kt
executable file
·294 lines (244 loc) · 8.44 KB
/
SupportedAPI.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.annotations.Import
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.io.*
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.io.File
import java.net.URL
import java.time.LocalDateTime
@DataSchema
interface Marker {
val a: String
val b: Int
}
@DataSchema
interface Explode {
val primitives: List<Int>
val frameColumn: DataFrame<Marker>
}
@DataSchema
interface Convert {
val timestamp: String
}
@DataSchema
interface Join1 {
val a: Int
val b: Int
}
@DataSchema
interface JoinLeaf {
val something: Int
val somethingElse: String
}
@DataSchema
interface Join2 {
val c: DataRow<JoinLeaf>
}
@DataSchema
interface Cast {
val a: Int
val b: String
val c: Int
}
@DataSchema
data class Rows(val a: Int, val b: Int)
object SupportedAPI {
fun dataFrameOf() {
// Special constructor for classes annotated with @DataSchema
// Convenient way to create dataframe from rows
val df: DataFrame<Rows> = dataFrameOf(Rows(1, 2))
df.a
val df1 = df.append(Rows(3, 4))
}
fun dataFrameOfInvoke() {
val df = dataFrameOf("a", "b")(1, listOf(2, 3, 4))
df[0].a
df[0].b.size
}
fun explode(df: DataFrame<Explode>) {
val df1 = df.explode { primitives and frameColumn }
}
private fun explode1(df: DataFrame<Explode>) = df.explode { primitives and frameColumn }
fun ungroup(df: DataFrame<Explode>) {
val df1 = df
.explode { frameColumn }
.ungroup { frameColumn }
df1.a
}
fun group(df: DataFrame<Explode>) {
val df1 = df.group { primitives and frameColumn }.into("group")
df1.group.primitives
}
fun parse(s: String): LocalDateTime = error("materialize LocalDateTime")
fun convert(df: DataFrame<Convert>) {
df.convert { timestamp }.with { parse(it) }.timestamp
}
fun join(df1: DataFrame<Join1>, df2: DataFrame<Join2>) {
val res = df1.join(df2) { a.match(right.c.something) }
df1
.add("key") { a }
.join(
df2.add("key") { c.something }
) {
key.match(right.key)
}
}
fun addDsl() {
fun Document.selectOrFail(query: String) = selectFirst(query) ?: error(query)
val df =
dataFrameOf("url")(URL("https://blog.jetbrains.com/kotlin/2024/08/track-and-analyze-github-star-growth-with-kandy-and-kotlin-dataframe/"))
.add("document") { Jsoup.parse(url, 3000) }
.add {
"title" from {
document.selectFirst("#major-updates")?.text()
}
"author" {
"name" from {
document
.selectFirst("#main > section > div.content.js-toc-content > div.author-post > div > div.author-post__text > div > a")
?.text()
}
"publishDate" from {
val time = document
.selectOrFail("#main > section > div.content.js-toc-content > div.author-post > div > div.author-post__text > time")
time.text()
}
}
"topics" from {
document.select("h2.wp-block-heading").map { it.text() }
}
}
}
private fun add(df: AnyFrame) = df
.add("a") { 42 }
.add {
"b" from { "" }
a into "c"
}
fun safeCast(df: DataFrame<*>) {
// try removing a column or commenting out a line in the add function and see the error
add(df).cast<Cast>()
}
fun cast(df: DataFrame<*>) {
// DataFrame<*> can be cast to anything
df.cast<Explode>().frameColumn
}
fun read() {
// Argument be either absolute path or path relative to project directory.
val df = @Import DataFrame.readCSV("jetbrains_repositories.csv")
df.full_name
// Execute `assemble` task to "cache" schema from this URL. Works for readJson
val df1 = @Import DataFrame.readJson("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains.json")
df1.repos
}
fun toDataFrame() {
val df = listOf(Record(1, "ab", NestedRecord(3.0), Test1(1, "2"))).toDataFrame(maxDepth = 1)
df.nestedRecord.c
}
fun toDataFrameDsl() {
val df = listOf(Record(1, "ab", NestedRecord(3.0), Test1(1, "2"))).toDataFrame {
properties(maxDepth = 2) {
preserve(NestedRecord::class)
preserve(Record::preserveProperty)
}
}
val nestedRecord: DataColumn<NestedRecord> = df.nestedRecord
val nestedRecord1: DataColumn<Test1> = df.preserveProperty
}
fun toDataFrameColumn() {
val df = listOf(File("")).toDataFrame(columnName = "file")
df.file
}
fun remove(list: List<Record>) {
list.toDataFrame().remove { a }
}
fun select(list: List<Record>) {
val df = list.toDataFrame().select { a and b }
df.a
df.b
}
fun dropNulls(df: DataFrame<*>) {
val nullableInt: Int? = 42
val df = df.add("a") { nullableInt }.dropNulls { a }
val nonNullValues: DataColumn<Int> = df.a
}
fun fillNulls(df: DataFrame<*>) {
val nullableInt: Int? = 42
val df = df.add("a") { nullableInt }.fillNulls { a }.with { 0 }
val nonNullValues: DataColumn<Int> = df.a
}
fun update(df: DataFrame<*>) {
val nullableInt: Int? = 42
val df = df.add("a") { nullableInt }.update { a }.with { 0 }
val nonNullValues: DataColumn<Int> = df.a
val df1 = df.update { a }.with { null }
val nullValues: DataColumn<Int?> = df1.a
}
fun rename() {
val df = listOf(Record(1, "ab", NestedRecord(3.0), Test1(1, "2"))).toDataFrame(maxDepth = 1)
df.nestedRecord.c
val df1 = df.rename { nestedRecord.c and nestedRecord }.into("group", "abc")
df1.group.abc
}
fun groupBy_aggregate(df: DataFrame<ActivePlayer>) {
val df1 = df.groupBy { race and expr { 12 } }.aggregate {
count() into "count"
1 into "i"
}
df1.count
df1.race
}
fun groupBy_toDataFrame(df: DataFrame<ActivePlayer>) {
val df1 = df.groupBy { race and expr { 12 } }.toDataFrame("grouped")
df1.race
df1.grouped[0].timestamp
}
fun castTo(organizations: List<String>) {
val sample = @Import DataFrame.readCSV("jetbrains_repositories.csv")
organizations.forEach { organization ->
val df = DataFrame.readCSV(organization).castTo(sample)
println(organizations)
println("Repositories: ${df.count()}")
println("Top 10:")
df.sortBy { stargazers_count.desc() }.take(10).print()
}
}
fun selectionDsl(df: DataFrame<Join2>) {
df.select { colsAtAnyDepth().colsOf<Int>() }.something
df.ungroup { c }.select { colsOf<String>() }.somethingElse
df.add("a") { 42 }.select { colsOf<Int>() }.a
}
fun flatten() {
val df = dataFrameOf("a", "b", "c", "d")(1, 2, 3, 4)
val grouped = df
.group { a and b }.into("e")
.group { e and c }.into("f")
val flattened = grouped.flatten { f.e }
flattened.f.a
val flattened1 = grouped.flatten { f }
flattened1.a
}
fun moveToTop() {
val df = dataFrameOf("a", "b", "c", "d")(1, 2, 3, 4)
val grouped = df
.group { a and b }.into("e")
.group { e and c }.into("f")
val df1 = grouped.move { f.e.a }.toTop()
df1.a
}
fun addId() {
val df = dataFrameOf("col")("empty").addId()
df.id
}
fun insertUnder() {
val df = dataFrameOf("age")(12).insert("yearOfBirth") { 2024 - age }.under("personalInfo")
df.personalInfo.yearOfBirth
val df1 = df.insert("name") { "Joe" }.under { personalInfo }
df1.personalInfo.name
}
}
class Test1(val a: Int, val b: String)
class NestedRecord(val c: Double)
class Record(val a: Int, val b: String, val nestedRecord: NestedRecord, val preserveProperty: Test1)