Search in sources :

Example 11 with ColumnType

use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.

the class XlsxReader method createColumn.

private Column<?> createColumn(int colNum, String name, Sheet sheet, int excelColNum, TableRange tableRange, XlsxReadOptions options) {
    Column<?> column;
    ColumnType columnType = options.columnTypeReadOptions().columnType(colNum, name).orElse(calculateColumnTypeForColumn(sheet, excelColNum, tableRange).orElse(ColumnType.STRING));
    column = columnType.create(name);
    return column;
}
Also used : ColumnType(tech.tablesaw.api.ColumnType)

Example 12 with ColumnType

use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.

the class FileReader method getColumnTypes.

/**
 * Returns an array containing the inferred columnTypes for the file being read, as calculated by
 * the ColumnType inference logic. These types may not be correct.
 */
public ColumnType[] getColumnTypes(Reader reader, ReadOptions options, int linesToSkip, AbstractParser<?> parser, String[] columnNames) {
    if (parser.getContext() == null)
        parser.beginParsing(reader);
    for (int i = 0; i < linesToSkip; i++) {
        parser.parseNext();
    }
    ColumnTypeDetector detector = new ColumnTypeDetector(options.columnTypesToDetect());
    ColumnType[] columnTypes = detector.detectColumnTypes(new Iterator<String[]>() {

        String[] nextRow = parser.parseNext();

        @Override
        public boolean hasNext() {
            return nextRow != null;
        }

        @Override
        public String[] next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            String[] tmp = nextRow;
            nextRow = parser.parseNext();
            return tmp;
        }
    }, options);
    // If there are columnTypes configured by the user use them
    for (int i = 0; i < columnTypes.length; i++) {
        boolean hasColumnName = columnNames != null && i < columnNames.length;
        Optional<ColumnType> configuredColumnType = options.columnTypeReadOptions().columnType(i, hasColumnName ? columnNames[i] : null);
        if (configuredColumnType.isPresent()) {
            columnTypes[i] = configuredColumnType.get();
        }
    }
    return columnTypes;
}
Also used : ColumnType(tech.tablesaw.api.ColumnType) NoSuchElementException(java.util.NoSuchElementException)

Example 13 with ColumnType

use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.

the class FileReader method parseRows.

protected Table parseRows(ReadOptions options, boolean headerOnly, Reader reader, ReadOptions.ColumnTypeReadOptions columnTypeReadOptions, AbstractParser<?> parser, int sampleSize) {
    parser.beginParsing(reader);
    Table table = Table.create(options.tableName());
    List<String> headerRow = Lists.newArrayList(getColumnNames(options, columnTypeReadOptions, parser));
    @SuppressWarnings({ "UnstableApiUsage", "OptionalGetWithoutIsPresent" }) ColumnType[] types = Streams.mapWithIndex(headerRow.stream(), (columnName, idx) -> columnTypeReadOptions.columnType((int) idx, columnName)).filter(Optional::isPresent).map(Optional::get).toArray(ColumnType[]::new);
    for (int x = 0; x < types.length; x++) {
        if (types[x] != SKIP) {
            String columnName = cleanName(headerRow.get(x));
            if (Strings.isNullOrEmpty(columnName)) {
                columnName = "Column " + table.columnCount();
            }
            Column<?> newColumn = types[x].create(columnName);
            table.addColumns(newColumn);
        }
    }
    if (!headerOnly) {
        String[] columnNames = selectColumnNames(headerRow, types);
        int[] columnIndexes = new int[columnNames.length];
        for (int i = 0; i < columnIndexes.length; i++) {
            // get the index in the original table, which includes skipped fields
            columnIndexes[i] = headerRow.indexOf(columnNames[i]);
        }
        addRows(options, types, parser, table, columnIndexes, sampleSize);
    }
    return table;
}
Also used : Logger(org.slf4j.Logger) ColumnType(tech.tablesaw.api.ColumnType) AbstractParser(com.univocity.parsers.common.AbstractParser) Iterator(java.util.Iterator) Table(tech.tablesaw.api.Table) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Random(java.util.Random) Reader(java.io.Reader) Streams(com.google.common.collect.Streams) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) AbstractColumnParser(tech.tablesaw.columns.AbstractColumnParser) Map(java.util.Map) Column(tech.tablesaw.columns.Column) SKIP(tech.tablesaw.api.ColumnType.SKIP) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) Optional(java.util.Optional)

Example 14 with ColumnType

use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.

the class Rows method compareRows.

public static boolean compareRows(int rowInOriginal, Table original, Table tempTable) {
    int columnCount = original.columnCount();
    boolean result;
    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
        ColumnType columnType = original.column(columnIndex).type();
        result = columnType.compare(rowInOriginal, tempTable.column(columnIndex), original.column(columnIndex));
        if (!result) {
            return false;
        }
    }
    return true;
}
Also used : ColumnType(tech.tablesaw.api.ColumnType)

