@@ -63,8 +63,8 @@ export class Table implements DataFrame {
6363 }
6464
6565 public readonly schema : Schema ;
66+ public readonly length : number ;
6667 public readonly numCols : number ;
67- public readonly numRows : number ;
6868 // List of inner RecordBatches
6969 public readonly batches : RecordBatch [ ] ;
7070 // List of inner Vectors, possibly spanning batches
@@ -98,12 +98,21 @@ export class Table implements DataFrame {
9898 columns . map ( ( col , idx ) => col . concat ( batch . columns [ idx ] ) ) ,
9999 batches [ 0 ] . columns
100100 ) ;
101+ this . length = this . batchesUnion . length ;
101102 this . numCols = this . batchesUnion . numCols ;
102- this . numRows = this . batchesUnion . numRows ;
103103 }
104104 public get ( index : number ) : Struct [ 'TValue' ] {
105105 return this . batchesUnion . get ( index ) ! ;
106106 }
107+ public getColumn ( name : string ) {
108+ return this . getColumnAt ( this . getColumnIndex ( name ) ) ;
109+ }
110+ public getColumnAt ( index : number ) {
111+ return this . columns [ index ] ;
112+ }
113+ public getColumnIndex ( name : string ) {
114+ return this . schema . fields . findIndex ( ( f ) => f . name === name ) ;
115+ }
107116 public [ Symbol . iterator ] ( ) : IterableIterator < Struct [ 'TValue' ] > {
108117 return this . batchesUnion [ Symbol . iterator ] ( ) as any ;
109118 }
@@ -116,7 +125,7 @@ export class Table implements DataFrame {
116125 // load batches
117126 const batch = batches [ batchIndex ] ;
118127 // yield all indices
119- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
128+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
120129 next ( index , batch ) ;
121130 }
122131 }
@@ -142,7 +151,7 @@ export class Table implements DataFrame {
142151 count_by . bind ( batch ) ;
143152 const keys = ( count_by . vector as DictionaryVector ) . indicies ;
144153 // yield all indices
145- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
154+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
146155 let key = keys . get ( index ) ;
147156 if ( key !== null ) { counts [ key ] ++ ; }
148157 }
@@ -152,6 +161,13 @@ export class Table implements DataFrame {
152161 public select ( ...columnNames : string [ ] ) {
153162 return new Table ( this . batches . map ( ( batch ) => batch . select ( ...columnNames ) ) ) ;
154163 }
164+ public toString ( separator ?: string ) {
165+ let str = '' ;
166+ for ( const row of this . rowsToString ( separator ) ) {
167+ str += row + '\n' ;
168+ }
169+ return str ;
170+ }
155171 public rowsToString ( separator = ' | ' ) : TableToStringIterator {
156172 return new TableToStringIterator ( tableRowsToString ( this , separator ) ) ;
157173 }
@@ -176,7 +192,7 @@ class FilteredDataFrame implements DataFrame {
176192 const batch = batches [ batchIndex ] ;
177193 const predicate = this . predicate . bind ( batch ) ;
178194 // yield all indices
179- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
195+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
180196 if ( predicate ( index , batch ) ) { next ( index , batch ) ; }
181197 }
182198 }
@@ -196,7 +212,7 @@ class FilteredDataFrame implements DataFrame {
196212 const batch = batches [ batchIndex ] ;
197213 const predicate = this . predicate . bind ( batch ) ;
198214 // yield all indices
199- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
215+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
200216 if ( predicate ( index , batch ) ) { ++ sum ; }
201217 }
202218 }
@@ -229,7 +245,7 @@ class FilteredDataFrame implements DataFrame {
229245 count_by . bind ( batch ) ;
230246 const keys = ( count_by . vector as DictionaryVector ) . indicies ;
231247 // yield all indices
232- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
248+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
233249 let key = keys . get ( index ) ;
234250 if ( key !== null && predicate ( index , batch ) ) { counts [ key ] ++ ; }
235251 }
@@ -251,7 +267,7 @@ export class CountByResult extends Table implements DataFrame {
251267 public asJSON ( ) : Object {
252268 const [ values , counts ] = this . columns ;
253269 const result = { } as { [ k : string ] : number | null } ;
254- for ( let i = - 1 ; ++ i < this . numRows ; ) {
270+ for ( let i = - 1 ; ++ i < this . length ; ) {
255271 result [ values . get ( i ) ] = counts . get ( i ) ;
256272 }
257273 return result ;
@@ -282,20 +298,20 @@ export class TableToStringIterator implements IterableIterator<string> {
282298 }
283299}
284300
285- function * tableRowsToString ( table : Table , separator = ' | ' ) {
301+ function * tableRowsToString ( table : Table , separator = ' | ' ) {
286302 const fields = table . schema . fields ;
287303 const header = [ 'row_id' , ...fields . map ( ( f ) => `${ f } ` ) ] . map ( stringify ) ;
288304 const maxColumnWidths = header . map ( x => x . length ) ;
289305 // Pass one to convert to strings and count max column widths
290- for ( let i = - 1 , n = table . numRows - 1 ; ++ i < n ; ) {
306+ for ( let i = - 1 , n = table . length - 1 ; ++ i < n ; ) {
291307 let val , row = [ i , ...table . get ( i ) ] ;
292308 for ( let j = - 1 , k = row . length ; ++ j < k ; ) {
293309 val = stringify ( row [ j ] ) ;
294310 maxColumnWidths [ j ] = Math . max ( maxColumnWidths [ j ] , val . length ) ;
295311 }
296312 }
297313 yield header . map ( ( x , j ) => leftPad ( x , ' ' , maxColumnWidths [ j ] ) ) . join ( separator ) ;
298- for ( let i = - 1 , n = table . numRows ; ++ i < n ; ) {
314+ for ( let i = - 1 , n = table . length ; ++ i < n ; ) {
299315 yield [ i , ...table . get ( i ) ]
300316 . map ( ( x ) => stringify ( x ) )
301317 . map ( ( x , j ) => leftPad ( x , ' ' , maxColumnWidths [ j ] ) )
0 commit comments