Skip to content

Commit 5d0b973

Browse files
committed
update docs for add
1 parent 332f5a8 commit 5d0b973

File tree

2 files changed

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

2 files changed

+40
-1
lines changed

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

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

824+
@Test
825+
fun addCalculated() {
826+
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") }
828+
// SampleStart
829+
val personWithCityInfo = df.add {
830+
val cityInfo = city.map { queryCityInfo(it) }
831+
"cityInfo" {
832+
cityInfo.map { it.location } into CityInfo::location
833+
cityInfo.map { it.population } into "population"
834+
}
835+
}
836+
// SampleEnd
837+
personWithCityInfo["cityInfo"]["population"] shouldBe df.city.map { it?.length ?: 0 }.named("population")
838+
}
839+
824840
@Test
825841
fun addMany_properties() {
826842
// SampleStart

docs/StardustDocs/topics/add.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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-->
@@ -146,6 +153,22 @@ df.add(df1, df2)
146153

147154
<!---END-->
148155

156+
**Add columns using intermediate result:**
157+
158+
<!---FUN addCalculated-->
159+
160+
```kotlin
161+
val personWithCityInfo = df.add {
162+
val cityInfo = city.map { queryCityInfo(it) }
163+
"cityInfo" {
164+
cityInfo.map { it.location } into CityInfo::location
165+
cityInfo.map { it.population } into "population"
166+
}
167+
}
168+
```
169+
170+
<!---END-->
171+
149172
## addId
150173

151174
Adds column with sequential values 0, 1, 2,... New column will be added in the beginning of columns list and will become the first column in `DataFrame`.

0 commit comments

Comments
 (0)