Class: Daru::IO::Importers::Excel

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/importers/excel.rb

Overview

Excel Importer Class, that extends read_excel method to Daru::DataFrame

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from, guess_parse

Methods inherited from Base

#optional_gem

Constructor Details

#initializeExcel

Checks for required gem dependencies of Excel Importer



20
21
22
# File 'lib/daru/io/importers/excel.rb', line 20

def initialize
  optional_gem 'spreadsheet', '~> 1.1.1'
end

Class Method Details

.read(path) ⇒ Daru::IO::Importers::Excel

Reads from an excel (.xls) file

Examples:

Reading from an excel file

instance = Daru::IO::Importers::Excel.read("test_xls.xls")

Parameters:

  • path (String)

    Path of Excel file, where the DataFrame is to be imported from.

Returns:



34
35
36
37
# File 'lib/daru/io/importers/excel.rb', line 34

def read(path)
  @file_data = Spreadsheet.open(path)
  self
end

Instance Method Details

#call(worksheet_id: 0, headers: true) ⇒ Daru::DataFrame

Imports a Daru::DataFrame from an Excel Importer instance

Examples:

Importing from a default worksheet

df = instance.call

#=> #<Daru::DataFrame(6x5)>
#            id     name      age     city       a1
#    0        1     Alex       20 New York      a,b
#    1        2   Claude       23   London      b,c
#    2        3    Peter       25   London        a
#    3        4    Franz      nil    Paris      nil
#    4        5   George      5.5     Tome    a,b,c
#    5        6  Fernand      nil      nil      nil

Importing from a specific worksheet

df = instance.call(worksheet_id: 0)

#=> #<Daru::DataFrame(6x5)>
#            id     name      age     city       a1
#    0        1     Alex       20 New York      a,b
#    1        2   Claude       23   London      b,c
#    2        3    Peter       25   London        a
#    3        4    Franz      nil    Paris      nil
#    4        5   George      5.5     Tome    a,b,c
#    5        6  Fernand      nil      nil      nil

Parameters:

  • worksheet_id (Integer) (defaults to: 0)

    The index of the worksheet in the excel file, from where the Daru::DataFrame will be imported. By default, the first worksheet has :worksheet_id as 0. In general, the n-th worksheet has its worksheet_id as n-1.

    If worksheet_id option is not given, it is taken as 0 by default and the Daru::DataFrame will be imported from the first worksheet in the excel file.

  • headers (Boolean) (defaults to: true)

    Defaults to true. When set to true, first row of the given worksheet_id is used as the order of the Daru::DataFrame and data of the Dataframe consists of the remaining rows.

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/daru/io/importers/excel.rb', line 79

def call(worksheet_id: 0, headers: true)
  worksheet = @file_data.worksheet(worksheet_id)
  headers   = if headers
                ArrayHelper.recode_repeated(worksheet.row(0)).map(&:to_sym)
              else
                (0..worksheet.row(0).to_a.size-1).to_a
              end

  df = Daru::DataFrame.new({})
  headers.each_with_index do |h,i|
    col = worksheet.column(i).to_a
    col.delete_at(0) if headers
    df[h] = col
  end

  df
end

#read(path) ⇒ Daru::IO::Importers::Excel

Reads from an excel (.xls) file

Examples:

Reading from an excel file

instance = Daru::IO::Importers::Excel.read("test_xls.xls")

Parameters:

  • path (String)

    Path of Excel file, where the DataFrame is to be imported from.

Returns:



34
35
36
37
# File 'lib/daru/io/importers/excel.rb', line 34

def read(path)
  @file_data = Spreadsheet.open(path)
  self
end