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 @@ -5199,6 +5199,19 @@ DataFrameTest >> testToMarkdown [
self assert: df toMarkdown equals: expectedString
]

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

| expectedString |
expectedString := '# City Population BeenThere
''A'' ''Barcelona'' 1.609 true
''B'' ''Dubai'' 2.789 true
''C'' ''London'' 8.788 false
'.

self assert: df toString equals: expectedString
]

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

Expand Down
37 changes: 37 additions & 0 deletions src/DataFrame/DataFrame.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,43 @@ DataFrame >> toMarkdown [
^ markdown contents
]

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

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

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).
stringTable nextPutAll: paddedColumnName, ' ' ].
stringTable cr.



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

^ stringTable contents
]

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