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
13 changes: 13 additions & 0 deletions src/DataFrame-Tests/DataFrameTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5133,6 +5133,19 @@ DataFrameTest >> testToColumnsAtApplyElementwise [
self assert: df equals: expected
]

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

| expectedString |
expectedString := '| # | City | Population | BeenThere |
| --- | ----------- | ---------- | --------- |
| ''A'' | ''Barcelona'' | 1.609 | true |
| ''B'' | ''Dubai'' | 2.789 | true |
| ''C'' | ''London'' | 8.788 | false |
'.
self assert: df toMarkdown equals: expectedString
]

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

Expand Down
46 changes: 46 additions & 0 deletions src/DataFrame/DataFrame.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,52 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [
self toColumnAt: each applyElementwise: aBlock ]
]

{ #category : #converting }
DataFrame >> toMarkdown [
" Prints the DataFrame as a Markdown formatted table"

| markdown columnWidths dataFrame |
dataFrame := self copy.
dataFrame addColumn: dataFrame rowNames named: '#' atPosition: 1.
markdown := WriteStream on: String new.
markdown nextPutAll: '| '.

columnWidths := dataFrame columnNames collect: [ :columnName |
| maxWidth |
maxWidth := columnName size.
dataFrame rows do: [ :row |
| value |
value := row at: columnName.
maxWidth := maxWidth max: value printString size ].
maxWidth ].

dataFrame columnNames withIndexDo: [ :columnName :index |
| paddedColumnName |
paddedColumnName := columnName padRightTo: (columnWidths at: index).
markdown nextPutAll: paddedColumnName , ' | ' ].
markdown cr.
markdown nextPutAll: '| '.

columnWidths do: [ :width |
| secondRow |
secondRow := '-'.
width - 1 timesRepeat: [ secondRow := secondRow , '-' ].
markdown nextPutAll: secondRow , ' | ' ].

markdown cr.

dataFrame asArrayOfRows do: [ :row |
markdown nextPutAll: '| '.
row withIndexDo: [ :value :index |
| paddedValue |
paddedValue := value printString padRightTo:
(columnWidths at: index).
markdown nextPutAll: paddedValue , ' | ' ].
markdown cr ].

^ markdown contents
]

{ #category : #geometry }
DataFrame >> transposed [
"Returns a transposed DataFrame. Columns become rows and rows become columns."
Expand Down