Skip to content

Commit a8303e1

Browse files
committed
Closed #76. Implemented and tested additional enumerating methods
1 parent d597153 commit a8303e1

2 files changed

Lines changed: 214 additions & 1 deletion

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,30 @@ DataFrameTest >> testInequality [
10581058
self assert: (df5 = df1) not.
10591059
]
10601060

1061+
{ #category : #tests }
1062+
DataFrameTest >> testInjectInto [
1063+
| numericDataFrame actual expected |
1064+
1065+
numericDataFrame := DataFrame
1066+
withRows: #(
1067+
(4.5 86 24)
1068+
(2.3 60 14)
1069+
(1.2 56 16)
1070+
(-1.0 12 18))
1071+
rowNames: #(A B C D)
1072+
columnNames: #(temperature humidity wind).
1073+
1074+
expected := DataSeries
1075+
withKeys: #(temperature humidity wind)
1076+
values: #(7.0 214 72)
1077+
name: '(no name)'.
1078+
1079+
actual := numericDataFrame inject: 0 into:
1080+
[ :sum :each | sum + each ].
1081+
1082+
self assert: actual equals: expected.
1083+
]
1084+
10611085
{ #category : #tests }
10621086
DataFrameTest >> testPrintOn [
10631087

@@ -1072,6 +1096,23 @@ DataFrameTest >> testPrintOn [
10721096
self assert: actual equals: expected.
10731097
]
10741098

1099+
{ #category : #tests }
1100+
DataFrameTest >> testReject [
1101+
| actual expected |
1102+
1103+
expected := DataFrame
1104+
withRows: #(
1105+
(Dubai 2.789 true)
1106+
(London 8.788 false))
1107+
rowNames: #(B C)
1108+
columnNames: df columnNames.
1109+
1110+
actual := df reject:
1111+
[ :row | (row at: #Population) < 2 ].
1112+
1113+
self assert: actual equals: expected.
1114+
]
1115+
10751116
{ #category : #tests }
10761117
DataFrameTest >> testRemoveColumn [
10771118

@@ -1423,6 +1464,23 @@ DataFrameTest >> testRowsPut [
14231464
self assert: dataFrame equals: expected.
14241465
]
14251466

1467+
{ #category : #tests }
1468+
DataFrameTest >> testSelect [
1469+
| actual expected |
1470+
1471+
expected := DataFrame
1472+
withRows: #(
1473+
(Dubai 2.789 true)
1474+
(London 8.788 false))
1475+
rowNames: #(B C)
1476+
columnNames: df columnNames.
1477+
1478+
actual := df select:
1479+
[ :row | (row at: #Population) > 2 ].
1480+
1481+
self assert: actual equals: expected.
1482+
]
1483+
14261484
{ #category : #tests }
14271485
DataFrameTest >> testSortBy [
14281486

@@ -1583,6 +1641,106 @@ DataFrameTest >> testVarSizeInstanceCreation [
15831641
self assert: dataFrame columnNames equals: #(1 2) asOrderedCollection.
15841642
]
15851643

1644+
{ #category : #tests }
1645+
DataFrameTest >> testWithIndexCollect [
1646+
| expectedDf expectedResult actualResult |
1647+
1648+
expectedDf := DataFrame withRows: #(
1649+
(Barcelona 1.609 true)
1650+
(Dubai 2.789 true)
1651+
(London 8.788 false)).
1652+
1653+
expectedDf rowNames: #(A B C).
1654+
expectedDf columnNames: #(City Population BeenThere).
1655+
1656+
expectedResult := DataFrame withRows: #(
1657+
('BARCELONA' 2)
1658+
('DUBAI' 4)
1659+
('LONDON' 11)).
1660+
1661+
expectedResult rowNames: #(A B C).
1662+
expectedResult columnNames: #(City Population).
1663+
1664+
actualResult := df withIndexCollect: [ :row :index |
1665+
row at: #City put: (row at: #City) asUppercase.
1666+
row at: #Population put: (row at: #Population) asInteger + index.
1667+
row removeAt: #BeenThere.
1668+
row ].
1669+
1670+
self assert: actualResult equals: expectedResult.
1671+
self assert: df equals: expectedDf.
1672+
]
1673+
1674+
{ #category : #tests }
1675+
DataFrameTest >> testWithIndexDo [
1676+
1677+
| actual expected |
1678+
1679+
expected := {
1680+
1 -> (DataSeries withKeys: df columnNames values: #(Barcelona 1.609 true) name: 'A') .
1681+
2 -> (DataSeries withKeys: df columnNames values: #(Dubai 2.789 true) name: 'B') .
1682+
3 -> (DataSeries withKeys: df columnNames values: #(London 8.788 false) name: 'C') } asDictionary.
1683+
1684+
actual := Dictionary new.
1685+
1686+
df withIndexDo: [ :each :index |
1687+
actual at: index put: each ].
1688+
1689+
self assert: actual equals: expected.
1690+
]
1691+
1692+
{ #category : #tests }
1693+
DataFrameTest >> testWithIndexDoCanModifyRows [
1694+
| expected |
1695+
1696+
expected := DataFrame
1697+
withRows: #(
1698+
(Barcelona 1.609 true)
1699+
(Dubai 4.789 true)
1700+
(London 11.788 false))
1701+
rowNames: df rowNames
1702+
columnNames: df columnNames.
1703+
1704+
df withIndexDo: [ :row :index |
1705+
row name = 'A'
1706+
ifFalse: [ row at: #Population put: ((row at: #Population) + index) ] ].
1707+
1708+
self assert: df equals: expected.
1709+
]
1710+
1711+
{ #category : #tests }
1712+
DataFrameTest >> testWithIndexReject [
1713+
| actual expected |
1714+
1715+
expected := DataFrame
1716+
withRows: #(
1717+
(Barcelona 1.609 true)
1718+
(London 8.788 false))
1719+
rowNames: #(A C)
1720+
columnNames: df columnNames.
1721+
1722+
actual := df withIndexReject: [ :row :index |
1723+
((row at: #Population) > 2) and: [ index % 2 = 0 ] ].
1724+
1725+
self assert: actual equals: expected.
1726+
]
1727+
1728+
{ #category : #tests }
1729+
DataFrameTest >> testWithIndexSelect [
1730+
| actual expected |
1731+
1732+
expected := DataFrame
1733+
withRows: #(
1734+
(Dubai 2.789 true))
1735+
rowNames: #(B)
1736+
columnNames: df columnNames.
1737+
1738+
actual := df withIndexSelect: [ :row :index |
1739+
((row at: #Population) > 2) and: [ index % 2 = 0 ] ].
1740+
1741+
self assert: actual equals: expected.
1742+
]
1743+
15861744
{ #category : #tests }
15871745
DataFrameTest >> testWithRowNameDo [
15881746

src/DataFrame/DataFrame.class.st

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ DataFrame >> closeTo: aDataFrame [
478478

479479
{ #category : #enumerating }
480480
DataFrame >> collect: aBlock [
481-
"Overrides the Collection>>collect to create DataFrame with the same numer of columns as values in the first row"
481+
"Overrides the Collection>>collect to create DataFrame with the same number of columns as values in the first row"
482482
| firstRow newDataFrame |
483483

484484
firstRow := (self rowAt: 1) copy.
@@ -1175,6 +1175,61 @@ DataFrame >> variance [
11751175
^ self applyToAllColumns: #variance
11761176
]
11771177

1178+
{ #category : #enumerating }
1179+
DataFrame >> withIndexCollect: elementAndIndexBlock [
1180+
"Overrides withIndexCollect: to create DataFrame with the same number of columns as values in the first row"
1181+
| firstRow newDataFrame |
1182+
1183+
firstRow := (self rowAt: 1) copy.
1184+
newDataFrame := self class new: 0@(elementAndIndexBlock value: firstRow value: 1) size.
1185+
newDataFrame columnNames: firstRow keys.
1186+
1187+
self withIndexDo: [ :each :index | newDataFrame add: (elementAndIndexBlock value: each copy value: index)].
1188+
^ newDataFrame
1189+
]
1190+
1191+
{ #category : #enumerating }
1192+
DataFrame >> withIndexDo: elementAndIndexBlock [
1193+
1194+
1 to: self size do: [ :i |
1195+
| row |
1196+
row := (self rowAt: i).
1197+
elementAndIndexBlock value: row value: i.
1198+
1199+
"A hack to allow modification of rows inside do block"
1200+
self rowAt: i put: row asArray ].
1201+
]
1202+
1203+
{ #category : #enumerating }
1204+
DataFrame >> withIndexReject: elementAndIndexBlock [
1205+
"Evaluate aBlock with each of the receiver's elements and index as the arguments.
1206+
Collect into a new collection like the receiver, only those elements for
1207+
which aBlock evaluates to false. Answer the new collection."
1208+
^ self withIndexSelect: [ :row :index | (elementAndIndexBlock value: row value: index) not ]
1209+
]
1210+
1211+
{ #category : #enumerating }
1212+
DataFrame >> withIndexSelect: elementAndIndexBlock [
1213+
"Evaluate aBlock with each of the receiver's elements and index as the arguments.
1214+
Collect into a new collection like the receiver, only those elements for
1215+
which aBlock evaluates to true. Answer the new collection."
1216+
1217+
| rows selectedRowIndices selectedRowNames selectedRowsAsArrays df |
1218+
1219+
rows := self asArrayOfRows.
1220+
selectedRowIndices := (1 to: rows size) select: [ :i |
1221+
elementAndIndexBlock value: (rows at: i) value: i ].
1222+
selectedRowNames := selectedRowIndices collect: [ :i | self rowNames at: i ].
1223+
selectedRowsAsArrays := selectedRowIndices collect: [ :i | (rows at: i) asArray ].
1224+
1225+
df := self class
1226+
withRows: selectedRowsAsArrays
1227+
rowNames: selectedRowNames
1228+
columnNames: self columnNames.
1229+
1230+
^ df
1231+
]
1232+
11781233
{ #category : #enumerating }
11791234
DataFrame >> withRowNameDo: elementAndKeyBlock [
11801235

0 commit comments

Comments
 (0)