SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.04k stars 139 forks source link

Loading a headers only csv should create empty DataFrame with headers #349

Closed parthm closed 7 years ago

parthm commented 7 years ago

Trying to load a CSV with only headers errors out right now. It would be better it an empty DataFrame is created with the headers. Daru: 0.1.5 Ruby: 2.3.3 File attached as zip as CSV attachments are not supported. column_headers_only.zip

As it stands now a the CSV file needs to be loaded and checked if empty before we can load it.

user@vbox:~/tmp$ cat column_headers_only.csv 
col0,col1,col2
user@vbox:~/tmp$ pry
[1] pry(main)> require 'daru'
=> true
[2] pry(main)> df = Daru::DataFrame.from_csv('column_headers_only.csv')
NoMethodError: undefined method `size' for nil:NilClass
from /home/user/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/daru-0.1.5/lib/daru/dataframe.rb:2383:in `initialize_from_hash_with_arrays'
[3] pry(main)> 
zverok commented 7 years ago

Accepted.

athityakumar commented 7 years ago

@parthm - Thanks for reporting this. 👍 @zverok - If this issue doesn't get fixed by June end, I'd like to tackle this in daru-io.

info-rchitect commented 7 years ago

As a temporary workaround I did this:

headers = CSV.open(mycsv, &:readline)
daru_hash = {}.tap do |daru|
  headers.each {|h| daru[h] = []}
end
my_dataframe = Daru::Dataframe.new(daru_hash, order: headers)
athityakumar commented 7 years ago

This has been fixed in daru-io with https://github.com/athityakumar/daru-io/pull/33