Example 15 with ColumnType

use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.

the class ASTDataset method normal.

@Override
public IASTAppendable normal(boolean nilIfUnevaluated) {
    Cache<IAST, IAST> cache = CacheBuilder.newBuilder().maximumSize(500).build();
    final List<String> names = fTable.columnNames();
    List<IStringX> namesStr = new ArrayList<IStringX>(names.size());
    for (int i = 0; i < names.size(); i++) {
        namesStr.add(F.stringx(names.get(i)));
    }
    if (names.size() == 1) {
        Column<?> column = fTable.column(names.get(0));
        ColumnType t = column.type();
        IASTAppendable resultList = F.ListAlloc(column.size());
        for (int j = 0; j < column.size(); j++) {
            Object obj = column.get(j);
            IExpr expr = F.NIL;
            expr = dataToExpr(obj, t);
            resultList.append(expr);
        }
        return resultList;
    }
    IASTAppendable list = F.ListAlloc(names.size());
    int size = fTable.rowCount();
    for (int k = 0; k < size; k++) {
        Row row = fTable.row(k);
        IAssociation assoc = F.assoc();
        for (int j = 0; j < row.columnCount(); j++) {
            String columnName = names.get(j);
            IStringX colName = namesStr.get(j);
            ColumnType t = row.getColumnType(columnName);
            Object obj = row.getObject(j);
            if (t.equals(ColumnType.EXPR)) {
                IExpr expr = (IExpr) obj;
                ruleCache(cache, assoc, F.Rule(colName, expr));
            } else if (t.equals(ColumnType.BOOLEAN)) {
                Boolean b = row.getBoolean(j);
                if (b) {
                    ruleCache(cache, assoc, F.Rule(colName, S.True));
                } else {
                    ruleCache(cache, assoc, F.Rule(colName, S.False));
                }
            } else if (t.equals(ColumnType.SHORT)) {
                short sValue = row.getShort(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(sValue)));
            } else if (t.equals(ColumnType.INTEGER)) {
                int iValue = row.getInt(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(iValue)));
            } else if (t.equals(ColumnType.LONG)) {
                long lValue = row.getLong(j);
                ruleCache(cache, assoc, F.Rule(colName, F.ZZ(lValue)));
            } else if (t.equals(ColumnType.FLOAT)) {
                float fValue = row.getFloat(j);
                ruleCache(cache, assoc, F.Rule(colName, F.num(fValue)));
            } else if (t.equals(ColumnType.DOUBLE)) {
                double dValue = row.getDouble(j);
                ruleCache(cache, assoc, F.Rule(colName, F.num(dValue)));
            } else if (t.equals(ColumnType.STRING)) {
                ruleCache(cache, assoc, F.Rule(colName, F.stringx(row.getString(j))));
            } else if (t.equals(ColumnType.LOCAL_DATE_TIME)) {
                LocalDateTime lDate = row.getDateTime(j);
                ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate)));
            } else if (t.equals(ColumnType.LOCAL_DATE)) {
                LocalDate lDate = row.getDate(j);
                ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate.atStartOfDay())));
            } else if (t.equals(ColumnType.LOCAL_TIME)) {
                LocalTime lTime = row.getTime(j);
                ruleCache(cache, assoc, F.Rule(colName, TimeObjectExpr.newInstance(lTime)));
            } else if (t.equals(ColumnType.SKIP)) {
                // ruleCache(cache, assoc, F.Rule(colName, F.Missing));
                ruleCache(cache, assoc, F.Rule(colName, F.Missing(S.NotAvailable)));
            } else {
                IExpr valueStr = F.stringx(obj.toString());
                ruleCache(cache, assoc, F.Rule(colName, valueStr));
            }
        }
        if (size == 1) {
            return assoc;
        }
        list.append(assoc);
    }
    return list;
}
Also used : LocalDateTime(java.time.LocalDateTime) IAssociation(org.matheclipse.core.interfaces.IAssociation) ColumnType(tech.tablesaw.api.ColumnType) LocalTime(java.time.LocalTime) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) Row(tech.tablesaw.api.Row)

Aggregations

ColumnType (tech.tablesaw.api.ColumnType)17 Table (tech.tablesaw.api.Table)7 ArrayList (java.util.ArrayList)6 Column (tech.tablesaw.columns.Column)6 List (java.util.List)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 Reader (java.io.Reader)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 DoubleColumn (tech.tablesaw.api.DoubleColumn)2 IntColumn (tech.tablesaw.api.IntColumn)2 LongColumn (tech.tablesaw.api.LongColumn)2 Row (tech.tablesaw.api.Row)2 Objects (com.google.common.base.Objects)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Streams (com.google.common.collect.Streams)1 AbstractParser (com.univocity.parsers.common.AbstractParser)1 ResultSetMetaData (java.sql.ResultSetMetaData)1