Skip to content

Commit 46e47f2

Browse files
committed
Closed #84. Implemented additional enumeration methods for DataFrame
1 parent 5e25cd2 commit 46e47f2

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

src/DataFrame-Tests/DataSeriesTest.class.st

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,34 @@ DataSeriesTest >> testCrossTabulateWith [
466466
self assert: (series1 crossTabulateWith: series2) equals: expected.
467467
]
468468

469+
{ #category : #tests }
470+
DataSeriesTest >> testDetect [
471+
472+
| expected actual |
473+
474+
expected := 7.
475+
actual := series detect: [ :x | x > 5 ].
476+
self assert: actual equals: expected.
477+
]
478+
479+
{ #category : #tests }
480+
DataSeriesTest >> testDetectIfNone [
481+
482+
| expected actual |
483+
484+
expected := 'not found'.
485+
actual := series detect: [ :x | x > 100 ] ifNone: [ 'not found' ].
486+
self assert: actual equals: expected.
487+
]
488+
489+
{ #category : #tests }
490+
DataSeriesTest >> testDetectNotFound [
491+
492+
self
493+
should: [ series detect: [ :x | x > 100 ] ]
494+
raise: NotFound.
495+
]
496+
469497
{ #category : #tests }
470498
DataSeriesTest >> testDivideByScalar [
471499

@@ -687,6 +715,16 @@ DataSeriesTest >> testInequalityDifferentValues [
687715
self assert: (firstSeries ~= secondSeries).
688716
]
689717

718+
{ #category : #tests }
719+
DataSeriesTest >> testInjectInto [
720+
721+
| expected actual |
722+
723+
expected := 115.
724+
actual := series inject: 0 into: [ :sum :x | sum + x ].
725+
self assert: actual equals: expected.
726+
]
727+
690728
{ #category : #tests }
691729
DataSeriesTest >> testInterquartileRange [
692730

@@ -779,6 +817,20 @@ DataSeriesTest >> testRange [
779817
self assert: series range equals: 17.
780818
]
781819

820+
{ #category : #tests }
821+
DataSeriesTest >> testReject [
822+
823+
| expected actual |
824+
825+
expected := DataSeries
826+
withKeys: #(a b c e f g)
827+
values: #(3 7 6 8 9 8)
828+
name: 'ExampleSeries'.
829+
830+
actual := series reject: [ :x | x >= 10 ].
831+
self assert: actual equals: expected.
832+
]
833+
782834
{ #category : #tests }
783835
DataSeriesTest >> testRemoveAt [
784836

@@ -827,6 +879,20 @@ DataSeriesTest >> testSecondQuartileEqualsMedian [
827879
self assert: series secondQuartile equals: series median.
828880
]
829881

882+
{ #category : #tests }
883+
DataSeriesTest >> testSelect [
884+
885+
| expected actual |
886+
887+
expected := DataSeries
888+
withKeys: #(a b c e f g)
889+
values: #(3 7 6 8 9 8)
890+
name: 'ExampleSeries'.
891+
892+
actual := series select: [ :x | x < 10 ].
893+
self assert: actual equals: expected.
894+
]
895+
830896
{ #category : #tests }
831897
DataSeriesTest >> testSeventh [
832898

@@ -1084,6 +1150,34 @@ DataSeriesTest >> testWithIndexCollect [
10841150
self assert: actual equals: expected.
10851151
]
10861152

1153+
{ #category : #tests }
1154+
DataSeriesTest >> testWithIndexDetect [
1155+
1156+
| expected actual |
1157+
1158+
expected := 6.
1159+
actual := series withIndexDetect: [ :x :i | (x > 5) and: (i odd) ].
1160+
self assert: actual equals: expected.
1161+
]
1162+
1163+
{ #category : #tests }
1164+
DataSeriesTest >> testWithIndexDetectIfNone [
1165+
1166+
| expected actual |
1167+
1168+
expected := 'not found'.
1169+
actual := series withIndexDetect: [ :x :i | i > 100 ] ifNone: [ 'not found' ].
1170+
self assert: actual equals: expected.
1171+
]
1172+
1173+
{ #category : #tests }
1174+
DataSeriesTest >> testWithIndexDetectNotFound [
1175+
1176+
self
1177+
should: [ series withIndexDetect: [ :x :i | i > 100 ] ]
1178+
raise: NotFound.
1179+
]
1180+
10871181
{ #category : #tests }
10881182
DataSeriesTest >> testWithIndexDo [
10891183

@@ -1096,6 +1190,34 @@ DataSeriesTest >> testWithIndexDo [
10961190
self assert: sum equals: (108173/4620).
10971191
]
10981192

1193+
{ #category : #tests }
1194+
DataSeriesTest >> testWithIndexReject [
1195+
1196+
| expected actual |
1197+
1198+
expected := DataSeries
1199+
withKeys: #(a c e g)
1200+
values: #(3 6 8 8)
1201+
name: 'ExampleSeries'.
1202+
1203+
actual := series withIndexReject: [ :x :i | x >= 10 or: i even ].
1204+
self assert: actual equals: expected.
1205+
]
1206+
1207+
{ #category : #tests }
1208+
DataSeriesTest >> testWithIndexSelect [
1209+
1210+
| expected actual |
1211+
1212+
expected := DataSeries
1213+
withKeys: #(a c e g)
1214+
values: #(3 6 8 8)
1215+
name: 'ExampleSeries'.
1216+
1217+
actual := series withIndexSelect: [ :x :i | x < 10 and: i odd ].
1218+
self assert: actual equals: expected.
1219+
]
1220+
10991221
{ #category : #tests }
11001222
DataSeriesTest >> testWithKeyCollect [
11011223

@@ -1112,6 +1234,34 @@ DataSeriesTest >> testWithKeyCollect [
11121234
self assert: actual equals: expected.
11131235
]
11141236

1237+
{ #category : #tests }
1238+
DataSeriesTest >> testWithKeyDetect [
1239+
1240+
| expected actual |
1241+
1242+
expected := 6.
1243+
actual := series withKeyDetect: [ :x :k | x > 5 and: (k = #c) ].
1244+
self assert: actual equals: expected.
1245+
]
1246+
1247+
{ #category : #tests }
1248+
DataSeriesTest >> testWithKeyDetectIfNone [
1249+
1250+
| expected actual |
1251+
1252+
expected := 'not found'.
1253+
actual := series withKeyDetect: [ :x :k | x > 5 and: (k = #NoSuchKey) ] ifNone: [ 'not found' ].
1254+
self assert: actual equals: expected.
1255+
]
1256+
1257+
{ #category : #tests }
1258+
DataSeriesTest >> testWithKeyDetectNotFound [
1259+
1260+
self
1261+
should: [ series withKeyDetect: [ :x :k | x > 5 and: (k = #NoSuchKey) ] ]
1262+
raise: NotFound.
1263+
]
1264+
11151265
{ #category : #tests }
11161266
DataSeriesTest >> testWithKeyDo [
11171267

@@ -1124,6 +1274,34 @@ DataSeriesTest >> testWithKeyDo [
11241274
self assert: sum equals: (108173/4620).
11251275
]
11261276

1277+
{ #category : #tests }
1278+
DataSeriesTest >> testWithKeyReject [
1279+
1280+
| expected actual |
1281+
1282+
expected := DataSeries
1283+
withKeys: #(a c g)
1284+
values: #(3 6 8)
1285+
name: 'ExampleSeries'.
1286+
1287+
actual := series withKeyReject: [ :x :k | x >= 10 or: (#(a c g) includes: k) not ].
1288+
self assert: actual equals: expected.
1289+
]
1290+
1291+
{ #category : #tests }
1292+
DataSeriesTest >> testWithKeySelect [
1293+
1294+
| expected actual |
1295+
1296+
expected := DataSeries
1297+
withKeys: #(a c g)
1298+
values: #(3 6 8)
1299+
name: 'ExampleSeries'.
1300+
1301+
actual := series withKeySelect: [ :x :k | x < 10 and: (#(a c g) includes: k) ].
1302+
self assert: actual equals: expected.
1303+
]
1304+
11271305
{ #category : #tests }
11281306
DataSeriesTest >> testZerothQuartile [
11291307

src/DataFrame/DataSeries.class.st

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ DataSeries >> quartile: aNumber [
333333
^ self quantile: (25 * aNumber)
334334
]
335335

336+
{ #category : #enumerating }
337+
DataSeries >> reject: aBlock [
338+
| result |
339+
result := super reject: aBlock.
340+
result name: self name.
341+
^ result
342+
]
343+
336344
{ #category : #removing }
337345
DataSeries >> removeAt: aKey [
338346
^ self removeKey: aKey
@@ -355,6 +363,14 @@ DataSeries >> secondQuartile [
355363
^ self quartile: 2
356364
]
357365

366+
{ #category : #enumerating }
367+
DataSeries >> select: aBlock [
368+
| result |
369+
result := super select: aBlock.
370+
result name: self name.
371+
^ result
372+
]
373+
358374
{ #category : #accessing }
359375
DataSeries >> seventh [
360376
"Answer the seventh element of the receiver.
@@ -497,11 +513,47 @@ DataSeries >> withIndexCollect: aBlock [
497513
^ result
498514
]
499515

516+
{ #category : #enumerating }
517+
DataSeries >> withIndexDetect: aBlock [
518+
519+
^ self withIndexDetect: aBlock ifNone: [ NotFound signal ].
520+
]
521+
522+
{ #category : #enumerating }
523+
DataSeries >> withIndexDetect: aBlock ifNone: exceptionBlock [
524+
525+
| selectedIndex |
526+
527+
selectedIndex := (1 to: self size)
528+
detect: [ :i | aBlock value: (self atIndex: i) value: i ]
529+
ifNone: [ ^ exceptionBlock value ].
530+
531+
^ self atIndex: selectedIndex.
532+
]
533+
500534
{ #category : #enumerating }
501535
DataSeries >> withIndexDo: aBlock [
502536
self keys withIndexDo: [ :each :i | aBlock value: (self at: each) value: i ]
503537
]
504538

539+
{ #category : #enumerating }
540+
DataSeries >> withIndexReject: aBlock [
541+
^ self withIndexSelect: [ :each :i | (aBlock value: each value: i) not ]
542+
]
543+
544+
{ #category : #enumerating }
545+
DataSeries >> withIndexSelect: aBlock [
546+
| selectedIndices |
547+
548+
selectedIndices := (1 to: self size) select: [ :i |
549+
aBlock value: (self atIndex: i) value: i ].
550+
551+
^ DataSeries
552+
withKeys: (selectedIndices collect: [ :i | self keys at: i ])
553+
values: (selectedIndices collect: [ :i | self atIndex: i ])
554+
name: self name.
555+
]
556+
505557
{ #category : #enumerating }
506558
DataSeries >> withKeyCollect: aBlock [
507559
| result |
@@ -512,11 +564,47 @@ DataSeries >> withKeyCollect: aBlock [
512564
^ result
513565
]
514566

567+
{ #category : #enumerating }
568+
DataSeries >> withKeyDetect: aBlock [
569+
570+
^ self withKeyDetect: aBlock ifNone: [ NotFound signal ].
571+
]
572+
573+
{ #category : #enumerating }
574+
DataSeries >> withKeyDetect: aBlock ifNone: exceptionBlock [
575+
576+
| selectedKey |
577+
578+
selectedKey := self keys
579+
detect: [ :key | aBlock value: (self at: key) value: key ]
580+
ifNone: [ ^ exceptionBlock value ].
581+
582+
^ self at: selectedKey.
583+
]
584+
515585
{ #category : #enumerating }
516586
DataSeries >> withKeyDo: aBlock [
517587
self keysDo: [ :each | aBlock value: (self at: each) value: each ]
518588
]
519589

590+
{ #category : #enumerating }
591+
DataSeries >> withKeyReject: aBlock [
592+
^ self withKeySelect: [ :each :key | (aBlock value: each value: key) not ]
593+
]
594+
595+
{ #category : #enumerating }
596+
DataSeries >> withKeySelect: aBlock [
597+
| selectedKeys |
598+
599+
selectedKeys := self keys select: [ :key |
600+
aBlock value: (self at: key) value: key ].
601+
602+
^ DataSeries
603+
withKeys: selectedKeys
604+
values: (selectedKeys collect: [ :key | self at: key ])
605+
name: self name.
606+
]
607+
520608
{ #category : #enumerating }
521609
DataSeries >> withSeries: otherDataSeries collect: twoArgBlock [
522610
"Collect and return the result of evaluating twoArgBlock with corresponding elements from this series and otherDataSeries."

0 commit comments

Comments
 (0)