Skip to content

Commit

Permalink
Merge pull request #273 from Joshua-Dias-Barreto/chainedSorting
Browse files Browse the repository at this point in the history
Added sorting methods for data frames based on row names.
  • Loading branch information
jecisc authored Jul 8, 2023
2 parents 3cdd6b5 + f75db9c commit a93858b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/DataFrame-Tests/DataFrameTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5157,6 +5157,54 @@ DataFrameTest >> testSortByAll [
self assert: actual equals: expected
]

{ #category : #tests }
DataFrameTest >> testSortByRowNames [

| dataFrame expected sortedDF |
dataFrame := DataFrame withRows:
#( #( 'CD' 3 2 )
#( 'AC' 1 4 )
#( 'DB' 2 4 )
#( 'BA' 5 1 ) ).
dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ).

expected := DataFrame withRows:
#( #( 'AC' 1 4 )
#( 'BA' 5 1 )
#( 'CD' 3 2 )
#( 'DB' 2 4 ) ).
expected rowNames: #( 'AC' 'BA' 'CD' 'DB' ).

sortedDF := dataFrame sortByRowNames.

self assert: sortedDF equals: expected
]

{ #category : #tests }
DataFrameTest >> testSortByRowNamesUsing [
"sorts by second letter of row name"

| dataFrame expected sortedDF |
dataFrame := DataFrame withRows:
#( #( 'CD' 3 2 )
#( 'AC' 1 4 )
#( 'DB' 2 4 )
#( 'BA' 5 1 ) ).
dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ).

expected := DataFrame withRows:
#( #( 'BA' 5 1 )
#( 'DB' 2 4 )
#( 'AC' 1 4 )
#( 'CD' 3 2 ) ).
expected rowNames: #( 'BA' 'DB' 'AC' 'CD' ).

sortedDF := dataFrame sortByRowNamesUsing: [ :name1 :name2 |
name1 second <= name2 second ].

self assert: sortedDF equals: expected
]

{ #category : #tests }
DataFrameTest >> testSortByUsing [
"Sort by second letter of city name"
Expand Down Expand Up @@ -5224,6 +5272,29 @@ DataFrameTest >> testSortDescendingByAll [
self assert: actual equals: expected
]

{ #category : #tests }
DataFrameTest >> testSortDescendingByRowNames [

| dataFrame expected sortedDF |
dataFrame := DataFrame withRows:
#( #( 'CD' 3 2 )
#( 'AC' 1 4 )
#( 'DB' 2 4 )
#( 'BA' 5 1 ) ).
dataFrame rowNames: #( 'CD' 'AC' 'DB' 'BA' ).

expected := DataFrame withRows:
#( #( 'DB' 2 4 )
#( 'CD' 3 2 )
#( 'BA' 5 1 )
#( 'AC' 1 4 ) ).
expected rowNames: #( 'DB' 'CD' 'BA' 'AC' ).

sortedDF := dataFrame sortDescendingByRowNames.

self assert: sortedDF equals: expected
]

{ #category : #tests }
DataFrameTest >> testToColumnApplyElementwise [

Expand Down
30 changes: 30 additions & 0 deletions src/DataFrame/DataFrame.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,29 @@ DataFrame >> sortByAll: arrayOfColumnNames [
^ self
]

{ #category : #sorting }
DataFrame >> sortByRowNames [
"Sorts the rows of the data frame based on the row names in ascending order"

self sortByRowNamesUsing: [ :a :b | a <= b ]
]

{ #category : #sorting }
DataFrame >> sortByRowNamesUsing: aBlock [
"Sorts the rows of the data frame based on the row names using the given comparison block"

| sortedKeys newContents |
sortedKeys := self rowNames sorted: aBlock.

newContents := DataFrameInternal new: self dimensions.

sortedKeys withIndexDo: [ :key :i |
newContents rowAt: i put: (self row: key) asArray ].

contents := newContents.
self rowNames: sortedKeys
]

{ #category : #sorting }
DataFrame >> sortDescendingBy: columnName [
"Rearranges the rows of the data frame in descending order of the values in the column named columnName"
Expand All @@ -2493,6 +2516,13 @@ DataFrame >> sortDescendingByAll: arrayOfColumnNames [
^ self
]

{ #category : #sorting }
DataFrame >> sortDescendingByRowNames [
"Sorts the rows of the data frame based on the row names in descending order"

self sortByRowNamesUsing: [ :a :b | a >= b ]
]

{ #category : #statistics }
DataFrame >> stdev [
"Standard deviation is a measure of how dispersed the data is in relation to the average"
Expand Down

0 comments on commit a93858b

Please sign in to comment.