Repository: jtablesaw/tablesaw
Pull request #429 (2018-12-31 16:55:00)

Nicer string to number encoding
Thanks for contributing.

- [X] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Before it was returning `[-127.0, -126.0, -125.0, -127.0, -124.0, -123.0, -122.0, -126.0]`. Now it's returning non-negative values starting from 0

## Testing

Added a unit test



Patch:
diff --git a/core/src/main/java/tech/tablesaw/api/StringColumn.java b/core/src/main/java/tech/tablesaw/api/StringColumn.java
index d6eadfe2..a0424ba6 100644
--- a/core/src/main/java/tech/tablesaw/api/StringColumn.java
+++ b/core/src/main/java/tech/tablesaw/api/StringColumn.java
@@ -15,7 +15,7 @@
 package tech.tablesaw.api;
 
 import com.google.common.base.Preconditions;
-import it.unimi.dsi.fastutil.ints.IntArrayList;
+
 import it.unimi.dsi.fastutil.ints.IntComparator;
 import tech.tablesaw.columns.AbstractColumn;
 import tech.tablesaw.columns.AbstractParser;
@@ -390,20 +390,10 @@ public class StringColumn extends AbstractColumn<String>
         return StringColumn.create(name() + " Unique values", strings);
     }
 
-    /**
-     * Returns the integers that back this column.
-     *
-     * @return data as {@link IntArrayList}
-     */
-    public IntArrayList data() {
-        return lookupTable.dataAsIntArray();
-    }
-
     public IntColumn asNumberColumn() {
-        IntColumn numberColumn = IntColumn.create(this.name() + ": codes");
-        IntArrayList data = data();
+        IntColumn numberColumn = IntColumn.create(this.name() + ": codes", size());
         for (int i = 0; i < size(); i++) {
-            numberColumn.append(data.getInt(i));
+            numberColumn.set(i, lookupTable.firstIndexOf(lookupTable.getValueForIndex(i)));
         }
         return numberColumn;
     }
@@ -490,9 +480,9 @@ public class StringColumn extends AbstractColumn<String>
     }
 
     public double[] asDoubleArray() {
-        double[] doubles = new double[data().size()];
+        double[] doubles = new double[size()];
         for (int i = 0; i < size(); i++) {
-            doubles[i] = data().getInt(i);
+            doubles[i] = lookupTable.firstIndexOf(lookupTable.getValueForIndex(i));
         }
         return doubles;
     }


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/api/StringColumnTest.java b/core/src/test/java/tech/tablesaw/api/StringColumnTest.java
index 85e91062..e2a1610f 100644
--- a/core/src/test/java/tech/tablesaw/api/StringColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/api/StringColumnTest.java
@@ -14,18 +14,22 @@
 
 package tech.tablesaw.api;
 
-import org.junit.Before;
-import org.junit.Test;
-import tech.tablesaw.TestDataUtil;
-import tech.tablesaw.columns.strings.StringColumnFormatter;
-import tech.tablesaw.selection.Selection;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static tech.tablesaw.columns.strings.StringPredicates.isEqualToIgnoringCase;
 
 import java.util.Arrays;
 import java.util.List;
 import java.util.function.Function;
 
-import static org.junit.Assert.*;
-import static tech.tablesaw.columns.strings.StringPredicates.isEqualToIgnoringCase;
+import org.junit.Before;
+import org.junit.Test;
+
+import tech.tablesaw.TestDataUtil;
+import tech.tablesaw.columns.strings.StringColumnFormatter;
+import tech.tablesaw.selection.Selection;
 
 public class StringColumnTest {
 
@@ -497,4 +501,21 @@ TODO: fix
         assertEquals("foo bam", result.get(0));
         assertEquals("bar bam", result.get(1));
     }
-}
\ No newline at end of file
+
+    @Test
+    public void asNumberColumn() {
+        String[] words = {"foo", "bar", "larry", "foo", "lion", "ben", "tiger", "bar"};
+        StringColumn wordColumn = StringColumn.create("words", words);
+        IntColumn result = wordColumn.asNumberColumn();
+        assertArrayEquals(new double[] { 0.0, 1.0, 2.0, 0.0, 4.0, 5.0, 6.0, 1.0 }, result.asDoubleArray(), 0.000_000_1);
+    }
+
+    @Test
+    public void asDoubleArray() {
+        String[] words = {"foo", "bar", "larry", "foo", "lion", "ben", "tiger", "bar"};
+        StringColumn wordColumn = StringColumn.create("words", words);
+        double[] result = wordColumn.asDoubleArray();
+        assertArrayEquals(new double[] { 0.0, 1.0, 2.0, 0.0, 4.0, 5.0, 6.0, 1.0 }, result, 0.000_000_1);
+    }
+
+}