Skip to content

Added writeToCsv: withColumnNames: withRowNames: #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
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
27 changes: 27 additions & 0 deletions src/DataFrame-IO-Tests/DataFrameCsvWriterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,30 @@ DataFrameCsvWriterTest >> testWriteToCsvWithSeparatorTab [
expected := TestCsvStrings tabQuoteCsvString.
self assertCollection: actual lines equals: expected lines
]

{ #category : #tests }
DataFrameCsvWriterTest >> testWriteToCsvWithoutColumnNames [
| actual expected |
dataFrame writeToCsv: commaQuoteCsvFile withColumnNames: false.
actual := self readFile: commaQuoteCsvFile.
expected := TestCsvStrings commaQuoteCsvStringWithoutColumnNames.
self assert: actual lines equals: expected lines
]

{ #category : #tests }
DataFrameCsvWriterTest >> testWriteToCsvWithoutRowNames [
| actual expected |
dataFrame writeToCsv: commaQuoteCsvFile withRowNames: false.
actual := self readFile: commaQuoteCsvFile.
expected := TestCsvStrings commaQuoteCsvStringWithoutRowNames.
self assert: actual lines equals: expected lines
]

{ #category : #tests }
DataFrameCsvWriterTest >> testWriteToCsvWithoutRowNamesWithoutColumnNames [
| actual expected |
dataFrame writeToCsv: commaQuoteCsvFile withColumnNames: false withRowNames: false.
actual := self readFile: commaQuoteCsvFile.
expected := TestCsvStrings commaQuoteCsvStringWithoutRowNamesWithoutColumnNames.
self assert: actual lines equals: expected lines
]
34 changes: 34 additions & 0 deletions src/DataFrame-IO-Tests/TestCsvStrings.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ TestCsvStrings class >> commaQuoteCsvString [
'
]

{ #category : #running }
TestCsvStrings class >> commaQuoteCsvStringWithoutColumnNames [

^ '"1:10 am","2.4","true","rain"
"1:30 am","0.5","true","rain"
"1:50 am","-1.2","true","snow"
"2:10 am","-2.3","false","-"
"2:30 am","3.2","true","rain"
'
]

{ #category : #running }
TestCsvStrings class >> commaQuoteCsvStringWithoutRowNames [

^ '"temperature","precipitation","type"
"2.4","true","rain"
"0.5","true","rain"
"-1.2","true","snow"
"-2.3","false","-"
"3.2","true","rain"
'
]

{ #category : #running }
TestCsvStrings class >> commaQuoteCsvStringWithoutRowNamesWithoutColumnNames [

^ '"2.4","true","rain"
"0.5","true","rain"
"-1.2","true","snow"
"-2.3","false","-"
"3.2","true","rain"
'
]

{ #category : #running }
TestCsvStrings class >> dollarSignCsvString [

Expand Down
36 changes: 33 additions & 3 deletions src/DataFrame-IO/DataFrame.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,39 @@ DataFrame >> writeTo: aLocation using: aDataFrameWriter [

{ #category : #'*DataFrame-IO' }
DataFrame >> writeToCsv: aFileReference [
| writer |
writer := DataFrameCsvWriter new.
self writeTo: aFileReference using: writer

self
writeToCsv: aFileReference
withColumnNames: true
withRowNames: true
]

{ #category : #'*DataFrame-IO' }
DataFrame >> writeToCsv: aFileReference withColumnNames: enableColumnNames [

self
writeToCsv: aFileReference
withColumnNames: enableColumnNames
withRowNames: true
]

{ #category : #'*DataFrame-IO' }
DataFrame >> writeToCsv: aFileReference withColumnNames: enableColumnNames withRowNames: enableRowNames [
| writer |
writer := DataFrameCsvWriter new.
enableColumnNames ifFalse: [ writer disableColumnName ].
enableRowNames ifFalse: [ writer disableRowName ].
self writeTo: aFileReference using: writer.

]

{ #category : #'*DataFrame-IO' }
DataFrame >> writeToCsv: aFileReference withRowNames: enableRowNames [

self
writeToCsv: aFileReference
withColumnNames: true
withRowNames: enableRowNames
]

{ #category : #'*DataFrame-IO' }
Expand Down
55 changes: 48 additions & 7 deletions src/DataFrame-IO/DataFrameCsvWriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ Class {
'separator',
'lineEndConvention',
'fieldWriter',
'rowNameEnabled'
'rowNameEnabled',
'columnNameEnabled'
],
#category : #'DataFrame-IO-Core'
}

{ #category : #accessing }
DataFrameCsvWriter >> columnNameEnabled [

^ columnNameEnabled
]

{ #category : #accessing }
DataFrameCsvWriter >> columnNameEnabled: aBoolean [

columnNameEnabled := aBoolean = true
]

{ #category : #accessing }
DataFrameCsvWriter >> defaultColumnIndexEnabled [

^ true
]

{ #category : #'accessing - field writers' }
DataFrameCsvWriter >> defaultFieldWriter [

Expand All @@ -36,12 +55,24 @@ DataFrameCsvWriter >> defaultSeparator [
^ $,
]

{ #category : #'enable/disable' }
DataFrameCsvWriter >> disableColumnName [

self columnNameEnabled: false
]

{ #category : #'enable/disable' }
DataFrameCsvWriter >> disableRowName [

self rowNameEnabled: false
]

{ #category : #'enable/disable' }
DataFrameCsvWriter >> enableColumnName [

self columnNameEnabled: true
]

{ #category : #'enable/disable' }
DataFrameCsvWriter >> enableRowName [

Expand All @@ -67,7 +98,8 @@ DataFrameCsvWriter >> initialize [
separator := self defaultSeparator.
lineEndConvention := self defaultLineEndConvention.
fieldWriter := self defaultFieldWriter.
rowNameEnabled := self defaultRowIndexEnabled
rowNameEnabled := self defaultRowIndexEnabled.
columnNameEnabled := self defaultColumnIndexEnabled
]

{ #category : #accessing }
Expand Down Expand Up @@ -156,16 +188,15 @@ DataFrameCsvWriter >> useRawFieldWriter [

{ #category : #writing }
DataFrameCsvWriter >> write: aDataFrame to: aFileReference [

| stream writer |
stream := aFileReference writeStream.

writer := NeoCSVWriter on: stream.
fieldWriter ifNotNil: [ writer fieldWriter: fieldWriter ].
writer separator: self separator.
writer lineEndConvention: self lineEndConvention.

self rowNameEnabled ifTrue: [
self columnNameEnabled ifTrue: [
self rowNameEnabled ifTrue: [
writer
writeField: '';
writeSeparator ].
Expand All @@ -178,7 +209,17 @@ DataFrameCsvWriter >> write: aDataFrame to: aFileReference [
writeField: row name;
writeSeparator;
nextPut: row ] ]
ifFalse: [ aDataFrame do: [ :row | writer nextPut: row ] ].
ifFalse: [ aDataFrame do: [ :row | writer nextPut: row ] ]]

ifFalse: [
self rowNameEnabled ifTrue: [
aDataFrame do: [ :row |
writer
writeField: row name;
writeSeparator;
nextPut: row ] ]
ifFalse: [ aDataFrame do: [ :row | writer nextPut: row ] ] ].

writer close.

writer close
]
Loading