From 4446637d44264f6eb283fbe05517f908c4c067ee Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Fri, 7 Jul 2023 18:34:22 +0530 Subject: [PATCH 1/2] Added methods to convert collections to dataframes with specific row and / or column names. --- src/DataFrame-Tests/DataFrameTest.class.st | 56 ++++++++++++++++++++++ src/DataFrame/Collection.extension.st | 26 ++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index f45dc9fb..c7fc717e 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -470,6 +470,62 @@ DataFrameTest >> testAsDataFrameEmptyColumns [ self assert: actual equals: expected ] +{ #category : #tests } +DataFrameTest >> testAsDataFrameWithColumnNames [ + + | actual expected | + actual := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + asDataFrameWithColumnNames: #( a b c ). + expected := DataFrame + withRows: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + columnNames: #( a b c ). + + self assert: actual equals: expected +] + +{ #category : #tests } +DataFrameTest >> testAsDataFrameWithColumnNamesWithRowNames [ + + | actual expected | + actual := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + asDataFrameWithColumnNames: #( a b c ) + withRowNames: #( d e f ). + expected := DataFrame + withRows: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + rowNames: #( d e f ) + columnNames: #( a b c ). + + self assert: actual equals: expected +] + +{ #category : #tests } +DataFrameTest >> testAsDataFrameWithRowNames [ + + | actual expected | + actual := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + asDataFrameWithRowNames: #( a b c ). + expected := DataFrame + withRows: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + rowNames: #( a b c ). + + self assert: actual equals: expected +] + +{ #category : #tests } +DataFrameTest >> testAsDataFrameWithRowNamesWithColumnNames [ + + | actual expected | + actual := #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + asDataFrameWithRowNames: #( a b c ) + withColumnNames: #( d e f ). + expected := DataFrame + withRows: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ) + rowNames: #( a b c ) + columnNames: #( d e f ). + + self assert: actual equals: expected +] + { #category : #tests } DataFrameTest >> testAsDataFrameWithUnevenRowElements [ diff --git a/src/DataFrame/Collection.extension.st b/src/DataFrame/Collection.extension.st index 30df4bf0..6f283879 100644 --- a/src/DataFrame/Collection.extension.st +++ b/src/DataFrame/Collection.extension.st @@ -31,6 +31,32 @@ Collection >> asDataFrame [ ^ dataFrame ] +{ #category : #'*DataFrame' } +Collection >> asDataFrameWithColumnNames: arrayOfColumnNames [ + + ^ self asDataFrame columnNames: arrayOfColumnNames +] + +{ #category : #'*DataFrame' } +Collection >> asDataFrameWithColumnNames: arrayOfColumnNames withRowNames: arrayOfRowNames [ + + ^ (self asDataFrame columnNames: arrayOfColumnNames) rowNames: + arrayOfRowNames +] + +{ #category : #'*DataFrame' } +Collection >> asDataFrameWithRowNames: arrayOfRowNames [ + + ^ self asDataFrame rowNames: arrayOfRowNames +] + +{ #category : #'*DataFrame' } +Collection >> asDataFrameWithRowNames: arrayOfRowNames withColumnNames: arrayOfColumnNames [ + + ^ (self asDataFrame rowNames: arrayOfRowNames) columnNames: + arrayOfColumnNames +] + { #category : #'*DataFrame-Core-Base' } Collection >> asDataSeries [ From 547ea70e4795310d9e92443cc820533ca5981424 Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Sat, 8 Jul 2023 21:03:31 +0530 Subject: [PATCH 2/2] Added comments for converting collections to data frames. --- src/DataFrame/Collection.extension.st | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/DataFrame/Collection.extension.st b/src/DataFrame/Collection.extension.st index 6f283879..45ac22fe 100644 --- a/src/DataFrame/Collection.extension.st +++ b/src/DataFrame/Collection.extension.st @@ -8,6 +8,7 @@ Collection >> ** arg [ { #category : #'*DataFrame' } Collection >> asDataFrame [ + "Answer a DataFrame whose elements are the elements of the receiver." | numberOfRows numberOfColumns dataFrame | numberOfRows := self size. @@ -33,12 +34,14 @@ Collection >> asDataFrame [ { #category : #'*DataFrame' } Collection >> asDataFrameWithColumnNames: arrayOfColumnNames [ + "Answer a DataFrame whose elements are the elements of the receiver and column names are the contents of the collection arrayOfColumnNames." ^ self asDataFrame columnNames: arrayOfColumnNames ] { #category : #'*DataFrame' } Collection >> asDataFrameWithColumnNames: arrayOfColumnNames withRowNames: arrayOfRowNames [ + "Answer a DataFrame whose elements are the elements of the receiver, column names are the contents of the collection arrayOfColumnNames and row names are the contents of the collection arrayOfRowNames." ^ (self asDataFrame columnNames: arrayOfColumnNames) rowNames: arrayOfRowNames @@ -46,12 +49,14 @@ Collection >> asDataFrameWithColumnNames: arrayOfColumnNames withRowNames: array { #category : #'*DataFrame' } Collection >> asDataFrameWithRowNames: arrayOfRowNames [ + "Answer a DataFrame whose elements are the elements of the receiver and row names are the contents of the collection arrayOfRowNames." ^ self asDataFrame rowNames: arrayOfRowNames ] { #category : #'*DataFrame' } Collection >> asDataFrameWithRowNames: arrayOfRowNames withColumnNames: arrayOfColumnNames [ + "Answer a DataFrame whose elements are the elements of the receiver, row names are the contents of the collection arrayOfRowNames and column names are the contents of the collection arrayOfColumnNames." ^ (self asDataFrame rowNames: arrayOfRowNames) columnNames: arrayOfColumnNames