From baf02c78ef04984ae006245c2b6236babce9c64c Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Thu, 29 Jun 2023 23:27:19 +0530 Subject: [PATCH 1/4] Added #sortByRowNamesUsing: with tests. --- src/DataFrame-Tests/DataFrameTest.class.st | 25 ++++++++++++++++++++++ src/DataFrame/DataFrame.class.st | 16 ++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index f45dc9fb..03be11d5 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -5157,6 +5157,31 @@ DataFrameTest >> testSortByAll [ self assert: actual 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" diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 0ee13b85..80f9149e 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2469,6 +2469,22 @@ DataFrame >> sortByAll: arrayOfColumnNames [ ^ self ] +{ #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" From b9e0259f003da25b2713017c7b3bf27592267902 Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Thu, 29 Jun 2023 23:27:42 +0530 Subject: [PATCH 2/4] Added #sortByRowNames with tests. --- src/DataFrame-Tests/DataFrameTest.class.st | 23 ++++++++++++++++++++++ src/DataFrame/DataFrame.class.st | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index 03be11d5..feb5720d 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -5157,6 +5157,29 @@ 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" diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 80f9149e..43e12765 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2469,6 +2469,13 @@ 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" From 605d20eca7f03c8d9e6ef95e5100ad551470e9f2 Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Thu, 29 Jun 2023 23:28:12 +0530 Subject: [PATCH 3/4] Added #sortDescendingByRowNames with tests. --- src/DataFrame-Tests/DataFrameTest.class.st | 23 ++++++++++++++++++++++ src/DataFrame/DataFrame.class.st | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index feb5720d..263d22b4 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -5272,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 [ diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 43e12765..f1cd2ff5 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2516,6 +2516,13 @@ DataFrame >> sortDescendingByAll: arrayOfColumnNames [ ^ self ] +{ #category : #sorting } +DataFrame >> sortDescendingByRowNames [ + "Sorts the rows of the data frame based on the row names in ascending 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" From f75db9c43233f88efbe9b388f81d60f1da479f11 Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Thu, 29 Jun 2023 23:36:18 +0530 Subject: [PATCH 4/4] Corrected comment 'ascending' -> 'descending'. --- src/DataFrame/DataFrame.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index f1cd2ff5..0b09d60d 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2518,7 +2518,7 @@ DataFrame >> sortDescendingByAll: arrayOfColumnNames [ { #category : #sorting } DataFrame >> sortDescendingByRowNames [ - "Sorts the rows of the data frame based on the row names in ascending order" + "Sorts the rows of the data frame based on the row names in descending order" self sortByRowNamesUsing: [ :a :b | a >= b ] ]