Skip to content

Commit 67e2515

Browse files
authored
Merge pull request #246 from Joshua-Dias-Barreto/markdownPrinter
Added DataFrame>> #toMarkdown along with tests
2 parents 2175378 + 4f61df2 commit 67e2515

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,6 +5133,19 @@ DataFrameTest >> testToColumnsAtApplyElementwise [
51335133
self assert: df equals: expected
51345134
]
51355135

5136+
{ #category : #tests }
5137+
DataFrameTest >> testToMarkdown [
5138+
5139+
| expectedString |
5140+
expectedString := '| # | City | Population | BeenThere |
5141+
| --- | ----------- | ---------- | --------- |
5142+
| ''A'' | ''Barcelona'' | 1.609 | true |
5143+
| ''B'' | ''Dubai'' | 2.789 | true |
5144+
| ''C'' | ''London'' | 8.788 | false |
5145+
'.
5146+
self assert: df toMarkdown equals: expectedString
5147+
]
5148+
51365149
{ #category : #tests }
51375150
DataFrameTest >> testTransposed [
51385151

src/DataFrame/DataFrame.class.st

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,52 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [
22592259
self toColumnAt: each applyElementwise: aBlock ]
22602260
]
22612261

2262+
{ #category : #converting }
2263+
DataFrame >> toMarkdown [
2264+
" Prints the DataFrame as a Markdown formatted table"
2265+
2266+
| markdown columnWidths dataFrame |
2267+
dataFrame := self copy.
2268+
dataFrame addColumn: dataFrame rowNames named: '#' atPosition: 1.
2269+
markdown := WriteStream on: String new.
2270+
markdown nextPutAll: '| '.
2271+
2272+
columnWidths := dataFrame columnNames collect: [ :columnName |
2273+
| maxWidth |
2274+
maxWidth := columnName size.
2275+
dataFrame rows do: [ :row |
2276+
| value |
2277+
value := row at: columnName.
2278+
maxWidth := maxWidth max: value printString size ].
2279+
maxWidth ].
2280+
2281+
dataFrame columnNames withIndexDo: [ :columnName :index |
2282+
| paddedColumnName |
2283+
paddedColumnName := columnName padRightTo: (columnWidths at: index).
2284+
markdown nextPutAll: paddedColumnName , ' | ' ].
2285+
markdown cr.
2286+
markdown nextPutAll: '| '.
2287+
2288+
columnWidths do: [ :width |
2289+
| secondRow |
2290+
secondRow := '-'.
2291+
width - 1 timesRepeat: [ secondRow := secondRow , '-' ].
2292+
markdown nextPutAll: secondRow , ' | ' ].
2293+
2294+
markdown cr.
2295+
2296+
dataFrame asArrayOfRows do: [ :row |
2297+
markdown nextPutAll: '| '.
2298+
row withIndexDo: [ :value :index |
2299+
| paddedValue |
2300+
paddedValue := value printString padRightTo:
2301+
(columnWidths at: index).
2302+
markdown nextPutAll: paddedValue , ' | ' ].
2303+
markdown cr ].
2304+
2305+
^ markdown contents
2306+
]
2307+
22622308
{ #category : #geometry }
22632309
DataFrame >> transposed [
22642310
"Returns a transposed DataFrame. Columns become rows and rows become columns."

0 commit comments

Comments
 (0)