@@ -243,6 +243,8 @@ def dup
243
243
244
244
# Iterate over each vector
245
245
def each_vector ( &block )
246
+ return to_enum ( :each_vector ) unless block_given?
247
+
246
248
@data . each ( &block )
247
249
248
250
self
@@ -252,6 +254,8 @@ def each_vector(&block)
252
254
253
255
# Iterate over each vector alongwith the name of the vector
254
256
def each_vector_with_index ( &block )
257
+ return to_enum ( :each_vector_with_index ) unless block_given?
258
+
255
259
@vectors . each do |vector |
256
260
yield @data [ @vectors [ vector ] ] , vector
257
261
end
@@ -263,6 +267,8 @@ def each_vector_with_index(&block)
263
267
264
268
# Iterate over each row
265
269
def each_row ( &block )
270
+ return to_enum ( :each_row ) unless block_given?
271
+
266
272
@index . each do |index |
267
273
yield access_row ( index )
268
274
end
@@ -271,6 +277,8 @@ def each_row(&block)
271
277
end
272
278
273
279
def each_row_with_index ( &block )
280
+ return to_enum ( :each_row_with_index ) unless block_given?
281
+
274
282
@index . each do |index |
275
283
yield access_row ( index ) , index
276
284
end
@@ -279,22 +287,27 @@ def each_row_with_index(&block)
279
287
end
280
288
281
289
# Map each vector. Returns a DataFrame whose vectors are modified according
282
- # to the value returned by the block. As is the case with Enumerable#map,
283
- # the object returned by each block must be a Daru::Vector for the dataframe
284
- # to remain relevant.
290
+ # to the value returned by the block. As is the case with Enumerable#map,
291
+ # the object returned by each block must be a Daru::Vector for the dataframe
292
+ # to remain relevant.
285
293
def map_vectors ( &block )
294
+ return to_enum ( :map_vectors ) unless block_given?
295
+
286
296
self . dup . map_vectors! ( &block )
287
297
end
288
298
289
299
# Destructive form of #map_vectors
290
300
def map_vectors! ( &block )
291
- @data . map! ( & block )
301
+ return to_enum ( :map_vectors! ) unless block_given?
292
302
303
+ @data . map! ( &block )
293
304
self
294
305
end
295
306
296
307
# Map vectors alongwith the index.
297
308
def map_vectors_with_index ( &block )
309
+ return to_enum ( :map_vectors_with_index ) unless block_given?
310
+
298
311
df = self . dup
299
312
df . each_vector_with_index do |vector , name |
300
313
df [ name , :vector ] = yield ( vector , name )
@@ -305,6 +318,8 @@ def map_vectors_with_index(&block)
305
318
306
319
# Map each row
307
320
def map_rows ( &block )
321
+ return to_enum ( :map_rows ) unless block_given?
322
+
308
323
df = self . dup
309
324
df . each_row_with_index do |row , index |
310
325
df [ index , :row ] = yield ( row )
@@ -314,6 +329,8 @@ def map_rows(&block)
314
329
end
315
330
316
331
def map_rows_with_index ( &block )
332
+ return to_enum ( :map_rows_with_index ) unless block_given?
333
+
317
334
df = self . dup
318
335
df . each_row_with_index do |row , index |
319
336
df [ index , :row ] = yield ( row , index )
@@ -374,6 +391,8 @@ def keep_vector_if &block
374
391
# Iterates over each row and retains it in a new DataFrame if the block returns
375
392
# true for that row.
376
393
def filter_rows &block
394
+ return to_enum ( :filter_rows ) unless block_given?
395
+
377
396
df = Daru ::DataFrame . new ( { } , order : @vectors . to_a )
378
397
marked = [ ]
379
398
@@ -392,6 +411,8 @@ def filter_rows &block
392
411
# Iterates over each vector and retains it in a new DataFrame if the block returns
393
412
# true for that vector.
394
413
def filter_vectors &block
414
+ return to_enum ( :filter_vectors ) unless block_given?
415
+
395
416
df = self . dup
396
417
df . keep_vector_if &block
397
418
0 commit comments