Skip to content

Commit f308ab8

Browse files
committed
update add.md example with API variants
1 parent 572aa39 commit f308ab8

File tree

2 files changed

+87
-2
lines changed
  • core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api
  • docs/StardustDocs/topics

2 files changed

+87
-2
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,21 @@ 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+
824827
@Test
825-
fun addCalculated() {
828+
fun addCalculatedApi() {
829+
// SampleStart
826830
class CityInfo(val city: String?, val population: Int, val location: String)
827-
fun queryCityInfo(city: String?): CityInfo { return CityInfo(city, city?.length ?: 0, "35.5 32.2") }
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() {
828839
// SampleStart
829840
val personWithCityInfo = df.add {
830841
val cityInfo = city.map { queryCityInfo(it) }
@@ -837,6 +848,35 @@ class Modify : TestBase() {
837848
personWithCityInfo["cityInfo"]["population"] shouldBe df.city.map { it?.length ?: 0 }.named("population")
838849
}
839850

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+
840880
@Test
841881
fun addMany_properties() {
842882
// SampleStart

docs/StardustDocs/topics/add.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,24 @@ df.add {
132132

133133
### Create columns using intermediate result
134134

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+
135150
<!---FUN addCalculated-->
151+
<tabs>
152+
<tab title="Properties">
136153

137154
```kotlin
138155
val personWithCityInfo = df.add {
@@ -144,6 +161,34 @@ val personWithCityInfo = df.add {
144161
}
145162
```
146163

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>
147192
<!---END-->
148193

149194
## Add existing column to `DataFrame`

0 commit comments

Comments
 (0)