@@ -63,8 +63,8 @@ export class Table implements DataFrame {
63
63
}
64
64
65
65
public readonly schema : Schema ;
66
+ public readonly length : number ;
66
67
public readonly numCols : number ;
67
- public readonly numRows : number ;
68
68
// List of inner RecordBatches
69
69
public readonly batches : RecordBatch [ ] ;
70
70
// List of inner Vectors, possibly spanning batches
@@ -98,12 +98,21 @@ export class Table implements DataFrame {
98
98
columns . map ( ( col , idx ) => col . concat ( batch . columns [ idx ] ) ) ,
99
99
batches [ 0 ] . columns
100
100
) ;
101
+ this . length = this . batchesUnion . length ;
101
102
this . numCols = this . batchesUnion . numCols ;
102
- this . numRows = this . batchesUnion . numRows ;
103
103
}
104
104
public get ( index : number ) : Struct [ 'TValue' ] {
105
105
return this . batchesUnion . get ( index ) ! ;
106
106
}
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
+ }
107
116
public [ Symbol . iterator ] ( ) : IterableIterator < Struct [ 'TValue' ] > {
108
117
return this . batchesUnion [ Symbol . iterator ] ( ) as any ;
109
118
}
@@ -116,7 +125,7 @@ export class Table implements DataFrame {
116
125
// load batches
117
126
const batch = batches [ batchIndex ] ;
118
127
// yield all indices
119
- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
128
+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
120
129
next ( index , batch ) ;
121
130
}
122
131
}
@@ -142,7 +151,7 @@ export class Table implements DataFrame {
142
151
count_by . bind ( batch ) ;
143
152
const keys = ( count_by . vector as DictionaryVector ) . indicies ;
144
153
// yield all indices
145
- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
154
+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
146
155
let key = keys . get ( index ) ;
147
156
if ( key !== null ) { counts [ key ] ++ ; }
148
157
}
@@ -152,6 +161,13 @@ export class Table implements DataFrame {
152
161
public select ( ...columnNames : string [ ] ) {
153
162
return new Table ( this . batches . map ( ( batch ) => batch . select ( ...columnNames ) ) ) ;
154
163
}
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
+ }
155
171
public rowsToString ( separator = ' | ' ) : TableToStringIterator {
156
172
return new TableToStringIterator ( tableRowsToString ( this , separator ) ) ;
157
173
}
@@ -176,7 +192,7 @@ class FilteredDataFrame implements DataFrame {
176
192
const batch = batches [ batchIndex ] ;
177
193
const predicate = this . predicate . bind ( batch ) ;
178
194
// yield all indices
179
- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
195
+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
180
196
if ( predicate ( index , batch ) ) { next ( index , batch ) ; }
181
197
}
182
198
}
@@ -196,7 +212,7 @@ class FilteredDataFrame implements DataFrame {
196
212
const batch = batches [ batchIndex ] ;
197
213
const predicate = this . predicate . bind ( batch ) ;
198
214
// yield all indices
199
- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
215
+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
200
216
if ( predicate ( index , batch ) ) { ++ sum ; }
201
217
}
202
218
}
@@ -229,7 +245,7 @@ class FilteredDataFrame implements DataFrame {
229
245
count_by . bind ( batch ) ;
230
246
const keys = ( count_by . vector as DictionaryVector ) . indicies ;
231
247
// yield all indices
232
- for ( let index = - 1 , numRows = batch . numRows ; ++ index < numRows ; ) {
248
+ for ( let index = - 1 , numRows = batch . length ; ++ index < numRows ; ) {
233
249
let key = keys . get ( index ) ;
234
250
if ( key !== null && predicate ( index , batch ) ) { counts [ key ] ++ ; }
235
251
}
@@ -251,7 +267,7 @@ export class CountByResult extends Table implements DataFrame {
251
267
public asJSON ( ) : Object {
252
268
const [ values , counts ] = this . columns ;
253
269
const result = { } as { [ k : string ] : number | null } ;
254
- for ( let i = - 1 ; ++ i < this . numRows ; ) {
270
+ for ( let i = - 1 ; ++ i < this . length ; ) {
255
271
result [ values . get ( i ) ] = counts . get ( i ) ;
256
272
}
257
273
return result ;
@@ -282,20 +298,20 @@ export class TableToStringIterator implements IterableIterator<string> {
282
298
}
283
299
}
284
300
285
- function * tableRowsToString ( table : Table , separator = ' | ' ) {
301
+ function * tableRowsToString ( table : Table , separator = ' | ' ) {
286
302
const fields = table . schema . fields ;
287
303
const header = [ 'row_id' , ...fields . map ( ( f ) => `${ f } ` ) ] . map ( stringify ) ;
288
304
const maxColumnWidths = header . map ( x => x . length ) ;
289
305
// 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 ; ) {
291
307
let val , row = [ i , ...table . get ( i ) ] ;
292
308
for ( let j = - 1 , k = row . length ; ++ j < k ; ) {
293
309
val = stringify ( row [ j ] ) ;
294
310
maxColumnWidths [ j ] = Math . max ( maxColumnWidths [ j ] , val . length ) ;
295
311
}
296
312
}
297
313
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 ; ) {
299
315
yield [ i , ...table . get ( i ) ]
300
316
. map ( ( x ) => stringify ( x ) )
301
317
. map ( ( x , j ) => leftPad ( x , ' ' , maxColumnWidths [ j ] ) )
0 commit comments