spec/io/io_spec.rb in daru-0.1.2 vs spec/io/io_spec.rb in daru-0.1.3

- old
+ new

@@ -2,22 +2,22 @@ describe Daru::IO do describe Daru::DataFrame do context ".from_csv" do it "loads from a CSV file" do - df = Daru::DataFrame.from_csv('spec/fixtures/matrix_test.csv', + df = Daru::DataFrame.from_csv('spec/fixtures/matrix_test.csv', col_sep: ' ', headers: true) df.vectors = [:image_resolution, :mls, :true_transform].to_index expect(df.vectors).to eq([:image_resolution, :mls, :true_transform].to_index) expect(df[:image_resolution].first).to eq(6.55779) expect(df[:true_transform].first).to eq("-0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,4262.65,0,0,0,1") end it "works properly for repeated headers" do df = Daru::DataFrame.from_csv('spec/fixtures/repeated_fields.csv',header_converters: :symbol) - expect(df.vectors.to_a).to eq(['a1', 'age_1', 'age_2', 'city', 'id', 'name_1', 'name_2']) + expect(df.vectors.to_a).to eq(["id", "name_1", "age_1", "city", "a1", "name_2", "age_2"]) age = Daru::Vector.new([3, 4, 5, 6, nil, 8]) expect(df['age_2']).to eq(age) end @@ -27,35 +27,56 @@ y = [9.629587310436753e+127, 1.9341543147883677e+129, 3.88485279048245e+130] y.zip(ds['y']).each do |y_expected, y_ds| expect(y_ds).to be_within(0.001).of(y_expected) end end + + it "follows the order of columns given in CSV" do + df = Daru::DataFrame.from_csv 'spec/fixtures/sales-funnel.csv' + expect(df.vectors.to_a).to eq(%W[Account Name Rep Manager Product Quantity Price Status]) + end end context "#write_csv" do - it "writes DataFrame to a CSV file" do - df = Daru::DataFrame.new({ - 'a' => [1,2,3,4,5], + before do + @df = Daru::DataFrame.new({ + 'a' => [1,2,3,4,5], 'b' => [11,22,33,44,55], 'c' => ['a', 'g', 4, 5,'addadf'], 'd' => [nil, 23, 4,'a','ff']}) - t = Tempfile.new('data.csv') - df.write_csv t.path + @tempfile = Tempfile.new('data.csv') - expect(Daru::DataFrame.from_csv(t.path)).to eq(df) end + + it "writes DataFrame to a CSV file" do + @df.write_csv @tempfile.path + expect(Daru::DataFrame.from_csv(@tempfile.path)).to eq(@df) + end + + it "will write headers unless headers=false" do + @df.write_csv @tempfile.path + first_line = File.open(@tempfile.path, &:readline).chomp.split(',', -1) + expect(first_line).to eq @df.vectors.to_a + end + + it "will not write headers when headers=false" do + @df.write_csv @tempfile.path, { headers: false } + first_line = File.open(@tempfile.path, &:readline).chomp.split(',', -1) + expect(first_line).to eq @df.head(1).map { |v| (v.first || '').to_s } + end + end context ".from_excel" do before do id = Daru::Vector.new([1, 2, 3, 4, 5, 6]) name = Daru::Vector.new(%w(Alex Claude Peter Franz George Fernand)) age = Daru::Vector.new( [20, 23, 25, nil, 5.5, nil]) city = Daru::Vector.new(['New York', 'London', 'London', 'Paris', 'Tome', nil]) a1 = Daru::Vector.new(['a,b', 'b,c', 'a', nil, 'a,b,c', nil]) - @expected = Daru::DataFrame.new({ - :id => id, :name => name, :age => age, :city => city, :a1 => a1 + @expected = Daru::DataFrame.new({ + :id => id, :name => name, :age => age, :city => city, :a1 => a1 }, order: [:id, :name, :age, :city, :a1]) end it "loads DataFrame from an Excel Spreadsheet" do df = Daru::DataFrame.from_excel 'spec/fixtures/test_xls.xls' @@ -188,12 +209,12 @@ json = File.read 'spec/fixtures/countries.json' df = Daru::DataFrame.new JSON.parse(json) expect(df.vectors).to eq([ - 'name', 'nativeName', 'tld', 'cca2', 'ccn3', 'cca3', 'currency', 'callingCode', - 'capital', 'altSpellings', 'relevance', 'region', 'subregion', 'language', + 'name', 'nativeName', 'tld', 'cca2', 'ccn3', 'cca3', 'currency', 'callingCode', + 'capital', 'altSpellings', 'relevance', 'region', 'subregion', 'language', 'languageCodes', 'translations', 'latlng', 'demonym', 'borders', 'area'].to_index) expect(df.row[0]['name']).to eq("Afghanistan") end end @@ -206,13 +227,13 @@ end end context "#save" do before do - @data_frame = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5], - c: [11,22,33,44,55]}, - order: [:a, :b, :c], + @data_frame = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5], + c: [11,22,33,44,55]}, + order: [:a, :b, :c], index: [:one, :two, :three, :four, :five]) end it "saves df to a file" do outfile = Tempfile.new('dataframe.df') @@ -233,10 +254,10 @@ context "#save" do ALL_DTYPES.each do |dtype| it "saves to a file and returns the same Vector of type #{dtype}" do vector = Daru::Vector.new( - [5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99], + [5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99], dtype: dtype) outfile = Tempfile.new('vector.vec') vector.save(outfile.path) expect(Daru::IO.load(outfile.path)).to eq(vector) end