Skip to content

Commit dca6a2a

Browse files
authored
Merge pull request #233 from Joshua-Dias-Barreto/handlingNils
Statistical methods such as stdev, max and min can now handle nils.
2 parents d8066a0 + 2393dd6 commit dca6a2a

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

src/DataFrame-Tests/DataSeriesTest.class.st

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,42 @@ DataSeriesTest >> testMathTan [
15881588
self assert: a tan closeTo: b
15891589
]
15901590

1591+
{ #category : #'tests - statistics' }
1592+
DataSeriesTest >> testMax [
1593+
1594+
self assert: #( 1 2 3 4 ) asDataSeries max equals: 4
1595+
]
1596+
1597+
{ #category : #'tests - statistics' }
1598+
DataSeriesTest >> testMaxWithNils [
1599+
1600+
self assert: #( 1 nil 3 nil ) asDataSeries max equals: 3
1601+
]
1602+
1603+
{ #category : #tests }
1604+
DataSeriesTest >> testMedian [
1605+
1606+
self assert: #( 1 2 3 4 5 ) asDataSeries median equals: 3
1607+
]
1608+
1609+
{ #category : #tests }
1610+
DataSeriesTest >> testMedianWithNils [
1611+
1612+
self assert: #( 1 2 nil 3 4 nil 5 ) asDataSeries median equals: 3
1613+
]
1614+
1615+
{ #category : #'tests - statistics' }
1616+
DataSeriesTest >> testMin [
1617+
1618+
self assert: #( 1 2 3 4 ) asDataSeries min equals: 1
1619+
]
1620+
1621+
{ #category : #'tests - statistics' }
1622+
DataSeriesTest >> testMinWithNils [
1623+
1624+
self assert: #( nil 2 nil 4 ) asDataSeries min equals: 2
1625+
]
1626+
15911627
{ #category : #'tests - creation' }
15921628
DataSeriesTest >> testNewFrom [
15931629

@@ -2176,6 +2212,18 @@ DataSeriesTest >> testStatsZerothQuartileEqualsMin [
21762212
self assert: series zerothQuartile equals: series min
21772213
]
21782214

2215+
{ #category : #'tests - statistics' }
2216+
DataSeriesTest >> testStdev [
2217+
2218+
self assert: #( 1 2 3 ) asDataSeries stdev equals: 1
2219+
]
2220+
2221+
{ #category : #tests }
2222+
DataSeriesTest >> testStdevWithNils [
2223+
2224+
self assert: #( 1 nil 2 nil 3 ) asDataSeries stdev equals: 1
2225+
]
2226+
21792227
{ #category : #'tests - arithmetic' }
21802228
DataSeriesTest >> testSum [
21812229

src/DataFrame/DataSeries.class.st

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ DataSeries >> atIndex: aNumber transform: aBlock [
167167
self at: key transform: aBlock
168168
]
169169

170-
{ #category : #information }
170+
{ #category : #statistics }
171171
DataSeries >> average [
172-
"We do not count the nils"
172+
"Returns the average without including nils"
173173

174-
^ (self values reject: #isNil) average
174+
^ self removeNils values average
175175
]
176176

177177
{ #category : #'data-types' }
@@ -520,6 +520,27 @@ DataSeries >> makeNumerical [
520520
forcedIsNumerical := true
521521
]
522522

523+
{ #category : #statistics }
524+
DataSeries >> max [
525+
"Returns the maximum value of the dataseries without including nils"
526+
527+
^ self removeNils values max
528+
]
529+
530+
{ #category : #statistics }
531+
DataSeries >> median [
532+
"Returns the median without including nils"
533+
534+
^ self removeNils values median
535+
]
536+
537+
{ #category : #statistics }
538+
DataSeries >> min [
539+
"Returns the minimum value of the dataseries without including nils"
540+
541+
^ self removeNils values min
542+
]
543+
523544
{ #category : #accessing }
524545
DataSeries >> mode [
525546
"The mode of a set of values is the value that appears most often. "
@@ -757,6 +778,13 @@ DataSeries >> sortedDescending [
757778
^ self sorted: [ :a :b | a > b ]
758779
]
759780

781+
{ #category : #statistics }
782+
DataSeries >> stdev [
783+
"Returns the standard deviation of the dataseries without including nils"
784+
785+
^ self removeNils values stdev
786+
]
787+
760788
{ #category : #transformation }
761789
DataSeries >> sum [
762790
"Return the sum of the values over the requested axis. Nil values are excluded."

0 commit comments

Comments
 (0)