From b73a02f5161e70cdc766ea2029c18e1178f91cb1 Mon Sep 17 00:00:00 2001 From: Oleksandr Zaytsev Date: Thu, 14 Mar 2019 15:37:32 +0100 Subject: [PATCH] Wrote tests and implemented the modifiers. --- src/DataFrame-Tests/DataFrameTest.class.st | 213 +++++++++++++++++++++ src/DataFrame/DataFrame.class.st | 72 +++++++ 2 files changed, 285 insertions(+) diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index f79ae1a9..b2bf571d 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -537,6 +537,41 @@ DataFrameTest >> testColumnsAt [ self assert: actualDataFrame equals: expectedDataFrame. ] +{ #category : #tests } +DataFrameTest >> testColumnsAtPut [ + | rowNames columnNames dataFrame newColumns expected | + + rowNames := #('01:10' '01:30' '01:50' '02:10' '02:30') collect: #asTime. + columnNames := #(temperature precipitation type). + + dataFrame := DataFrame + withRows: #( + (2.4 true rain) + (0.5 true rain) + (-1.2 true snow) + (-2.3 false -) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + newColumns := #( + (1 1 1 1 1) + (2 2 2 2 2)). + + expected := DataFrame + withRows: #( + (2 true 1) + (2 true 1) + (2 true 1) + (2 false 1) + (2 true 1)) + rowNames: rowNames + columnNames: columnNames. + + dataFrame columnsAt: #(3 1) put: newColumns. + self assert: dataFrame equals: expected. +] + { #category : #tests } DataFrameTest >> testColumnsFromTo [ @@ -555,6 +590,76 @@ DataFrameTest >> testColumnsFromTo [ self assert: actualDataFrame equals: expectedDataFrame. ] +{ #category : #tests } +DataFrameTest >> testColumnsFromToPut [ + | rowNames columnNames dataFrame newColumns expected | + + rowNames := #('01:10' '01:30' '01:50' '02:10' '02:30') collect: #asTime. + columnNames := #(temperature precipitation type). + + dataFrame := DataFrame + withRows: #( + (2.4 true rain) + (0.5 true rain) + (-1.2 true snow) + (-2.3 false -) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + newColumns := #( + (1 1 1 1 1) + (2 2 2 2 2)). + + expected := DataFrame + withRows: #( + (2.4 2 1) + (0.5 2 1) + (-1.2 2 1) + (-2.3 2 1) + (3.2 2 1)) + rowNames: rowNames + columnNames: columnNames. + + dataFrame columnsFrom: 3 to: 2 put: newColumns. + self assert: dataFrame equals: expected. +] + +{ #category : #tests } +DataFrameTest >> testColumnsPut [ + | rowNames columnNames dataFrame newColumns expected | + + rowNames := #('01:10' '01:30' '01:50' '02:10' '02:30') collect: #asTime. + columnNames := #(temperature precipitation type). + + dataFrame := DataFrame + withRows: #( + (2.4 true rain) + (0.5 true rain) + (-1.2 true snow) + (-2.3 false -) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + newColumns := #( + (1 1 1 1 1) + (2 2 2 2 2)). + + expected := DataFrame + withRows: #( + (2 true 1) + (2 true 1) + (2 true 1) + (2 false 1) + (2 true 1)) + rowNames: rowNames + columnNames: columnNames. + + dataFrame columns: #(type temperature) put: newColumns. + self assert: dataFrame equals: expected. +] + { #category : #tests } DataFrameTest >> testCreateDataFrameWith3ColumnsAndNoRows [ | dataFrame | @@ -1189,6 +1294,42 @@ DataFrameTest >> testRowsAt [ self assert: actualDataFrame equals: expectedDataFrame. ] +{ #category : #tests } +DataFrameTest >> testRowsAtPut [ + | rowNames columnNames dataFrame newRows expected | + + rowNames := #('01:10' '01:30' '01:50' '02:10' '02:30') collect: #asTime. + columnNames := #(temperature precipitation type). + + dataFrame := DataFrame + withRows: #( + (2.4 true rain) + (0.5 true rain) + (-1.2 true snow) + (-2.3 false -) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + newRows := #( + (1 1 1) + (2 2 2) + (3 3 3)). + + expected := DataFrame + withRows: #( + (2.4 true rain) + (3 3 3) + (1 1 1) + (-2.3 false -) + (2 2 2)) + rowNames: rowNames + columnNames: columnNames. + + dataFrame rowsAt: #(3 5 2) put: newRows. + self assert: dataFrame equals: expected. +] + { #category : #tests } DataFrameTest >> testRowsFromTo [ @@ -1206,6 +1347,78 @@ DataFrameTest >> testRowsFromTo [ self assert: actualDataFrame equals: expectedDataFrame. ] +{ #category : #tests } +DataFrameTest >> testRowsFromToPut [ + | rowNames columnNames dataFrame newRows expected | + + rowNames := #('01:10' '01:30' '01:50' '02:10' '02:30') collect: #asTime. + columnNames := #(temperature precipitation type). + + dataFrame := DataFrame + withRows: #( + (2.4 true rain) + (0.5 true rain) + (-1.2 true snow) + (-2.3 false -) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + newRows := #( + (1 1 1) + (2 2 2) + (3 3 3)). + + expected := DataFrame + withRows: #( + (2.4 true rain) + (3 3 3) + (2 2 2) + (1 1 1) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + dataFrame rowsFrom: 4 to: 2 put: newRows. + self assert: dataFrame equals: expected. +] + +{ #category : #tests } +DataFrameTest >> testRowsPut [ + | rowNames columnNames dataFrame newRows expected | + + rowNames := #('01:10' '01:30' '01:50' '02:10' '02:30') collect: #asTime. + columnNames := #(temperature precipitation type). + + dataFrame := DataFrame + withRows: #( + (2.4 true rain) + (0.5 true rain) + (-1.2 true snow) + (-2.3 false -) + (3.2 true rain)) + rowNames: rowNames + columnNames: columnNames. + + newRows := #( + (1 1 1) + (2 2 2) + (3 3 3)). + + expected := DataFrame + withRows: #( + (2.4 true rain) + (3 3 3) + (1 1 1) + (-2.3 false -) + (2 2 2)) + rowNames: rowNames + columnNames: columnNames. + + dataFrame rows: { '01:50' asTime . '02:30' asTime . '01:30' asTime } put: newRows. + self assert: dataFrame equals: expected. +] + { #category : #tests } DataFrameTest >> testSortBy [ diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 9024ae27..b2b0667c 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -553,6 +553,16 @@ DataFrame >> columns: anArrayOfNames [ ^ self columnsAt: anArrayOfNumbers. ] +{ #category : #accessing } +DataFrame >> columns: anArrayOfColumnNames put: anArrayOfArrays [ + + anArrayOfArrays size = anArrayOfColumnNames size + ifFalse: [ SizeMismatch signal ]. + + anArrayOfColumnNames with: anArrayOfArrays do: [ :name :array | + self column: name put: array ]. +] + { #category : #accessing } DataFrame >> columnsAt: anArrayOfNumbers [ @@ -567,6 +577,16 @@ DataFrame >> columnsAt: anArrayOfNumbers [ columnNames: newColumnNames. ] +{ #category : #accessing } +DataFrame >> columnsAt: anArrayOfNumbers put: anArrayOfArrays [ + + anArrayOfArrays size = anArrayOfNumbers size + ifFalse: [ SizeMismatch signal ]. + + anArrayOfNumbers with: anArrayOfArrays do: [ :index :array | + self columnAt: index put: array ]. +] + { #category : #accessing } DataFrame >> columnsFrom: begin to: end [ @@ -579,6 +599,22 @@ DataFrame >> columnsFrom: begin to: end [ ^ self columnsAt: array. ] +{ #category : #accessing } +DataFrame >> columnsFrom: firstNumber to: secondNumber put: anArrayOfArrays [ + + | interval | + + anArrayOfArrays size = ((firstNumber - secondNumber) abs + 1) + ifFalse: [ SizeMismatch signal ]. + + interval := secondNumber >= firstNumber + ifTrue: [ (firstNumber to: secondNumber) ] + ifFalse: [ (secondNumber to: firstNumber) reversed ]. + + interval withIndexDo: [ :columnIndex :i | + self columnAt: columnIndex put: (anArrayOfArrays at: i) ]. +] + { #category : #accessing } DataFrame >> contents [ @@ -931,6 +967,16 @@ DataFrame >> rows: anArrayOfNames [ ^ self rowsAt: anArrayOfNumbers. ] +{ #category : #accessing } +DataFrame >> rows: anArrayOfRowNames put: anArrayOfArrays [ + + anArrayOfArrays size = anArrayOfRowNames size + ifFalse: [ SizeMismatch signal ]. + + anArrayOfRowNames with: anArrayOfArrays do: [ :name :array | + self row: name put: array ]. +] + { #category : #accessing } DataFrame >> rowsAt: anArrayOfNumbers [ @@ -945,12 +991,38 @@ DataFrame >> rowsAt: anArrayOfNumbers [ columnNames: self columnNames. ] +{ #category : #accessing } +DataFrame >> rowsAt: anArrayOfNumbers put: anArrayOfArrays [ + + anArrayOfArrays size = anArrayOfNumbers size + ifFalse: [ SizeMismatch signal ]. + + anArrayOfNumbers with: anArrayOfArrays do: [ :index :array | + self rowAt: index put: array ]. +] + { #category : #accessing } DataFrame >> rowsFrom: begin to: end [ ^ self rowsAt: (begin to: end). ] +{ #category : #accessing } +DataFrame >> rowsFrom: firstNumber to: secondNumber put: anArrayOfArrays [ + + | interval | + + anArrayOfArrays size = ((firstNumber - secondNumber) abs + 1) + ifFalse: [ SizeMismatch signal ]. + + interval := secondNumber >= firstNumber + ifTrue: [ (firstNumber to: secondNumber) ] + ifFalse: [ (secondNumber to: firstNumber) reversed ]. + + interval withIndexDo: [ :rowIndex :i | + self rowAt: rowIndex put: (anArrayOfArrays at: i) ]. +] + { #category : #enumerating } DataFrame >> select: aBlock [ "Evaluate aBlock with each of the receiver's elements as the argument.