Skip to content

Commit 81fa574

Browse files
authored
Merge pull request #255 from Joshua-Dias-Barreto/markdownPrinter
Fixed #106. DataFrame>> #addRow: now considers key ordering
2 parents 15158c1 + d1b3406 commit 81fa574

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,28 @@ DataFrameTest >> testAddRowAtPosition [
236236
self assert: df equals: expected
237237
]
238238

239+
{ #category : #tests }
240+
DataFrameTest >> testAddRowAtPositionWithKeysAsColumnNames [
241+
| row expected |
242+
243+
row := DataSeries
244+
withKeys: #( 'Population' 'BeenThere' 'City' )
245+
values: #( 2.141 true Paris )
246+
name: 'X'.
247+
df addRow: row atPosition: 2.
248+
249+
expected := DataFrame withRows: #(
250+
(Barcelona 1.609 true)
251+
(Paris 2.141 true)
252+
(Dubai 2.789 true)
253+
(London 8.788 false)).
254+
255+
expected rowNames: #(A X B C).
256+
expected columnNames: #(City Population BeenThere).
257+
258+
self assert: df equals: expected
259+
]
260+
239261
{ #category : #tests }
240262
DataFrameTest >> testAddRowNameMustBeDistinct [
241263

@@ -295,6 +317,28 @@ DataFrameTest >> testAddRowSizeMismatch [
295317
self should: aBlock raise: SizeMismatch
296318
]
297319

320+
{ #category : #tests }
321+
DataFrameTest >> testAddRowWithKeysAsColumnNames [
322+
323+
| row expected |
324+
row := DataSeries
325+
withKeys: #( 'Population' 'BeenThere' 'City' )
326+
values: #( 2.141 true Paris )
327+
name: 'X'.
328+
df addRow: row.
329+
330+
expected := DataFrame withRows:
331+
#( #( Barcelona 1.609 true )
332+
#( Dubai 2.789 true )
333+
#( London 8.788 false )
334+
#( Paris 2.141 true ) ).
335+
336+
expected rowNames: #( A B C X ).
337+
expected columnNames: #( City Population BeenThere ).
338+
339+
self assert: df equals: expected
340+
]
341+
298342
{ #category : #tests }
299343
DataFrameTest >> testApplyElementwise [
300344

src/DataFrame/DataFrame.class.st

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,23 @@ DataFrame >> addEmptyRowNamed: aString atPosition: aNumber [
397397
{ #category : #adding }
398398
DataFrame >> addRow: aDataSeries [
399399
"Add DataSeries as a new row at the end"
400-
self addRow: aDataSeries asArray named: aDataSeries name
400+
401+
self addRow: aDataSeries atPosition: self numberOfRows + 1
401402
]
402403

403404
{ #category : #adding }
404405
DataFrame >> addRow: aDataSeries atPosition: aNumber [
405406
"Add DataSeries as a new row at the given position"
406-
self addRow: aDataSeries named: aDataSeries name atPosition: aNumber
407+
408+
| row |
409+
row := Array new: self columnNames size.
410+
self columnNames withIndexDo: [ :columnName :index |
411+
| value |
412+
value := aDataSeries
413+
at: columnName
414+
ifAbsent: [ aDataSeries atIndex: index ].
415+
row at: index put: value ].
416+
self addRow: row named: aDataSeries name atPosition: aNumber
407417
]
408418

409419
{ #category : #adding }
@@ -2164,7 +2174,7 @@ DataFrame >> size [
21642174
{ #category : #sorting }
21652175
DataFrame >> sortBy: columnName [
21662176
"Rearranges the rows of the data frame in ascending order of the values in the column named columnName"
2167-
2177+
21682178
self sortBy: columnName using: [ :a :b | a <= b ]
21692179
]
21702180

0 commit comments

Comments
 (0)