Skip to content

Commit b8d743e

Browse files
authored
Merge pull request #279 from hernanmd/df_create_with_colnames
Create DataFrame from CSV setting the column names and separator
2 parents 9bbfeae + 188c86c commit b8d743e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/DataFrame-IO-Tests/DataFrameCsvReaderTest.class.st

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ DataFrameCsvReaderTest >> testReadCsv [
7878
self assert: actualDataFrame equals: dataFrameWithoutRowNames
7979
]
8080

81+
{ #category : #tests }
82+
DataFrameCsvReaderTest >> testReadCsvWithColumnNamesWithSeparator [
83+
84+
| controlDataFrame customColumnNames |
85+
86+
customColumnNames := #('Col1' 'Col2' 'Col3' 'Col4').
87+
88+
controlDataFrame := DataFrame
89+
readFromCsv: tabCsvFile
90+
withColumnNames: customColumnNames
91+
separator: Character tab.
92+
93+
self
94+
assertCollection: controlDataFrame columnNames
95+
equals: customColumnNames asOrderedCollection.
96+
97+
"The original header is counted as a normal row now"
98+
self
99+
assert: controlDataFrame size
100+
equals: 6.
101+
]
102+
81103
{ #category : #tests }
82104
DataFrameCsvReaderTest >> testReadCsvWithRowNames [
83105
| actualDataFrame |

src/DataFrame-IO/DataFrame.extension.st

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ DataFrame class >> readFromCsv: aFileReference [
3737
^ self readFrom: aFileReference using: reader
3838
]
3939

40+
{ #category : #'*DataFrame-IO' }
41+
DataFrame class >> readFromCsv: aFileReference withColumnNames: anArrayOfColumnNames separator: aSeparator [
42+
43+
| dataFrame |
44+
45+
dataFrame := DataFrameCsvReader new
46+
separator: aSeparator;
47+
columnNames: anArrayOfColumnNames;
48+
readFrom: aFileReference.
49+
dataFrame calculateDataTypes.
50+
^ dataFrame.
51+
]
52+
4053
{ #category : #'*DataFrame-IO' }
4154
DataFrame class >> readFromCsv: aFileReference withSeparator: aSeparator [
4255
| reader |

src/DataFrame-IO/DataFrameCsvReader.class.st

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ Class {
4242
#category : #'DataFrame-IO-Core'
4343
}
4444

45+
{ #category : #reading }
46+
DataFrameCsvReader >> columnNames: aCollectionOfString [
47+
"Set the receiver' s column names"
48+
49+
columnNames := aCollectionOfString
50+
]
51+
4552
{ #category : #reading }
4653
DataFrameCsvReader >> createDataFrame [
4754
| df |
@@ -91,7 +98,9 @@ DataFrameCsvReader >> initialize [
9198

9299
{ #category : #reading }
93100
DataFrameCsvReader >> readColumnNamesWith: aReader [
94-
columnNames := aReader readHeader.
101+
"Set the receiver's column names if they were not manually set"
102+
103+
columnNames ifNil: [ columnNames := aReader readHeader ].
95104

96105
self includeRowNames ifTrue: [
97106
columnNames := columnNames copyWithoutFirst ]

0 commit comments

Comments
 (0)