SciRuby / daru

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

#tail and #head not working for Daru::DataFrame #341

Closed pedrorio closed 7 years ago

pedrorio commented 7 years ago

Hello, I was trying to backtest some bitcoin investing strategies and the #tail and #head methods do not work unless I specify a Daru::Vector.

For instance, p df[0].tail works but p df.tail does not.

I was using daru from rubygems and changed the gem source to this github repo but the #summary method also broke. I am using a Daru::DateTimeIndex index.

Here is my error:

/usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/index/index.rb:209:in `key': undefined method `[]' for nil:NilClass (NoMethodError)
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/index/index.rb:151:in `map'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/index/index.rb:151:in `at'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/vector.rb:250:in `at'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/vector.rb:1183:in `reject_values'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/maths/statistics/vector.rb:817:in `midpoint_percentile'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/maths/statistics/vector.rb:352:in `percentile'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/maths/statistics/vector.rb:27:in `median'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/vector.rb:955:in `numeric_summary'
    from /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/daru-7b70cfadf4d2/lib/daru/vector.rb:932:in `summary'
    from btc.rb:96:in `<main>'
v0dro commented 7 years ago

Thanks for your report. We'll have a look shortly.

zverok commented 7 years ago

@pedrorio could you please show us the DataFrame you are working with?.. Basic tests for #head and #tail seems to be working, as well as my manual testing.

cpankaj commented 7 years ago

DataFrame head and tail doesn't work if it has DateTimeIndex duplicate of https://github.com/SciRuby/daru/issues/331 You can reproduce using below code


require 'daru'

date_array = ['2017-01-01', '2017-02-01', '2017-03-01']
vector_array = ['a', 'b', 'c']

idx = Daru::DateTimeIndex.new(date_array)
df = Daru::DataFrame.new({col1: vector_array}, index: idx)

p df.head
baarkerlounger commented 7 years ago

This is fixed and should be closed.