From 7c70f3c7ce7acdc83676eee6614fb57dbd7e0bdb Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Wed, 31 May 2023 20:55:14 +0530 Subject: [PATCH 1/2] Added DataFrame>> #toMarkdown along with tests --- src/DataFrame-Tests/DataFrameTest.class.st | 13 +++++++ src/DataFrame/DataFrame.class.st | 45 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/DataFrame-Tests/DataFrameTest.class.st b/src/DataFrame-Tests/DataFrameTest.class.st index 8e6a58c2..2316c277 100644 --- a/src/DataFrame-Tests/DataFrameTest.class.st +++ b/src/DataFrame-Tests/DataFrameTest.class.st @@ -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 [ diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 5ac0c83e..994496fc 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2259,6 +2259,51 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [ self toColumnAt: each applyElementwise: aBlock ] ] +{ #category : #converting } +DataFrame >> toMarkdown [ + " Prints the DataFrame as a Markdown formatted table" + + | markdown columnWidths | + self addColumn: self rowNames named: '#' atPosition: 1. + markdown := WriteStream on: String new. + markdown nextPutAll: '| '. + + columnWidths := self columnNames collect: [ :columnName | + | maxWidth | + maxWidth := columnName size. + self rows do: [ :row | + | value | + value := row at: columnName. + maxWidth := maxWidth max: value printString size ]. + maxWidth ]. + + self 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. + + self 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." From 4f61df20769c2966475c784c00a17c079fe4b5b6 Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Thu, 1 Jun 2023 14:24:38 +0530 Subject: [PATCH 2/2] Copied the dataframe so that it doesn't change the original. --- src/DataFrame/DataFrame.class.st | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/DataFrame/DataFrame.class.st b/src/DataFrame/DataFrame.class.st index 994496fc..6d209d56 100644 --- a/src/DataFrame/DataFrame.class.st +++ b/src/DataFrame/DataFrame.class.st @@ -2263,21 +2263,22 @@ DataFrame >> toColumnsAt: arrayOfColumnNumbers applyElementwise: aBlock [ DataFrame >> toMarkdown [ " Prints the DataFrame as a Markdown formatted table" - | markdown columnWidths | - self addColumn: self rowNames named: '#' atPosition: 1. + | markdown columnWidths dataFrame | + dataFrame := self copy. + dataFrame addColumn: dataFrame rowNames named: '#' atPosition: 1. markdown := WriteStream on: String new. markdown nextPutAll: '| '. - columnWidths := self columnNames collect: [ :columnName | + columnWidths := dataFrame columnNames collect: [ :columnName | | maxWidth | maxWidth := columnName size. - self rows do: [ :row | + dataFrame rows do: [ :row | | value | value := row at: columnName. maxWidth := maxWidth max: value printString size ]. maxWidth ]. - self columnNames withIndexDo: [ :columnName :index | + dataFrame columnNames withIndexDo: [ :columnName :index | | paddedColumnName | paddedColumnName := columnName padRightTo: (columnWidths at: index). markdown nextPutAll: paddedColumnName , ' | ' ]. @@ -2292,7 +2293,7 @@ DataFrame >> toMarkdown [ markdown cr. - self asArrayOfRows do: [ :row | + dataFrame asArrayOfRows do: [ :row | markdown nextPutAll: '| '. row withIndexDo: [ :value :index | | paddedValue |