Skip to content

Commit f6723a9

Browse files
authored
Merge pull request #192 from jecisc/speed-up-creation-of-DataSeries
Speed up creation of data series
2 parents cb5244e + 5cbef10 commit f6723a9

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,25 +3725,18 @@ DataFrameTest >> testReplaceNilsWithMedian [
37253725
{ #category : #replacing }
37263726
DataFrameTest >> testReplaceNilsWithMode [
37273727

3728-
|expected|
3729-
3730-
df := DataFrame withRows: #(
3731-
(7 1 nil)
3732-
(8 nil nil)
3733-
(nil 1 43)).
3728+
| expected |
3729+
df := DataFrame withRows: #( #( 7 1 nil ) #( 8 nil nil ) #( nil 1 43 ) ).
37343730

3735-
df rowNames: #(A B C).
3736-
df columnNames: #(Column1 Column2 Column3).
3731+
df rowNames: #( A B C ).
3732+
df columnNames: #( Column1 Column2 Column3 ).
37373733

3738-
expected := DataFrame withRows: #(
3739-
(7 1 43)
3740-
(8 1 43)
3741-
(7 1 43)).
3734+
expected := DataFrame withRows: #( #( 7 1 43 ) #( 8 1 43 ) #( 8 1 43 ) ).
37423735

3743-
expected rowNames: #(A B C).
3744-
expected columnNames: #(Column1 Column2 Column3).
3736+
expected rowNames: #( A B C ).
3737+
expected columnNames: #( Column1 Column2 Column3 ).
37453738

3746-
self assert: (df replaceNilsWithMode) equals: expected
3739+
self assert: df replaceNilsWithMode equals: expected
37473740
]
37483741

37493742
{ #category : #replacing }

src/DataFrame/DataFrame.class.st

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,13 +1645,11 @@ DataFrame >> replaceNilsWithMedian [
16451645
{ #category : #replacing }
16461646
DataFrame >> replaceNilsWithMode [
16471647

1648-
| modeOfColumn |
16491648
1 to: self numberOfColumns do: [ :i |
1650-
modeOfColumn := ((self columnAt: i) select: [ :ele | ele isNotNil ]) mode.
1649+
| modeOfColumn |
16511650
1 to: self numberOfRows do: [ :j |
1652-
(self at: j at: i) ifNil: [
1653-
self at: j at: i put: modeOfColumn ] ].
1654-
]
1651+
(self at: j at: i) ifNil: [ self at: j at: i put: (modeOfColumn ifNil: [ modeOfColumn := ((self columnAt: i) select: [ :ele | ele isNotNil ]) mode ]) ] ].
1652+
modeOfColumn := nil ]
16551653
]
16561654

16571655
{ #category : #replacing }

src/DataFrame/DataSeries.class.st

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ DataSeries class >> newFrom: aCollection [
2121
ifFalse: [ aCollection withIndexCollect: [ :each :i | i -> each ] ])
2222
]
2323

24+
{ #category : #'instance creation' }
25+
DataSeries class >> newFromKeys: keys andValues: values [
26+
27+
| dict |
28+
self flag: #pharo12. "This is a copy of the superclass with a speed up. I'll propose this speedup in Pharo 12 so when Pharo 12 will be the minimal suuported version then we can drop this method."
29+
dict := self new: keys size.
30+
keys with: values do: [ :k :v | dict at: k put: v ].
31+
^ dict
32+
]
33+
2434
{ #category : #'instance creation' }
2535
DataSeries class >> withKeys: keys values: values [
2636
^ self newFromKeys: keys andValues: values
@@ -450,6 +460,7 @@ DataSeries >> makeNumerical [
450460

451461
{ #category : #accessing }
452462
DataSeries >> mode [
463+
453464
| valueCounts maxCount |
454465
valueCounts := self valueCounts.
455466
maxCount := valueCounts max.

0 commit comments

Comments
 (0)