use of tech.tablesaw.api.DoubleColumn in project symja_android_library by axkr.
the class RollingColumn method calc.
@SuppressWarnings({ "unchecked" })
public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OUT> function) {
// TODO: the subset operation copies the array. creating a view would likely be more efficient
Column<?> result = function.returnType().create(generateNewColumnName(function));
for (int i = 0; i < window - 1; i++) {
result.appendMissing();
}
for (int origColIndex = 0; origColIndex < column.size() - window + 1; origColIndex++) {
Selection selection = new BitmapBackedSelection();
selection.addRange(origColIndex, origColIndex + window);
INCOL subsetCol = (INCOL) column.subset(selection.toArray());
OUT answer = function.summarize(subsetCol);
if (answer instanceof Number) {
Number number = (Number) answer;
((DoubleColumn) result).append(number.doubleValue());
} else {
result.appendObj(answer);
}
}
return result;
}
use of tech.tablesaw.api.DoubleColumn in project symja_android_library by axkr.
the class XlsxReader method appendValue.
@SuppressWarnings("unchecked")
private Column<?> appendValue(Column<?> column, Cell cell) {
CellType cellType = cell.getCellType() == FORMULA ? cell.getCachedFormulaResultType() : cell.getCellType();
switch(cellType) {
case STRING:
column.appendCell(cell.getRichStringCellValue().getString());
return null;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
// This will return inconsistent results across time zones, but that matches Excel's
// behavior
LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
if (column.type() == ColumnType.STRING) {
// If column has String type try to honor it and leave the value as an string as similar
// as posible as seen in Excel
String dataFormatStyle = cell.getCellStyle().getDataFormatString();
String val;
if ("general".equalsIgnoreCase(dataFormatStyle)) {
val = new CellGeneralFormatter().format(cell.getNumericCellValue());
} else {
val = new CellDateFormatter(dataFormatStyle).format(cell.getDateCellValue());
}
column.appendCell(val);
} else {
column.appendCell(localDate.toString());
}
return null;
} else {
double num = cell.getNumericCellValue();
if (column.type() == ColumnType.INTEGER) {
Column<Integer> intColumn = (Column<Integer>) column;
if ((int) num == num) {
intColumn.append((int) num);
return null;
} else if ((long) num == num) {
Column<Long> altColumn = LongColumn.create(column.name(), column.size());
altColumn = intColumn.mapInto(s -> (long) s, altColumn);
altColumn.append((long) num);
return altColumn;
} else {
Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
altColumn = intColumn.mapInto(s -> (double) s, altColumn);
altColumn.append(num);
return altColumn;
}
} else if (column.type() == ColumnType.LONG) {
Column<Long> longColumn = (Column<Long>) column;
if ((long) num == num) {
longColumn.append((long) num);
return null;
} else {
Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
altColumn = longColumn.mapInto(s -> (double) s, altColumn);
altColumn.append(num);
return altColumn;
}
} else if (column.type() == ColumnType.DOUBLE) {
Column<Double> doubleColumn = (Column<Double>) column;
doubleColumn.append(num);
return null;
} else if (column.type() == ColumnType.STRING) {
// If column has String type try to honor it and leave the value as an string as similar
// as posible as seen in Excel
Column<String> stringColumn = (Column<String>) column;
String dataFormatStyle = cell.getCellStyle().getDataFormatString();
String val;
if ("general".equalsIgnoreCase(dataFormatStyle)) {
val = new CellGeneralFormatter().format(cell.getNumericCellValue());
} else {
val = new CellNumberFormatter(dataFormatStyle).format(cell.getNumericCellValue());
}
stringColumn.append(val);
}
}
break;
case BOOLEAN:
if (column.type() == ColumnType.BOOLEAN) {
Column<Boolean> booleanColumn = (Column<Boolean>) column;
booleanColumn.append(cell.getBooleanCellValue());
return null;
} else if (column.type() == ColumnType.STRING) {
// If column has String type try to honor it and leave the value as an string as similar
// as posible as seen in Excel
Column<String> stringColumn = (Column<String>) column;
String val = new CellGeneralFormatter().format(cell.getBooleanCellValue());
stringColumn.append(val);
}
default:
break;
}
return null;
}
use of tech.tablesaw.api.DoubleColumn in project symja_android_library by axkr.
the class CrossTab method rowPercents.
private static Table rowPercents(Table xTabCounts) {
Table pctTable = Table.create("Crosstab Row Proportions: ");
StringColumn labels = StringColumn.create(LABEL_COLUMN_NAME);
pctTable.addColumns(labels);
for (int i = 0; i < xTabCounts.rowCount(); i++) {
labels.append(xTabCounts.column(0).getString(i));
}
// create the new cols
DoubleColumn[] newColumns = new DoubleColumn[xTabCounts.columnCount() - 1];
for (int i = 1; i < xTabCounts.columnCount(); i++) {
Column<?> column = xTabCounts.column(i);
newColumns[i - 1] = DoubleColumn.create(column.name());
}
for (int i = 0; i < xTabCounts.rowCount(); i++) {
double rowTotal = xTabCounts.numberColumn(xTabCounts.columnCount() - 1).getDouble(i);
for (int c = 0; c < newColumns.length; c++) {
if (rowTotal == 0) {
newColumns[c].append(Double.NaN);
} else {
newColumns[c].append(xTabCounts.numberColumn(c + 1).getDouble(i) / rowTotal);
}
}
}
pctTable.addColumns(newColumns);
return pctTable;
}
use of tech.tablesaw.api.DoubleColumn in project symja_android_library by axkr.
the class TimeMapFunctions method timeWindow.
/**
* Returns a column containing integers representing the nth group (0-based) that a date falls
* into.
*
* <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
* after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
* to the second ("1") group.
*
* @param unit A ChronoUnit greater than or equal to a day
* @param n The number of units in each group.
* @param start The starting point of the first group; group boundaries are offsets from this
* point
*/
default DoubleColumn timeWindow(ChronoUnit unit, int n, LocalTime start) {
String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
int packedStartTime = PackedLocalTime.pack(start);
DoubleColumn numberColumn = DoubleColumn.create(newColumnName, size());
for (int i = 0; i < size(); i++) {
int packedTime = getIntInternal(i);
int result;
switch(unit) {
case HOURS:
result = PackedLocalTime.hoursUntil(packedTime, packedStartTime) / n;
numberColumn.append(result);
break;
case MINUTES:
result = PackedLocalTime.minutesUntil(packedTime, packedStartTime) / n;
numberColumn.append(result);
break;
case SECONDS:
result = PackedLocalTime.secondsUntil(packedTime, packedStartTime) / n;
numberColumn.append(result);
break;
default:
throw new UnsupportedTemporalTypeException("The ChronoUnit " + unit + " is not supported for timeWindows on times");
}
}
numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
return numberColumn;
}
use of tech.tablesaw.api.DoubleColumn in project symja_android_library by axkr.
the class NumberMapFunctions method asRatio.
/**
* Return the elements of this column as the ratios of their value and the sum of all elements
*/
default DoubleColumn asRatio() {
DoubleColumn pctColumn = DoubleColumn.create(name() + " percents", size());
double total = sum();
for (int i = 0; i < size(); i++) {
if (total != 0) {
pctColumn.set(i, getDouble(i) / total);
}
}
return pctColumn;
}
Aggregations