Skip to content

Commit 9137dab

Browse files
committed
iterators return enumertor if no block
1 parent 315e7a5 commit 9137dab

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

History.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@
7474
* Added statistics methods #mean, #std, #max, #min, #count, #product, #sum to DataFrame.
7575
* Added #describe to DataFrame for producing multiple statistics data of numerical vectors in one shot.
7676
* Monkey patched Ruby Matrix to include #elementwise_division.
77-
* Added #covariance to calculate the covariance between numbers of a DataFrame and #correlation to calculate correlation.
77+
* Added #covariance to calculate the covariance between numbers of a DataFrame and #correlation to calculate correlation.
78+
* Enumerators return Enumerator objects if there is no block.

lib/daru/dataframe.rb

+25-4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ def dup
243243

244244
# Iterate over each vector
245245
def each_vector(&block)
246+
return to_enum(:each_vector) unless block_given?
247+
246248
@data.each(&block)
247249

248250
self
@@ -252,6 +254,8 @@ def each_vector(&block)
252254

253255
# Iterate over each vector alongwith the name of the vector
254256
def each_vector_with_index(&block)
257+
return to_enum(:each_vector_with_index) unless block_given?
258+
255259
@vectors.each do |vector|
256260
yield @data[@vectors[vector]], vector
257261
end
@@ -263,6 +267,8 @@ def each_vector_with_index(&block)
263267

264268
# Iterate over each row
265269
def each_row(&block)
270+
return to_enum(:each_row) unless block_given?
271+
266272
@index.each do |index|
267273
yield access_row(index)
268274
end
@@ -271,6 +277,8 @@ def each_row(&block)
271277
end
272278

273279
def each_row_with_index(&block)
280+
return to_enum(:each_row_with_index) unless block_given?
281+
274282
@index.each do |index|
275283
yield access_row(index), index
276284
end
@@ -279,22 +287,27 @@ def each_row_with_index(&block)
279287
end
280288

281289
# 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.
285293
def map_vectors(&block)
294+
return to_enum(:map_vectors) unless block_given?
295+
286296
self.dup.map_vectors!(&block)
287297
end
288298

289299
# Destructive form of #map_vectors
290300
def map_vectors!(&block)
291-
@data.map!(&block)
301+
return to_enum(:map_vectors!) unless block_given?
292302

303+
@data.map!(&block)
293304
self
294305
end
295306

296307
# Map vectors alongwith the index.
297308
def map_vectors_with_index(&block)
309+
return to_enum(:map_vectors_with_index) unless block_given?
310+
298311
df = self.dup
299312
df.each_vector_with_index do |vector, name|
300313
df[name, :vector] = yield(vector, name)
@@ -305,6 +318,8 @@ def map_vectors_with_index(&block)
305318

306319
# Map each row
307320
def map_rows(&block)
321+
return to_enum(:map_rows) unless block_given?
322+
308323
df = self.dup
309324
df.each_row_with_index do |row, index|
310325
df[index, :row] = yield(row)
@@ -314,6 +329,8 @@ def map_rows(&block)
314329
end
315330

316331
def map_rows_with_index(&block)
332+
return to_enum(:map_rows_with_index) unless block_given?
333+
317334
df = self.dup
318335
df.each_row_with_index do |row, index|
319336
df[index, :row] = yield(row, index)
@@ -374,6 +391,8 @@ def keep_vector_if &block
374391
# Iterates over each row and retains it in a new DataFrame if the block returns
375392
# true for that row.
376393
def filter_rows &block
394+
return to_enum(:filter_rows) unless block_given?
395+
377396
df = Daru::DataFrame.new({}, order: @vectors.to_a)
378397
marked = []
379398

@@ -392,6 +411,8 @@ def filter_rows &block
392411
# Iterates over each vector and retains it in a new DataFrame if the block returns
393412
# true for that vector.
394413
def filter_vectors &block
414+
return to_enum(:filter_vectors) unless block_given?
415+
395416
df = self.dup
396417
df.keep_vector_if &block
397418

spec/dataframe_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,11 @@
745745

746746
expect(ret).to eq(@data_frame)
747747
end
748+
749+
it "returns Enumerable if no block specified" do
750+
ret = @data_frame.each_vector
751+
expect(ret.is_a?(Enumerator)).to eq(true)
752+
end
748753
end
749754

750755
context Daru::MultiIndex do

0 commit comments

Comments
 (0)