DataFrameTabularPrintWorker extends BasePrintWorker { public StringBuilder print(DataFrame df) { if (df == null) { out.append("null"); return out; } Index columns = df.getColumnsIndex(); int w = columns.size(); if (w == 0) { return out; } String[] labels = columns.getLabels(); int[] columnWidth = new int[w]; String[] columnFormat = new String[w]; DataFrameTruncator truncator = DataFrameTruncator.create(df, maxDisplayRows); for (int i = 0; i < w; i++) { columnWidth[i] = labels[i].length(); } DataFrame head = truncator.head(); List<String[]> headData = new ArrayList<>(head.height()); for (RowProxy p : head) { String[] rValue = new String[w]; for (int i = 0; i < w; i++) { rValue[i] = String.valueOf(p.get(i)); columnWidth[i] = Math.max(columnWidth[i], rValue[i].length()); } headData.add(rValue); } List<String[]> tailData = null; if (truncator.isTruncated()) { DataFrame tail = truncator.tail(); tailData = new ArrayList<>(tail.height()); for (RowProxy p : tail) { String[] rValue = new String[w]; for (int i = 0; i < w; i++) { rValue[i] = String.valueOf(p.get(i)); columnWidth[i] = Math.max(columnWidth[i], rValue[i].length()); } tailData.add(rValue); } } appendNewLine(); for (int i = 0; i < w; i++) { columnWidth[i] = Math.min(columnWidth[i], maxDisplayColumnWidth); columnFormat[i] = columnFormat(columnWidth[i], df.getColumn(i).getInferredType()); } for (int i = 0; i < w; i++) { if (i > 0) { append(" "); } appendFixedWidth(labels[i], columnWidth[i], columnFormat[i]); } appendNewLine(); for (int i = 0; i < w; i++) { if (i > 0) { append(" "); } for (int j = 0; j < columnWidth[i]; j++) { append("-"); } } for (String[] row : headData) { appendNewLine(); for (int i = 0; i < w; i++) { if (i > 0) { append(" "); } appendFixedWidth(row[i], columnWidth[i], columnFormat[i]); } } if (truncator.isTruncated()) { appendNewLine().append("..."); for (String[] row : tailData) { appendNewLine(); for (int i = 0; i < w; i++) { if (i > 0) { append(" "); } appendFixedWidth(row[i], columnWidth[i], columnFormat[i]); } } } int h = df.height(); String rowsLabel = h == 1 ? " row x " : " rows x "; String columnsLabel = w == 1 ? " column" : " columns"; appendNewLine().append(h).append(rowsLabel).append(w).append(columnsLabel); return out; } DataFrameTabularPrintWorker(StringBuilder out, int maxDisplayRows, int maxDisplayColumnWith); StringBuilder print(DataFrame df);  }
@Test public void testPrint_Full() { DataFrameTabularPrintWorker w = new DataFrameTabularPrintWorker(new StringBuilder(), 5, 10); assertEquals(System.lineSeparator() + "col1 column2" + System.lineSeparator() + "----- -------" + System.lineSeparator() + "one 1" + System.lineSeparator() + "two 2" + System.lineSeparator() + "three 3" + System.lineSeparator() + "four 4" + System.lineSeparator() + "4 rows x 2 columns", w.print(df).toString()); }
@Test public void testPrint_TruncateRows() { DataFrameTabularPrintWorker w = new DataFrameTabularPrintWorker(new StringBuilder(), 2, 10); assertEquals(System.lineSeparator() + "col1 column2" + System.lineSeparator() + "---- -------" + System.lineSeparator() + "one 1" + System.lineSeparator() + "..." + System.lineSeparator() + "four 4" + System.lineSeparator() + "4 rows x 2 columns", w.print(df).toString()); }
@Test public void testPrint_TruncateColumns() { DataFrameTabularPrintWorker w = new DataFrameTabularPrintWorker(new StringBuilder(), 5, 4); assertEquals(System.lineSeparator() + "col1 c..2" + System.lineSeparator() + "---- ----" + System.lineSeparator() + "one 1" + System.lineSeparator() + "two 2" + System.lineSeparator() + "t..e 3" + System.lineSeparator() + "four 4" + System.lineSeparator() + "4 rows x 2 columns", w.print(df).toString()); }