Skip to content

Commit d9418bd

Browse files
authored
Merge pull request #264 from Joshua-Dias-Barreto/chainedSorting
Fixes #263. Added Collection>> #asDataFrame with tests.
2 parents 8e3d7f2 + 7756b0b commit d9418bd

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,52 @@ DataFrameTest >> testAsArrayOfRowsWithName [
421421
equals: expected asOrderedCollection
422422
]
423423

424+
{ #category : #tests }
425+
DataFrameTest >> testAsDataFrame [
426+
427+
| actual expected |
428+
actual := #( #( 1 2 3 )
429+
#( 4 5 6 )
430+
#( 7 8 9 ) ) asDataFrame.
431+
expected := DataFrame withRows: #( #( 1 2 3 ) #( 4 5 6 ) #( 7 8 9 ) ).
432+
433+
self assert: actual equals: expected
434+
]
435+
436+
{ #category : #tests }
437+
DataFrameTest >> testAsDataFrameEmpty [
438+
439+
| actual expected |
440+
actual := #() asDataFrame.
441+
expected := DataFrame new.
442+
443+
self assert: actual equals: expected
444+
]
445+
446+
{ #category : #tests }
447+
DataFrameTest >> testAsDataFrameEmptyColumns [
448+
449+
| actual expected |
450+
actual := #( #()
451+
#()
452+
#() ) asDataFrame.
453+
expected := DataFrame new: 3 @ 0.
454+
455+
self assert: actual equals: expected
456+
]
457+
458+
{ #category : #tests }
459+
DataFrameTest >> testAsDataFrameWithUnevenRowElements [
460+
461+
| actual expected |
462+
actual := #( #( 1 2 )
463+
#( 3 4 5 )
464+
#( 6 7 8 9 ) ) asDataFrame.
465+
expected := DataFrame withRows: #( #( 1 2 ) #( 3 4 5 ) #( 6 7 8 9 ) ).
466+
467+
self assert: actual equals: expected
468+
]
469+
424470
{ #category : #tests }
425471
DataFrameTest >> testAt [
426472

src/DataFrame/Collection.extension.st

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ Collection >> ** arg [
66
^ self raisedTo: arg
77
]
88

9+
{ #category : #'*DataFrame' }
10+
Collection >> asDataFrame [
11+
12+
| numberOfRows numberOfColumns dataFrame |
13+
numberOfRows := self size.
14+
numberOfColumns := 0.
15+
numberOfRows = 0 ifFalse: [
16+
numberOfColumns := (self collect: #size) max ].
17+
dataFrame := DataFrame new: numberOfRows @ numberOfColumns.
18+
19+
1 to: numberOfRows do: [ :rowIndex |
20+
| row |
21+
row := self at: rowIndex.
22+
1 to: numberOfColumns do: [ :colIndex |
23+
| value |
24+
value := row at: colIndex ifAbsent: [ nil ].
25+
dataFrame at: rowIndex at: colIndex put: value ] ].
26+
27+
28+
^ dataFrame
29+
]
30+
931
{ #category : #'*DataFrame-Core-Base' }
1032
Collection >> asDataSeries [
1133

0 commit comments

Comments
 (0)