Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/DataFrame-Tests/DataFrameInternalTest.class.st
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Class {
Class {
#name : #DataFrameInternalTest,
#superclass : #TestCase,
#instVars : [
Expand Down Expand Up @@ -370,6 +370,19 @@ DataFrameInternalTest >> testVarSizeInstanceCreation [
self assert: dfActual numberOfColumns equals: 8.
]

{ #category : #tests }
DataFrameInternalTest >> testWithColumns [

| dfi validOutput |
dfi := DataFrameInternal withColumns: #((1 2) (1 2 3 4 5) ('a' 3.14 'bcd')).
validOutput := Array2D
rows: 5
columns: 3
contents: #(1 1 'a' 2 2 3.14 nil 3 'bcd' nil 4 nil nil 5 nil).

self assert: dfi asArray2D equals: validOutput.
]

{ #category : #tests }
DataFrameInternalTest >> testWithIndicesCollect [

Expand Down Expand Up @@ -399,3 +412,16 @@ DataFrameInternalTest >> testWithIndicesDo [
dfInternal withIndicesDo: [ :each :i :j |
self assert: each equals: ((i - 1) * 2 + j) ].
]

{ #category : #tests }
DataFrameInternalTest >> testWithRows [

| dfi validOutput |
dfi := DataFrameInternal withRows: #((1 2) (1 2 3 4 5) ('a' 3.14 'bcd')).
validOutput := Array2D
rows: 3
columns: 5
contents: #(1 2 nil nil nil 1 2 3 4 5 'a' 3.14 'bcd' nil nil).

self assert: dfi asArray2D equals: validOutput.
]
97 changes: 97 additions & 0 deletions src/DataFrame-Tests/DataFrameTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,42 @@ DataFrameTest >> testCreateEmptyDataFrameWithColumnNames [
self assert: dataFrame columnNames equals: columnNames.
]

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

| dataFrame |
dataFrame := DataFrame withColumns: #().

self assert: dataFrame class equals: DataFrame.
]

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

| dataFrame |
dataFrame := DataFrame withColumns: #() columnNames: #().

self assert: dataFrame class equals: DataFrame.
]

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

| dataFrame |
dataFrame := DataFrame withColumns: #() rowNames: #().

self assert: dataFrame class equals: DataFrame.
]

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

| dataFrame |
dataFrame := DataFrame withColumns: #() rowNames: #() columnNames: #().

self assert: dataFrame class equals: DataFrame.
]

{ #category : #tests }
DataFrameTest >> testCreateEmptyDataFrameWithRowNames [
| rowNames dataFrame |
Expand Down Expand Up @@ -1111,6 +1147,42 @@ DataFrameTest >> testCreateEmptyDataFrameWithRowNamesColumnNames [
self assert: dataFrame columnNames equals: columnNames.
]

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

| dataFrame |
dataFrame := DataFrame withRows: #().

self assert: dataFrame class equals: DataFrame.
]

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

| dataFrame |
dataFrame := DataFrame withRows: #() columnNames: #().

self assert: dataFrame class equals: DataFrame.
]

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

| dataFrame |
dataFrame := DataFrame withRows: #() rowNames: #().

self assert: dataFrame class equals: DataFrame.
]

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

| dataFrame |
dataFrame := DataFrame withRows: #() rowNames: #() columnNames: #().

self assert: dataFrame class equals: DataFrame.
]

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

Expand Down Expand Up @@ -1407,6 +1479,19 @@ DataFrameTest >> testReject [
self assert: actual equals: expected.
]

{ #category : #tests }
DataFrameTest >> testRejectEntireDataFrame [
| actual expected |

expected := DataFrame
withRows: #().

actual := df reject:
[ :row | (row at: #Population) < 10 ].

self assert: actual equals: expected.
]

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

Expand Down Expand Up @@ -1948,6 +2033,18 @@ DataFrameTest >> testSelect [
self assert: actual equals: expected.
]

{ #category : #tests }
DataFrameTest >> testSelectEmptyDataFrame [
| actual expected |

expected := DataFrame withRows: #().

actual := df select:
[ :row | (row at: #Population) > 10 ].

self assert: actual equals: expected.
]

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

Expand Down
9 changes: 6 additions & 3 deletions src/DataFrame/DataFrame.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -1155,17 +1155,20 @@ DataFrame >> select: aBlock [
Collect into a new collection like the receiver, only those elements for
which aBlock evaluates to true. Answer the new collection."

| rows selectedRows selectedRowNames selectedRowsAsArrays df |
| rows selectedRows selectedRowNames selectedColumnNames selectedRowsAsArrays df |

rows := self asArrayOfRows.
selectedRows := rows select: aBlock.
selectedRowNames := selectedRows collect: #name.
selectedRowNames := (selectedRows isEmpty)
ifTrue: [ #() ] ifFalse: [ selectedRows collect: #name ].
selectedColumnNames := (selectedRows isEmpty)
ifTrue: [ #() ] ifFalse: [ self columnNames ].
selectedRowsAsArrays := selectedRows collect: #asArray.

df := self class
withRows: selectedRowsAsArrays
rowNames: selectedRowNames
columnNames: self columnNames.
columnNames: selectedColumnNames.

^ df
]
Expand Down
26 changes: 16 additions & 10 deletions src/DataFrame/DataFrameInternal.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ DataFrameInternal class >> withColumns: anArrayOfArrays [

| numberOfRows numberOfColumns dfInternal |

numberOfRows := anArrayOfArrays first size.
numberOfColumns := anArrayOfArrays size.
numberOfRows := 0.
1 to: numberOfColumns do: [ :index |
numberOfRows := numberOfRows max: (anArrayOfArrays at: index) size
].
dfInternal := self new: (numberOfRows @ numberOfColumns ).

1 to: numberOfRows do: [ :i |
1 to: numberOfColumns do: [ :j |
dfInternal at: i at: j put:
((anArrayOfArrays at: j) asArray at: i) ] ].
dfInternal at: i at: j
put: ((anArrayOfArrays at: j) asArray at: i ifAbsent: nil) ] ].

^ dfInternal
]
Expand All @@ -54,14 +57,17 @@ DataFrameInternal class >> withRows: anArrayOfArrays [
| numberOfRows numberOfColumns dfInternal |

numberOfRows := anArrayOfArrays size.
numberOfColumns := anArrayOfArrays first size.
dfInternal := self new: (numberOfRows @ numberOfColumns ).
numberOfColumns := 0.
1 to: numberOfRows do: [ :index |
numberOfColumns := numberOfColumns max: (anArrayOfArrays at: index) size
].
dfInternal := self new: numberOfRows @ numberOfColumns.

1 to: numberOfRows do: [ :i |
1 to: numberOfColumns do: [ :j |
dfInternal at: i at: j
put: ((anArrayOfArrays at: i) asArray at: j ifAbsent: nil) ] ].

1 to: numberOfRows do: [ :i |
1 to: numberOfColumns do: [ :j |
dfInternal at: i at: j put:
((anArrayOfArrays at: i) asArray at: j) ] ].

^ dfInternal
]

Expand Down