Skip to content

Commit dc83eca

Browse files
committed
Closed #74. Made it possible to mutate rows with DataFrame>>do:
1 parent 6fa4b20 commit dc83eca

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,24 @@ DataFrameTest >> testDo [
992992
self assert: actual equals: expected.
993993
]
994994

995+
{ #category : #tests }
996+
DataFrameTest >> testDoCanModifyRows [
997+
| expected |
998+
999+
expected := DataFrame
1000+
withRows: #(
1001+
(BARCELONA 1.609 true)
1002+
(DUBAI 2.789 true)
1003+
(LONDON 8.788 false))
1004+
rowNames: df rowNames
1005+
columnNames: df columnNames.
1006+
1007+
df do: [ :row |
1008+
row at: #City put: (row at: #City) asUppercase ].
1009+
1010+
self assert: df equals: expected.
1011+
]
1012+
9951013
{ #category : #tests }
9961014
DataFrameTest >> testEquality [
9971015

@@ -1586,3 +1604,22 @@ DataFrameTest >> testWithKeyDo [
15861604

15871605
self assert: actual equals: expected.
15881606
]
1607+
1608+
{ #category : #tests }
1609+
DataFrameTest >> testWithKeyDoCanModifyRows [
1610+
| expected |
1611+
1612+
expected := DataFrame
1613+
withRows: #(
1614+
(Barcelona 1.609 true)
1615+
(DUBAI 2.789 true)
1616+
(LONDON 8.788 false))
1617+
rowNames: df rowNames
1618+
columnNames: df columnNames.
1619+
1620+
df withKeyDo: [ :row :key |
1621+
row name = 'A'
1622+
ifFalse: [ row at: #City put: (row at: #City) asUppercase ] ].
1623+
1624+
self assert: df equals: expected.
1625+
]

src/DataFrame/DataFrame.class.st

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,13 @@ DataFrame >> dimensions [
647647
{ #category : #enumerating }
648648
DataFrame >> do: aBlock [
649649
"We enumerate through the data enrties - through rows of a data frame"
650-
650+
| row |
651+
651652
1 to: self numberOfRows do: [ :i |
652-
aBlock value: (self rowAt: i) ].
653+
row := self rowAt: i.
654+
aBlock value: row.
655+
"A hack to allow modification of rows inside do block"
656+
self rowAt: i put: row asArray ].
653657
]
654658

655659
{ #category : #statistics }
@@ -1174,8 +1178,14 @@ DataFrame >> variance [
11741178
{ #category : #enumerating }
11751179
DataFrame >> withKeyDo: elementAndKeyBlock [
11761180

1177-
1 to: self size do: [ :index |
1181+
1 to: self size do: [ :i |
1182+
| row |
1183+
row := (self rowAt: i).
1184+
11781185
elementAndKeyBlock
1179-
value: (self rowAt: index)
1180-
value: (rowNames at: index) ].
1186+
value: row
1187+
value: (rowNames at: i).
1188+
1189+
"A hack to allow modification of rows inside do block"
1190+
self rowAt: i put: row asArray ].
11811191
]

0 commit comments

Comments
 (0)