SciRuby / daru

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

lag should support negative arguments #354

Closed parthm closed 7 years ago

parthm commented 7 years ago

At the moment, if we call vector.lag -1 the vector returned has all nils. It would be better if lag supported positive as well as negative shifts.

[16] pry(main)> v = Daru::Vector.new(1..5)
=> #<Daru::Vector(5)>
   0   1
   1   2
   2   3
   3   4
   4   5
[18] pry(main)> v.lag 1
=> #<Daru::Vector(5)>
   0 nil
   1   1
   2   2
   3   3
   4   4
[19] pry(main)> v.lag -1
=> #<Daru::Vector(5)>
   0 nil
   1 nil
   2 nil
   3 nil
   4 nil
[20] pry(main)> 

Behavior for pandas shift function for reference:

In [7]: s = pd.Series(range(5))

In [8]: s
Out[8]: 
0    0
1    1
2    2
3    3
4    4
dtype: int32

In [9]: s.shift(1)
Out[9]: 
0    NaN
1    0.0
2    1.0
3    2.0
4    3.0
dtype: float64

In [10]: s.shift(-1)
Out[10]: 
0    1.0
1    2.0
2    3.0
3    4.0
4    NaN
dtype: float64
parthm commented 7 years ago

I suppose this can be marked as fixed as its closed in #355

zverok commented 7 years ago

:+1: