@@ -9,8 +9,7 @@ import scala.reflect.ClassTag
99class  Series [I , @ specialized(Double , Int , Float , Long ) V ]
1010(val  idx :  IndexVector [I ], val  data :  Vector [V ])(implicit  iTag: ClassTag [I ], vTag : ClassTag [V ], vSem : Semiring [V ])
1111  extends  scala.collection.Map [I , V ]
12-     with  Slice1dOps [I , Series [I , V ]]
13- {
12+     with  Slice1dOps [I , Series [I , V ]] {
1413
1514  if  (idx.size !=  data.length) {
1615    throw  new  IllegalArgumentException (" index.length != values.length" 
@@ -46,29 +45,29 @@ class Series[I, @specialized(Double, Int, Float, Long) V]
4645
4746  override  def  toString :  String  =  toString(5 , 5 )
4847
49-   def  toString (head : Int , tail: Int , indexFormat: I => String  =  _.toString, dataFormat : V => String  =  _.toString):  String  =  {
50-     val  rows  =  Math .min(idx.size, head+ tail+ 1 )
51-     val  output  =  Array .ofDim[String ](rows+ 2 , 2 )
48+   def  toString (head : Int , tail :  Int , indexFormat :  I   =>   String  =  _.toString, dataFormat : V   =>   String  =  _.toString):  String  =  {
49+     val  rows  =  Math .min(idx.size, head  +   tail  +   1 )
50+     val  output  =  Array .ofDim[String ](rows  +   2 , 2 )
5251    output(0 )(0 ) =  s " Series[ ${iTag.runtimeClass.getSimpleName}, ${vTag.runtimeClass.getSimpleName}]: " 
5352    output(0 )(1 ) =  " " 
5453    output(1 )(0 ) =  s " index " 
5554    output(1 )(1 ) =  s " value " 
56-     for (i <-  0  until rows) {
55+     for   (i <-  0  until rows) {
5756      val  j  =  if  (i <  head) i else  (idx.size -  rows +  i)
58-       output(2 + i)(0 ) =  indexFormat(idx(j))
59-       output(2 + i)(1 ) =  dataFormat(data(j))
57+       output(2   +   i)(0 ) =  indexFormat(idx(j))
58+       output(2   +   i)(1 ) =  dataFormat(data(j))
6059      if  (rows <  idx.size &&  i ==  head) {
61-         output(2 + i)(0 ) =  " ..." 
62-         output(2 + i)(1 ) =  " ..." 
60+         output(2   +   i)(0 ) =  " ..." 
61+         output(2   +   i)(1 ) =  " ..." 
6362      }
6463    }
6564    //  adjust column size
66-     for (c <-  0  to 1 ) {
65+     for   (c <-  0  to 1 ) {
6766      var  size  =  0 
68-       for (r <-  1  until output.length) {
67+       for   (r <-  1  until output.length) {
6968        size =  Math .max(size, output(r)(c).length)
7069      }
71-       for (r <-    1  until output.length) {
70+       for   (r <-  1  until output.length) {
7271        output(r)(c) =  output(r)(c) +  "  " *  (size -  output(r)(c).length)
7372      }
7473    }
@@ -95,23 +94,23 @@ class Series[I, @specialized(Double, Int, Float, Long) V]
9594    this .align(idx)
9695  }
9796
98-   def  align (idx : IndexVector [I ]):  Series [I ,V ] =  {
97+   def  align (idx : IndexVector [I ]):  Series [I ,  V ] =  {
9998    val  result  =  {
100-       if (this .idx ==  idx) this 
101-       else  if  (idx.forall(i =>  this .idx.contains(i))) Series [I ,V ](idx, this .data(this .idx.loc(idx.toIndexedSeq).slices))
99+       if   (this .idx ==  idx) this 
100+       else  if  (idx.forall(i =>  this .idx.contains(i))) Series [I ,  V ](idx, this .data(this .idx.loc(idx.toIndexedSeq).slices))
102101      else  {
103102        var  r  =  Series (idx, new  DenseVector [V ](idx.size))
104103        var  ii  =  idx.intersect(this .idx)
105-         for (k <-  ii.toIndexedSeq) {
106-           r(k) =  this (k)
104+         for   (k <-  ii.toIndexedSeq) {
105+           r(k) =  this   (k)
107106        }
108107        r
109108      }
110109    }
111110    result
112111  }
113112
114-   def  fillLike (fillValue: V ):  Series [I , V ] =  Series .fill(idx, fillValue)
113+   def  fillLike (fillValue :  V ):  Series [I , V ] =  Series .fill(idx, fillValue)
115114
116115  override  def  get (key : I ):  Option [V ] =  idx.hashIndexOf(key).map(i =>  data(i))
117116
@@ -120,6 +119,28 @@ class Series[I, @specialized(Double, Int, Float, Long) V]
120119  override  def  - (key : I ):  collection.Map [I , V ] =  ??? 
121120
122121  override  def  - (key1 : I , key2 : I , keys : I * ):  collection.Map [I , V ] =  ??? 
122+ 
123+   def  shift (period: Int , fillValue : V ):  Series [I , V ]  =  {
124+     val  result  =  copy
125+     if (period >  0 ) {
126+       for (i <-  idx.indices) {
127+         if (i <  period) {
128+           result.data.update(i, fillValue)
129+         } else  {
130+           result.data.update(i, this .data(i -  period))
131+         }
132+       }
133+     } else  if  (period <  0 ) {
134+       for (i <-  idx.indices) {
135+         if (i >  idx.indices.last -  period) {
136+           result.data.update(i, fillValue)
137+         } else  {
138+           result.data.update(i, this .data(i +  period))
139+         }
140+       }
141+     }
142+     result
143+   }
123144}
124145
125146object  Series  {
0 commit comments