Skip to content

Commit 0eb1b6e

Browse files
authored
Merge pull request #252 from Kotlin/add-docs
Documentation improvements
2 parents 162cde7 + 9d63206 commit 0eb1b6e

File tree

7 files changed

+142
-9
lines changed

7 files changed

+142
-9
lines changed

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Modify.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,62 @@ class Modify : TestBase() {
821821
// SampleEnd
822822
}
823823

824+
private class CityInfo(val city: String?, val population: Int, val location: String)
825+
private fun queryCityInfo(city: String?): CityInfo { return CityInfo(city, city?.length ?: 0, "35.5 32.2") }
826+
827+
@Test
828+
fun addCalculatedApi() {
829+
// SampleStart
830+
class CityInfo(val city: String?, val population: Int, val location: String)
831+
fun queryCityInfo(city: String?): CityInfo {
832+
return CityInfo(city, city?.length ?: 0, "35.5 32.2")
833+
}
834+
// SampleEnd
835+
}
836+
837+
@Test
838+
fun addCalculated_properties() {
839+
// SampleStart
840+
val personWithCityInfo = df.add {
841+
val cityInfo = city.map { queryCityInfo(it) }
842+
"cityInfo" {
843+
cityInfo.map { it.location } into CityInfo::location
844+
cityInfo.map { it.population } into "population"
845+
}
846+
}
847+
// SampleEnd
848+
personWithCityInfo["cityInfo"]["population"] shouldBe df.city.map { it?.length ?: 0 }.named("population")
849+
}
850+
851+
@Test
852+
fun addCalculated_accessors() {
853+
// SampleStart
854+
val city by column<String?>()
855+
val personWithCityInfo = df.add {
856+
val cityInfo = city().map { queryCityInfo(it) }
857+
"cityInfo" {
858+
cityInfo.map { it.location } into CityInfo::location
859+
cityInfo.map { it.population } into "population"
860+
}
861+
}
862+
// SampleEnd
863+
personWithCityInfo["cityInfo"]["population"] shouldBe df.city.map { it?.length ?: 0 }.named("population")
864+
}
865+
866+
@Test
867+
fun addCalculated_strings() {
868+
// SampleStart
869+
val personWithCityInfo = df.add {
870+
val cityInfo = "city"<String?>().map { queryCityInfo(it) }
871+
"cityInfo" {
872+
cityInfo.map { it.location } into CityInfo::location
873+
cityInfo.map { it.population } into "population"
874+
}
875+
}
876+
// SampleEnd
877+
personWithCityInfo["cityInfo"]["population"] shouldBe df.city.map { it?.length ?: 0 }.named("population")
878+
}
879+
824880
@Test
825881
fun addMany_properties() {
826882
// SampleStart

docs/StardustDocs/d.tree

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<toc-element id="DataColumn.md"/>
2626
<toc-element id="DataRow.md"/>
2727
</toc-element>
28-
<toc-element id="operations.md">
28+
<toc-element id="operations.md"/>
29+
<toc-element toc-title="Operations">
2930
<toc-element id="create.md" show-structure-depth="3">
3031
<toc-element id="createColumn.md" toc-title="DataColumn"/>
3132
<toc-element id="createDataFrame.md" toc-title="DataFrame"/>
@@ -172,4 +173,5 @@
172173
</toc-element>
173174
</toc-element>
174175
<toc-element id="gradleReference.md"/>
176+
<toc-element href="https://github.com/Kotlin/dataframe/tree/master/examples" toc-title="Examples"/>
175177
</product-profile>

docs/StardustDocs/project.ihp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
<images dir="images" version="0.9" web-path="images/" />
88
<vars src="v.list"/>
99
<product src="d.tree" version="0.9" id="dataframe"/>
10+
<settings>
11+
<default-property element-name="toc-element" property-name="show-structure-depth" value="2"/>
12+
</settings>
1013
</ihp>

docs/StardustDocs/topics/add.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Returns `DataFrame` which contains all columns from original `DataFrame` followed by newly added columns. Original `DataFrame` is not modified.
66

7-
**Create new column and add it to `DataFrame`:**
7+
## Create new column and add it to `DataFrame`
88

99
```text
1010
add(columnName: String) { rowExpression }
@@ -55,7 +55,7 @@ df.add("fibonacci") {
5555

5656
<!---END-->
5757

58-
**Create and add several columns to `DataFrame`:**
58+
## Create and add several columns to `DataFrame`
5959

6060
```kotlin
6161
add {
@@ -64,7 +64,14 @@ add {
6464
...
6565
}
6666

67-
columnMapping = column into columnName | columnName from column | columnName from { rowExpression }
67+
columnMapping = column into columnName
68+
| columnName from column
69+
| columnName from { rowExpression }
70+
| columnGroupName {
71+
columnMapping
72+
columnMapping
73+
...
74+
}
6875
```
6976

7077
<!---FUN addMany-->
@@ -123,7 +130,68 @@ df.add {
123130
</tab></tabs>
124131
<!---END-->
125132

126-
**Add existing column to `DataFrame`:**
133+
### Create columns using intermediate result
134+
135+
Consider this API:
136+
137+
<!---FUN addCalculatedApi-->
138+
139+
```kotlin
140+
class CityInfo(val city: String?, val population: Int, val location: String)
141+
fun queryCityInfo(city: String?): CityInfo {
142+
return CityInfo(city, city?.length ?: 0, "35.5 32.2")
143+
}
144+
```
145+
146+
<!---END-->
147+
148+
Use the following approach to add multiple columns by calling the given API only once per row:
149+
150+
<!---FUN addCalculated-->
151+
<tabs>
152+
<tab title="Properties">
153+
154+
```kotlin
155+
val personWithCityInfo = df.add {
156+
val cityInfo = city.map { queryCityInfo(it) }
157+
"cityInfo" {
158+
cityInfo.map { it.location } into CityInfo::location
159+
cityInfo.map { it.population } into "population"
160+
}
161+
}
162+
```
163+
164+
</tab>
165+
<tab title="Accessors">
166+
167+
```kotlin
168+
val city by column<String?>()
169+
val personWithCityInfo = df.add {
170+
val cityInfo = city().map { queryCityInfo(it) }
171+
"cityInfo" {
172+
cityInfo.map { it.location } into CityInfo::location
173+
cityInfo.map { it.population } into "population"
174+
}
175+
}
176+
```
177+
178+
</tab>
179+
<tab title="Strings">
180+
181+
```kotlin
182+
val personWithCityInfo = df.add {
183+
val cityInfo = "city"<String?>().map { queryCityInfo(it) }
184+
"cityInfo" {
185+
cityInfo.map { it.location } into CityInfo::location
186+
cityInfo.map { it.population } into "population"
187+
}
188+
}
189+
```
190+
191+
</tab></tabs>
192+
<!---END-->
193+
194+
## Add existing column to `DataFrame`
127195

128196
<!---FUN addExisting-->
129197

@@ -136,7 +204,7 @@ df + score
136204

137205
<!---END-->
138206

139-
**Add all columns from another `DataFrame`:**
207+
## Add all columns from another `DataFrame`
140208

141209
<!---FUN addDfs-->
142210

docs/StardustDocs/topics/operations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[//]: # (title: Operations)
1+
[//]: # (title: Operations Overview)
22

33
<!---IMPORT org.jetbrains.kotlinx.dataframe.samples.api.Modify-->
44

docs/StardustDocs/topics/types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[//]: # (title: Types)
1+
[//]: # (title: Data Abstractions)
22

33
* [`DataColumn`](DataColumn.md) is a named, typed and ordered collection of elements
44
* [`DataFrame`](DataFrame.md) consists of one or several [`DataColumns`](DataColumn.md) with unique names and equal size

examples/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Examples of Kotlin Dataframe
22

33
### Idea examples
4-
* [movies](idea-examples/movies)
4+
* [movies](idea-examples/movies) Using 3 different [Access APIs](https://kotlin.github.io/dataframe/apilevels.html) to perform data cleaning task
55
* [titanic](idea-examples/titanic)
66
* [youtube](idea-examples/youtube)
7+
* [json](idea-examples/json) Using OpenAPI support in DataFrame's Gradle and KSP plugins to access data from [API guru](https://apis.guru/) in a type-safe manner
78

89
### Notebook examples
910

11+
* people [Datalore](https://datalore.jetbrains.com/view/notebook/aOTioEClQQrsZZBKeUPAQj)
12+
Small artificial dataset used in [DataFrame API examples](https://kotlin.github.io/dataframe/operations.html)
13+
1014
* puzzles ([Jupyter](jupyter-notebooks/puzzles/40%20puzzles.ipynb)/[Datalore](https://datalore.jetbrains.com/view/notebook/CVp3br3CDXjUGaxxqfJjFF)) &ndash;
1115
Inspired [by 100 pandas puzzles](https://github.com/ajcr/100-pandas-puzzles). You will go from the simplest tasks to
1216
complex problems where need to think. This notebook will show you how to solve these tasks with the Kotlin

0 commit comments

Comments
 (0)