Skip to content

Commit

Permalink
Support percentage by parsing percentage to double (#906)
Browse files Browse the repository at this point in the history
* support percentage by parsing percentage to double and add corresponding tests

* support percentage by parsing percentage to double and add corresponding tests(update)

* simpify code

* Solve conflict
  • Loading branch information
Kerwinooooo committed May 22, 2021
1 parent 16147ed commit 04e6d6b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
19 changes: 17 additions & 2 deletions core/src/main/java/tech/tablesaw/columns/numbers/DoubleParser.java
@@ -1,6 +1,8 @@
package tech.tablesaw.columns.numbers;

import com.google.common.collect.Lists;
import java.text.NumberFormat;
import java.text.ParseException;
import tech.tablesaw.api.ColumnType;
import tech.tablesaw.columns.AbstractColumnParser;
import tech.tablesaw.io.ReadOptions;
Expand All @@ -24,9 +26,14 @@ public boolean canParse(String s) {
return true;
}
try {
Double.parseDouble(AbstractColumnParser.remove(s, ','));
if (isPercent(AbstractColumnParser.remove(s, ','))) {
s = AbstractColumnParser.remove(s, ',');
Number number = NumberFormat.getPercentInstance().parse(s);
} else {
Double.parseDouble(AbstractColumnParser.remove(s, ','));
}
return true;
} catch (NumberFormatException e) {
} catch (NumberFormatException | ParseException e) {
// it's all part of the plan
return false;
}
Expand All @@ -42,6 +49,14 @@ public double parseDouble(String s) {
if (isMissing(s)) {
return DoubleColumnType.missingValueIndicator();
}
if (isPercent(AbstractColumnParser.remove(s, ','))) {
s = AbstractColumnParser.remove(s, ',').substring(0, s.length() - 1);
return Double.parseDouble(s) / 100.0;
}
return Double.parseDouble(AbstractColumnParser.remove(s, ','));
}

private boolean isPercent(String s) {
return s.charAt(s.length() - 1) == '%';
}
}
52 changes: 52 additions & 0 deletions core/src/test/java/tech/tablesaw/api/DoubleParserTest.java
@@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tech.tablesaw.api;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import tech.tablesaw.columns.numbers.DoubleParser;

class DoubleParserTest {
@Test
void testCanParse1() {
DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);
assertTrue(doubleParser.canParse("1.3"));
}

@Test
void testCanParse2() {
DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);
assertTrue(doubleParser.canParse("1.3%"));
}

@Test
void testCanParse3() {
DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);
assertFalse(doubleParser.canParse("%"));
}

@Test
void testParseDouble1() {
DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);
assertEquals(1.3, doubleParser.parseDouble("1.3"));
}

@Test
void testParseDouble2() {
DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);
assertEquals(0.012, doubleParser.parseDouble("1.2%"));
}
}
23 changes: 23 additions & 0 deletions core/src/test/java/tech/tablesaw/io/csv/CsvReaderTest.java
Expand Up @@ -49,10 +49,13 @@
import tech.tablesaw.api.ColumnType;
import tech.tablesaw.api.DateColumn;
import tech.tablesaw.api.DateTimeColumn;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.LongColumn;
import tech.tablesaw.api.ShortColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.columns.numbers.DoubleColumnType;
import tech.tablesaw.columns.numbers.NumberColumnFormatter;
import tech.tablesaw.io.AddCellToColumnException;

/** Tests for CSV Reading */
Expand Down Expand Up @@ -846,6 +849,26 @@ public void preserveQuote() throws IOException {
assertEquals(table.get(0, 0), out.get(0, 0));
}

@Test
public void testReadCsvWithPercentage1() throws IOException {
Table table = Table.read().csv(CsvReadOptions.builder("../data/currency_percent.csv"));
assertEquals(DoubleColumnType.instance(), table.columnTypes()[1]);
assertEquals(DoubleColumnType.instance(), table.columnTypes()[2]);
}

@Test
public void testReadCsvWithPercentage2() throws IOException {
Table table = Table.read().csv(CsvReadOptions.builder("../data/currency_percent.csv"));
DoubleColumn column = (DoubleColumn) table.column(1);
assertEquals("0.0132", column.getString(0));
assertEquals("0.32768", column.getString(1));
assertEquals("1", column.getString(2));
column.setPrintFormatter(NumberColumnFormatter.percent(2));
assertEquals("1.32%", column.getString(0));
assertEquals("32.77%", column.getString(1));
assertEquals("100.00%", column.getString(2));
}

@Test
public void testSkipRowsWithInvalidColumnCount() throws IOException {
Table table =
Expand Down
4 changes: 4 additions & 0 deletions data/currency_percent.csv
@@ -0,0 +1,4 @@
Currency,Percentage,double
$10.3,1.32%,13.14
$50.10,32.768%,35
$10,100%,93.131

0 comments on commit 04e6d6b

Please sign in to comment.