Skip to content

Commit

Permalink
Load/store DataFrames from CSV #6
Browse files Browse the repository at this point in the history
* expanding CSV test suite
* supporting commons-csv formats
  • Loading branch information
andrus committed Feb 18, 2019
1 parent b4e027f commit 5e8e500
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dflib-csv/src/main/java/com/nhl/dflib/csv/CsvLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ public CsvLoader columnType(String column, ValueMapper<String, ?> typeConverter)
return this;
}

/**
* Optionally sets the style or format of the imported CSV. CSVFormat comes from "commons-csv" library and
* contains a number of predefined formats, such as CSVFormat.MYSQL, etc. It also allows to customize the format
* further, by defining custom delimiters, line separators, etc.
*
* @param format a format object defined in commons-csv library
* @return this loader instance
*/
public CsvLoader format(CSVFormat format) {
this.format = format;
return this;
}

public DataFrame fromFile(String filePath) {
try (Reader r = readerFromFilePath(filePath)) {
return fromReader(r);
Expand Down
21 changes: 21 additions & 0 deletions dflib-csv/src/test/java/com/nhl/dflib/csv/CsvLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nhl.dflib.DFAsserts;
import com.nhl.dflib.DataFrame;
import com.nhl.dflib.map.ValueMapper;
import org.apache.commons.csv.CSVFormat;
import org.junit.Test;

import java.io.StringReader;
Expand Down Expand Up @@ -32,6 +33,26 @@ public void testFromFile() {
.expectRow(1, "4", "5", "6");
}

@Test
public void testFromFile_DefaultFormat_Excel() {
DataFrame df = new CsvLoader().fromFile(csvPath("from_excel.csv"));
new DFAsserts(df, "A", "b", "C")
.expectHeight(2)
.expectRow(0, "commas,quotes\"'", "-85.7", "3")
.expectRow(1, "with, commas", "5.50001", "6");
}

@Test
public void testFromFile_MySQLFormat() {
DataFrame df = new CsvLoader().format(CSVFormat.MYSQL).fromFile(csvPath("from_mysql.csv"));
new DFAsserts(df, "1", "3365430", " xxxx")
.expectHeight(4)
.expectRow(0, "2", "2289959", "yyyy")
.expectRow(1, "3", "3995478", "zzzzz")
.expectRow(2, "4", "4112467", "nnn")
.expectRow(3, "5", "1474364", "eee");
}

@Test
public void testFromFile_Columns() {
DataFrame df = new CsvLoader().columns("X", "Y", "Z").fromFile(csvPath("f1.csv"));
Expand Down
3 changes: 3 additions & 0 deletions dflib-csv/src/test/resources/com/nhl/dflib/csv/from_excel.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A,b,C
"commas,quotes""'",-85.7,3
"with, commas",5.50001,6
5 changes: 5 additions & 0 deletions dflib-csv/src/test/resources/com/nhl/dflib/csv/from_mysql.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 3365430 xxxx
2 2289959 yyyy
3 3995478 zzzzz
4 4112467 nnn
5 1474364 eee

0 comments on commit 5e8e500

Please sign in to comment.