Skip to content

Commit a93858b

Browse files
authored
Merge pull request #273 from Joshua-Dias-Barreto/chainedSorting
Added sorting methods for data frames based on row names.
2 parents 3cdd6b5 + f75db9c commit a93858b

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5157,6 +5157,54 @@ DataFrameTest >> testSortByAll [
51575157
self assert: actual equals: expected
51585158
]
51595159

5160+
{ #category : #tests }
5161+
DataFrameTest >> testSortByRowNames [
5162+
5163+
| dataFrame expected sortedDF |
5164+
dataFrame := DataFrame withRows:
5165+
#( #( 'CD' 3 2 )
5166+
#( 'AC' 1 4 )
5167+
#( 'DB' 2 4 )
5168+
#( 'BA' 5 1 ) ).
5169+
dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ).
5170+
5171+
expected := DataFrame withRows:
5172+
#( #( 'AC' 1 4 )
5173+
#( 'BA' 5 1 )
5174+
#( 'CD' 3 2 )
5175+
#( 'DB' 2 4 ) ).
5176+
expected rowNames: #( 'AC' 'BA' 'CD' 'DB' ).
5177+
5178+
sortedDF := dataFrame sortByRowNames.
5179+
5180+
self assert: sortedDF equals: expected
5181+
]
5182+
5183+
{ #category : #tests }
5184+
DataFrameTest >> testSortByRowNamesUsing [
5185+
"sorts by second letter of row name"
5186+
5187+
| dataFrame expected sortedDF |
5188+
dataFrame := DataFrame withRows:
5189+
#( #( 'CD' 3 2 )
5190+
#( 'AC' 1 4 )
5191+
#( 'DB' 2 4 )
5192+
#( 'BA' 5 1 ) ).
5193+
dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ).
5194+
5195+
expected := DataFrame withRows:
5196+
#( #( 'BA' 5 1 )
5197+
#( 'DB' 2 4 )
5198+
#( 'AC' 1 4 )
5199+
#( 'CD' 3 2 ) ).
5200+
expected rowNames: #( 'BA' 'DB' 'AC' 'CD' ).
5201+
5202+
sortedDF := dataFrame sortByRowNamesUsing: [ :name1 :name2 |
5203+
name1 second <= name2 second ].
5204+
5205+
self assert: sortedDF equals: expected
5206+
]
5207+
51605208
{ #category : #tests }
51615209
DataFrameTest >> testSortByUsing [
51625210
"Sort by second letter of city name"
@@ -5224,6 +5272,29 @@ DataFrameTest >> testSortDescendingByAll [
52245272
self assert: actual equals: expected
52255273
]
52265274

5275+
{ #category : #tests }
5276+
DataFrameTest >> testSortDescendingByRowNames [
5277+
5278+
| dataFrame expected sortedDF |
5279+
dataFrame := DataFrame withRows:
5280+
#( #( 'CD' 3 2 )
5281+
#( 'AC' 1 4 )
5282+
#( 'DB' 2 4 )
5283+
#( 'BA' 5 1 ) ).
5284+
dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ).
5285+
5286+
expected := DataFrame withRows:
5287+
#( #( 'DB' 2 4 )
5288+
#( 'CD' 3 2 )
5289+
#( 'BA' 5 1 )
5290+
#( 'AC' 1 4 ) ).
5291+
expected rowNames: #( 'DB' 'CD' 'BA' 'AC' ).
5292+
5293+
sortedDF := dataFrame sortDescendingByRowNames.
5294+
5295+
self assert: sortedDF equals: expected
5296+
]
5297+
52275298
{ #category : #tests }
52285299
DataFrameTest >> testToColumnApplyElementwise [
52295300

src/DataFrame/DataFrame.class.st

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,29 @@ DataFrame >> sortByAll: arrayOfColumnNames [
24692469
^ self
24702470
]
24712471

2472+
{ #category : #sorting }
2473+
DataFrame >> sortByRowNames [
2474+
"Sorts the rows of the data frame based on the row names in ascending order"
2475+
2476+
self sortByRowNamesUsing: [ :a :b | a <= b ]
2477+
]
2478+
2479+
{ #category : #sorting }
2480+
DataFrame >> sortByRowNamesUsing: aBlock [
2481+
"Sorts the rows of the data frame based on the row names using the given comparison block"
2482+
2483+
| sortedKeys newContents |
2484+
sortedKeys := self rowNames sorted: aBlock.
2485+
2486+
newContents := DataFrameInternal new: self dimensions.
2487+
2488+
sortedKeys withIndexDo: [ :key :i |
2489+
newContents rowAt: i put: (self row: key) asArray ].
2490+
2491+
contents := newContents.
2492+
self rowNames: sortedKeys
2493+
]
2494+
24722495
{ #category : #sorting }
24732496
DataFrame >> sortDescendingBy: columnName [
24742497
"Rearranges the rows of the data frame in descending order of the values in the column named columnName"
@@ -2493,6 +2516,13 @@ DataFrame >> sortDescendingByAll: arrayOfColumnNames [
24932516
^ self
24942517
]
24952518

2519+
{ #category : #sorting }
2520+
DataFrame >> sortDescendingByRowNames [
2521+
"Sorts the rows of the data frame based on the row names in descending order"
2522+
2523+
self sortByRowNamesUsing: [ :a :b | a >= b ]
2524+
]
2525+
24962526
{ #category : #statistics }
24972527
DataFrame >> stdev [
24982528
"Standard deviation is a measure of how dispersed the data is in relation to the average"

0 commit comments

Comments
 (0)